Interpret the clever design of stETH

Intermediate3/22/2024, 10:07:31 PM
The fundamental logic of stETH is to deposit ETH into Lido. Lido utilizes these ETH to participate in Ethereum's PoS (Proof of Stake) to earn rewards, and users receive corresponding stETH as proof of their participation. Subsequently, Lido distributes the rewards to the addresses holding stETH.

Forwarded Title:Clever contract design: see how stETH automatically distributes profits daily? Let your ETH participate in staking to earn stable interest.

The author converted a small amount of Ether (ETH) into stable Ether (stETH) and found that the amount of stETH increased naturally every day, yielding continuous profits. However, there were no transaction records visible in the account. Why is this? This article will explore the clever design behind it and reveal the secret of profit distribution.

1 stETH has generated some profits after a few days.

Before diving into how stETH earns profits, let’s first introduce the logic behind it, which is Ethereum staking. Readers who are already familiar with this concept can skip ahead.

Initially, Ethereum, like Bitcoin, used Proof of Work (PoW) as its consensus mechanism. However, due to concerns over power consumption, security, and scalability, Ethereum transitioned to Proof of Stake (PoS) in September 2022.

Previously reliant on mining power to achieve consensus, Ethereum now relies on participants staking ETH to gain voting rights and earn rewards through voting. This incentivizes consensus through PoS.

By staking 32 ETH, one can join the Ethereum network as a validator, responsible for storing data, processing transactions, and adding new blocks to the blockchain. Validators receive ETH rewards for correctly packaging transactions into new blocks and verifying other validators’ work, allowing for relatively stable returns through staking.

However, traditional staking is cumbersome for ordinary users, as it requires 32 ETH and a dedicated computer with uninterrupted access to the Ethereum network. Additionally, staking ETH locks up liquidity. Hence, the emergence of Liquid Staking Derivatives (LSD), aimed at addressing the barriers and liquidity issues of traditional staking. LSD allows users to stake less than 32 ETH and delegate their ETH to third-party staking providers, receiving corresponding staking tokens (such as Lido’s stETH or Rocket Pool’s rETH). These liquid tokens can be traded, borrowed, or used for other financial activities on various platforms, enabling users to participate in staking more conveniently while maintaining liquidity.

So, the fundamental logic behind stETH is that users provide their ETH to Lido, which then uses these ETH to participate in Ethereum’s PoS to generate profits. In return, users receive stETH as proof of their stake. Next, Lido distributes the profits to the addresses holding stETH.

We can observe that the profits of stETH are updated automatically every day. The following chart illustrates the profit situation we tested, and users can verify the relevant content of their encrypted wallets daily.

At this point, those familiar with smart contract development might wonder: with such small daily profits, is it even enough to cover the gas fees?

Indeed, if Lido were to distribute profits using the simplest method, it would likely struggle to cover the cost of gas. Sending tokens to such a large number of addresses would require significant gas fees, which may not be feasible.

However, Lido has managed to achieve automatic profit growth in stETH holdings within wallets without any apparent transactions. How is this possible?

We found Lido’s contract at https://etherscan.io/token/0xae7ab96520de3a18e5e111b5eaab095312d7fe84 and traced the balanceOf method in the contract.

The balanceOf method is compliant with the ERC20 standard and is used by wallets to retrieve the amount of tokens owned by a user.

In the stETH contract, we see that it calls the getPooledEthByShares method. This method takes the mapping (address => uint256) private shares; as input. Does this represent how much stETH a user has? Clearly not, as updating the data for each address every day would be impractical. While updating shares in a single transaction could achieve this, the gas consumption would still be substantial.

By now, many of you may have guessed how the contract works. Let’s continue examining the getPooledEthByShares method.

It can be observed that the final result is obtained by multiplying the sharesAmount in the address by _getTotalPooledEther() and dividing by _getTotalShares.

_getTotalPooledEther represents the total amount of stETH (which, if stETH is pegged to ETH at a 1:1 ratio, also represents the total amount of ETH), while _getTotalShares represents the total number of shares. With this calculation, the amount of stETH owned by each address is dynamically determined.

For example, if there are a total of 1000 shares, with Address A owning 100 shares (as represented by sharesAmount), and the total stETH is 1000 (as returned by _getTotalPooledEther), then according to this calculation, Address A would own 100 stETH.

