Reprinted from personal blog https://www.hackdefi.xyz/posts/erc20-rebase-3/
Practical Example#
Let's expand with a practical example. Suppose both A and B have rebase enabled, with initial balances and shares of 100, while C has rebase disabled with a balance of 200.
The current state is as follows, which satisfies the equation condition:
- totalSupply = 100 + 100 + 200 = 400
- sharePrice = 1
- rebasingSupply = (100 + 100) * 1 = 200
- nonRebasingSupply = 200
Now C calls distribute to contribute their 200 tokens, resulting in the following state change:
- totalSupply = 100 + 100 - 200 = 0
- sharePrice = 1 + (200/200) = 2
- rebasingSupply = (100 + 100) * 2 = 400
- nonRebasingSupply = 0
At this point, the equation is clearly no longer valid. To ensure the equation holds, totalSupply should be 400 instead of 0. Returning to the first version, each time we call distribute, we modify the balance of each user participating in rebase through _mint. In fact, the system has issued new tokens because a burn operation was performed earlier, thus maintaining balance.
In the second version, while a burn was also performed earlier, no new tokens were issued; only the sharePrice was updated. Therefore, even though the user's balance on paper increased, the actual total available for withdrawal is insufficient. Thus, the missing 400 is the amount that needs to be minted. Similarly, we do not need to proportionally mint for each user, but rather record it in a global variable, and mint the corresponding amount when the user exits rebase.
Compensation Minting#
First, define a global unminted variable:
Unminted needs to be increased during distribute and decreased during exit:
The modified code is as follows. We have added the unminted variable and accumulate it during distribute, minting the corresponding number of tokens to the user during exit.
Thus, the core framework of ERC20Rebase has been implemented. However, the code is for reference only and focuses solely on the core logic. Compared to the ERC20RebaseDistributor contract in ECG, we still lack a very critical part: linear release. The dividend tokens are not returned to holders in a lump sum but are linearly increased over a certain period.
With the addition of a time dimension, many issues will also arise: if the total shares change during the dividend period, how can we ensure fair distribution?