Explain the ingenious design of stETH, how does it automatically distribute income on a daily basis?

24-03-11 21:00
Read this article in 12 Minutes
总结 AI summary
View the summary 收起
Original title: "Ingenious contract design, see how stETH automatically distributes income on a daily basis?" Let your ETH participate in pledge to obtain stable interest"
Original source: ZAN Team


The author converted his few ETH into stETH Later, I found that ETH was growing naturally every day and continued to gain profits. But I don’t see any transactions in my account. Why? This article will take you through the ingenious design behind it and reveal the secret of revenue distribution.


1 stETH past few Tianhou has already made some profits


Before that, let me introduce the logic behind stETH earning profits, which is the staking of Ethereum. I already understand it. Readers of this concept can skip ahead.



The original Ethereum, like Bitcoin, used proof of work (Proof of Work (PoW) as its consensus mechanism, but due to PoW power consumption and other security and performance development considerations, Ethereum will be upgraded to Proof of Stake (PoS) from September 2022.


Ethereum, which originally relied on computing power mining to attract miners to achieve consensus, has transformed into relying on everyone to obtain voting rights by pledging ETH and voting. income, thereby motivating everyone to achieve consensus through PoS.


By staking 32 ETH, you can join the Ethereum network and become a validator, responsible for storing data, processing transactions, and adding new blocks to the blockchain. As long as the transaction is correctly packaged into a new block and the work of other validators is checked, you will be rewarded with ETH, which is equivalent to giving ETH a relatively stable income through staking.


But such a pledge is still too troublesome for ordinary users. After all, 32 Ethereums and one must be able to access the Ethereum network all year round. There are still certain thresholds for dedicated computers. Moreover, staking ETH will cause the liquidity of this part of ETH to be lost. So there are Liquid Staking Derivatives (LSD), which aim to solve the threshold and liquidity problems in traditional staking, allowing users to pledge less than 32 ETH without owning nodes. It is to entrust ETH to a third party for pledge and obtain the corresponding pledge tokens (such as Lido's stETH or Rocket Pool's rETH). These liquidity tokens can be traded, borrowed or used for other financial activities on other platforms. In this way, Users can not only participate in staking more conveniently to obtain rewards, but also maintain the flexibility of funds.



So the essential logic of stETH is to give ETH to Lido, and Lido will use these ETH to participate. To obtain income from Ethereum's PoS, users will receive the corresponding stETH as a voucher. The next step is for Lido to distribute the proceeds to these addresses that hold stETH.


We can see that the income of stETH is automatically updated every day. The picture below is the income situation of our test, which corresponds to the encryption wallet verification related content that can be checked every day.



But at this point I think students who are familiar with smart contract development will be confused. : With so little income distributed every day, the income may not be enough to pay for GAS.


Indeed, if Lido distributes income according to the simplest method, it will be difficult to cover the cost of GAS. From our intuition point of view, it is unimaginable for GAS to send tokens to so many addresses.


But it is true that Lido has achieved automatic growth of stETH income in the wallet, and we have not found any transactions at this address. How is this achieved?


We found Lido’s contract https://etherscan.io/token/0xae7ab96520de3a18e5e111b5eaab095312d7fe84 Trace back to the balanceOf method of the contract:



balanceOf is a method that complies with the ERC20 specification. The wallet uses this method to obtain how many tokens the user has.


We can see that the getPooledEthByShares method is called in the stETH contract. The input parameter of this method is  mapping (address => uint256) private shares;. How much stETH does this represent for the user? Obviously not, otherwise the data of each address would need to be updated every day. Although this can also be done by calling the method in the contract to update the shares, so that the tokens of all addresses can be updated in one transaction, but obviously this is not the case for GAS. The consumption is also huge.


By now everyone must have guessed how the contract is implemented. Let’s continue to look at the getPooledEthByShares method.



You can see that the final returned result is multiplied by the  sharesAmount  in the address  _getTotalPooledEther() Remove _getTotalShares.


_getTotalPooledEther Represents how much stETH there is in total (if stETH to ETH is 1:1, it also represents how much ETH there is), _getTotalShares represents how many shares there are. In this way, how much stETH each address has is calculated dynamically.


For example, if there are now a total of 1,000 shares (Shares, which is the number returned by the  _getTotalShares  method), of which the A address has 100 shares (corresponding to the above of sharesAmount). These 1000 shares correspond to the amount returned by 1000 stETH (that is,_getTotalPooledEther). Then according to this calculation, address A corresponds to 100 stETH. Then Lido takes the total 1,000 ETH to pledge and obtains the income of 1 ETH. Then the corresponding update _getTotalPooledEther is 1001, that is, the initial total of 1,000 stETH has increased to 1,001, then the new calculated A address is You have 100 * 1001 / 1000 = 100.1 stETH.


To put it simply, if the shares owned by each address remain unchanged and the stETH corresponding to the shares increases, then the stETH will naturally increase after calculation. .


Let’s continue to look at the code. The logic in  _getTotalPooledEther  will be affected by the  handleOracleReport method, and this method will update the contract related data. The specific call will be through https://etherscan.io/address/0x852deD011285fe67063a08005c71a85690503Cee The contract regularly calls submitReportData to update the data (submitReportData will call the handleOracleReport of the Lido contract):



We can see that there are calls to update related content every day, which is why although we cannot see There is a transaction that releases income to our address, but the amount still changes every day.


This actually reflects a feature of Ethereum ERC20 smart contracts, that is, the number of tokens owned by these ERC20 contracts is not hard-coded in the address, but It is returned by the contract method, so it may happen that although the account does not have any transactions, the number of tokens may also change. On the one hand, this makes the ERC20 contract more flexible, but on the other hand, it also brings a lot of confusion to many friends who are not familiar with the contract. I hope this article can help everyone understand smart contracts more and interact with smart contracts more safely.


In addition, although you can obtain seemingly stable staking income by pledging ETH to stETH, there are still possible risks. This article is only a review of the pledge contract. Technical research reference and does not constitute any investment advice.


Original link


欢迎加入律动 BlockBeats 官方社群:

Telegram 订阅群:https://t.me/theblockbeats

Telegram 交流群:https://t.me/BlockBeats_App

Twitter 官方账号:https://twitter.com/BlockBeatsAsia

Choose Library
Add Library
Cancel
Finish
Add Library
Visible to myself only
Public
Save
Correction/Report
Submit