Now, if Lido stakes the total of 1000 ETH and earns 1 ETH in profit, updating _getTotalPooledEther to 1001, which means the total stETH increases from 1000 to 1001. Therefore, the new calculation for Address A would be 100 * 1001 / 1000 = 100.1 stETH.

In simpler terms, while the shares held by each address remain constant, the stETH corresponding to those shares increases. Hence, when recalculated, the stETH holdings naturally increase.

Continuing with the code, the logic within _getTotalPooledEther is influenced by the handleOracleReport method, which updates relevant data within the contract. This method is periodically called, and the specific invocation occurs through the contract at https://etherscan.io/address/0x852deD011285fe67063a08005c71a85690503Cee, which periodically calls submitReportData to update the data (submitReportData internally calls handleOracleReport in the Lido contract):

We can see that there are daily calls to update relevant content, which explains why, even though we cannot see any transaction distributing profits to our address, the balance changes every day.

This illustrates a characteristic of Ethereum ERC20 smart contracts: the amount of tokens owned by addresses is not hard coded but returned by contract methods. Therefore, it’s possible for the token balance of an address to change even without any transactions. While this flexibility makes ERC20 contracts more versatile, it can also be confusing for those unfamiliar with contracts. I hope this article helps readers better understand smart contracts and interact with them more safely and intelligently.

Furthermore, while staking ETH to obtain stETH seems to offer stable staking rewards, there are still potential risks involved. This article serves as a technical exploration of staking contracts and should not be construed as investment advice.

Statement:

  1. This article is reproduced from [ZAN Team],Original title “Ingenious contract design, see how stETH automatically distributes income on a daily basis? Let your ETH participate in staking to obtain stable interest”,Copyright belongs to the original author[ZAN Team], if you have any objections to the reprint, please contact the Gate Learn team, and the team will handle it as soon as possible according to relevant procedures.

  2. Disclaimer: The views and opinions expressed in this article represent only the author’s personal views and do not constitute any investment advice.

  3. Other language versions of the article are translated by the Gate Learn team and are not mentioned in Gate.io), the translated article may not be reproduced, distributed or plagiarized.

Interpret the clever design of stETH

Intermediate3/22/2024, 10:07:31 PM
The fundamental logic of stETH is to deposit ETH into Lido. Lido utilizes these ETH to participate in Ethereum's PoS (Proof of Stake) to earn rewards, and users receive corresponding stETH as proof of their participation. Subsequently, Lido distributes the rewards to the addresses holding stETH.

Forwarded Title:Clever contract design: see how stETH automatically distributes profits daily? Let your ETH participate in staking to earn stable interest.

The author converted a small amount of Ether (ETH) into stable Ether (stETH) and found that the amount of stETH increased naturally every day, yielding continuous profits. However, there were no transaction records visible in the account. Why is this? This article will explore the clever design behind it and reveal the secret of profit distribution.

1 stETH has generated some profits after a few days.

Before diving into how stETH earns profits, let’s first introduce the logic behind it, which is Ethereum staking. Readers who are already familiar with this concept can skip ahead.

Initially, Ethereum, like Bitcoin, used Proof of Work (PoW) as its consensus mechanism. However, due to concerns over power consumption, security, and scalability, Ethereum transitioned to Proof of Stake (PoS) in September 2022.

Previously reliant on mining power to achieve consensus, Ethereum now relies on participants staking ETH to gain voting rights and earn rewards through voting. This incentivizes consensus through PoS.

By staking 32 ETH, one can join the Ethereum network as a validator, responsible for storing data, processing transactions, and adding new blocks to the blockchain. Validators receive ETH rewards for correctly packaging transactions into new blocks and verifying other validators’ work, allowing for relatively stable returns through staking.

However, traditional staking is cumbersome for ordinary users, as it requires 32 ETH and a dedicated computer with uninterrupted access to the Ethereum network. Additionally, staking ETH locks up liquidity. Hence, the emergence of Liquid Staking Derivatives (LSD), aimed at addressing the barriers and liquidity issues of traditional staking. LSD allows users to stake less than 32 ETH and delegate their ETH to third-party staking providers, receiving corresponding staking tokens (such as Lido’s stETH or Rocket Pool’s rETH). These liquid tokens can be traded, borrowed, or used for other financial activities on various platforms, enabling users to participate in staking more conveniently while maintaining liquidity.

So, the fundamental logic behind stETH is that users provide their ETH to Lido, which then uses these ETH to participate in Ethereum’s PoS to generate profits. In return, users receive stETH as proof of their stake. Next, Lido distributes the profits to the addresses holding stETH.

We can observe that the profits of stETH are updated automatically every day. The following chart illustrates the profit situation we tested, and users can verify the relevant content of their encrypted wallets daily.

At this point, those familiar with smart contract development might wonder: with such small daily profits, is it even enough to cover the gas fees?

Indeed, if Lido were to distribute profits using the simplest method, it would likely struggle to cover the cost of gas. Sending tokens to such a large number of addresses would require significant gas fees, which may not be feasible.

However, Lido has managed to achieve automatic profit growth in stETH holdings within wallets without any apparent transactions. How is this possible?

We found Lido’s contract at https://etherscan.io/token/0xae7ab96520de3a18e5e111b5eaab095312d7fe84 and traced the balanceOf method in the contract.

The balanceOf method is compliant with the ERC20 standard and is used by wallets to retrieve the amount of tokens owned by a user.

In the stETH contract, we see that it calls the getPooledEthByShares method. This method takes the mapping (address => uint256) private shares; as input. Does this represent how much stETH a user has? Clearly not, as updating the data for each address every day would be impractical. While updating shares in a single transaction could achieve this, the gas consumption would still be substantial.

By now, many of you may have guessed how the contract works. Let’s continue examining the getPooledEthByShares method.

It can be observed that the final result is obtained by multiplying the sharesAmount in the address by _getTotalPooledEther() and dividing by _getTotalShares.

_getTotalPooledEther represents the total amount of stETH (which, if stETH is pegged to ETH at a 1:1 ratio, also represents the total amount of ETH), while _getTotalShares represents the total number of shares. With this calculation, the amount of stETH owned by each address is dynamically determined.

For example, if there are a total of 1000 shares, with Address A owning 100 shares (as represented by sharesAmount), and the total stETH is 1000 (as returned by _getTotalPooledEther), then according to this calculation, Address A would own 100 stETH.

Now, if Lido stakes the total of 1000 ETH and earns 1 ETH in profit, updating _getTotalPooledEther to 1001, which means the total stETH increases from 1000 to 1001. Therefore, the new calculation for Address A would be 100 * 1001 / 1000 = 100.1 stETH.

In simpler terms, while the shares held by each address remain constant, the stETH corresponding to those shares increases. Hence, when recalculated, the stETH holdings naturally increase.

Continuing with the code, the logic within _getTotalPooledEther is influenced by the handleOracleReport method, which updates relevant data within the contract. This method is periodically called, and the specific invocation occurs through the contract at https://etherscan.io/address/0x852deD011285fe67063a08005c71a85690503Cee, which periodically calls submitReportData to update the data (submitReportData internally calls handleOracleReport in the Lido contract):

We can see that there are daily calls to update relevant content, which explains why, even though we cannot see any transaction distributing profits to our address, the balance changes every day.

This illustrates a characteristic of Ethereum ERC20 smart contracts: the amount of tokens owned by addresses is not hard coded but returned by contract methods. Therefore, it’s possible for the token balance of an address to change even without any transactions. While this flexibility makes ERC20 contracts more versatile, it can also be confusing for those unfamiliar with contracts. I hope this article helps readers better understand smart contracts and interact with them more safely and intelligently.

Furthermore, while staking ETH to obtain stETH seems to offer stable staking rewards, there are still potential risks involved. This article serves as a technical exploration of staking contracts and should not be construed as investment advice.

Statement:

  1. This article is reproduced from [ZAN Team],Original title “Ingenious contract design, see how stETH automatically distributes income on a daily basis? Let your ETH participate in staking to obtain stable interest”,Copyright belongs to the original author[ZAN Team], if you have any objections to the reprint, please contact the Gate Learn team, and the team will handle it as soon as possible according to relevant procedures.

  2. Disclaimer: The views and opinions expressed in this article represent only the author’s personal views and do not constitute any investment advice.

  3. Other language versions of the article are translated by the Gate Learn team and are not mentioned in Gate.io), the translated article may not be reproduced, distributed or plagiarized.

Start Now
Sign up and get a
$100
Voucher!