banner
zach

zach

github
twitter
medium

ERC20 Rebase的設計與實現-3 補償鑄造

轉載於個人博客 https://www.hackdefi.xyz/posts/erc20-rebase-3/

實際例子#

我們以實際例子展開,假設 A、B 均開啟 rebase,且初始 balance 和 share 均為 100,C 未開啟 rebase,balance 為 200

此時的狀態如下,可見是滿足等式條件的:

  • totalSupply = 100 + 100 + 200 = 400
  • sharePrice = 1
  • rebasingSupply = (100 + 100) * 1 = 200
  • nonRebasingSupply = 200

那麼 C 調用 distribute 來貢獻自己的 200 token,狀態變化為:

  • totalSupply = 100 + 100 - 200 = 0
  • sharePrice = 1 + (200/200) = 2
  • rebasingSupply = (100 + 100) * 2 = 400
  • nonRebasingSupply = 0

此時顯然等式不再成立,為了保證等式成立,totalSupply 應該是 400 而不是 0,回到第一個版本,我們每次調用 distribute 時都會通過_mint 來修改每個參與 rebase 用戶的 balance,實際上系統是增發了,因為前面進行了銷毀操作,因此保持了平衡

那麼在第二版中,前面也進行了銷毀,但是卻沒有增發,只是更新 sharePrice,那麼就算用戶帳面上的 balance 增加了,但是實際來領取時,系統總量是不夠的。因此,這裡差的 400 就是需要增發的量,同理我們不需要按比例增發給每個用戶,而是記錄在一個全局變量中,當用戶退出 rebase 時再 mint 出相應的數額

補償鑄造#

首先定義全局的 unminted 變量:

uint256 public unminted;

unminted 需要在 distribute 時增加,在 exit 時減少:

    function _exitRebase(address user)  internal {
        uint256 shares = rebasingAccount[user].nShares;
        rebasingAccount[user].isRebasing = false;
        rebasingAccount[user].nShares = 0;
        totalShares -= shares;
        uint256 balance = share2Balance(shares);
        uint256 rawBalance = ERC20.balanceOf(user);
        if (balance > rawBalance) {
            uint256 delta = balance - rawBalance;
            ERC20._mint(user, delta);
            unminted -= delta;
        }
        emit RebaseExit(user, shares, block.timestamp);
    }

    function distribute(uint256 amount) external {
        require(balanceOf(msg.sender)>=amount, "SimpleERC20Rebase: not enough");
        _burn(msg.sender, amount);
        sharePrice += amount*1e30 / totalShares;
        unminted += amount;
    }

修改後的代碼如下,我們新增了 unminted 變量,並在 distribute 時累加,在 exit 時增發相應數目 token 給用戶

至此,ERC20Rebase 的核心框架已經實現了,不過代碼僅供參考,只關注了核心邏輯的實現,對比 ecg 中的 ERC20RebaseDistributor 合約,我們仍欠缺很關鍵的部分:線性釋放,分紅的 token 不是一次性反饋給 holder,而是在一定的周期內線性增加。

由於添加了時間的維度,相應的也會衍生出許多問題:如果分紅周期內 share 總量發生變化,如何保證公平分發?

Code#

// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.8.13;

import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract SimpleERC20Rebase is ERC20 {
    event RebaseEnter(address indexed account, uint256 indexed shares, uint256 indexed timestamp);
    event RebaseExit(address indexed account, uint256 indexed shares, uint256 indexed timestamp);
    
    struct RebasingState {
        bool isRebasing;
        uint256 nShares;
    }
    mapping(address => RebasingState) internal rebasingAccount;
    uint256 public totalShares;
    uint256 public unminted;
    uint256 public sharePrice = 1e30;

    constructor(
        string memory _name,
        string memory _symbol
    ) ERC20(_name, _symbol) {}

    function rebasingSupply() public view returns (uint256) {
        return share2Balance(totalShares);
    }

    function nonRebasingSupply() public view returns (uint256) {
        return totalSupply() - rebasingSupply();
    }

    function share2Balance(uint256 shares) view public returns (uint256) {
        return shares * sharePrice / 1e30;
    }

    function balance2Share(uint256 balance) view public returns (uint256) {
        return balance * 1e30 / sharePrice ;
    }

    function enterRebase() external {
        require(!rebasingAccount[msg.sender].isRebasing, "SimpleERC20Rebase: already rebasing");
        _enterRebase(msg.sender);
    }

    function _enterRebase(address user) internal {
        uint256 balance = balanceOf(user);
        uint256 shares = balance2Share(balance);
        rebasingAccount[user].isRebasing = true;
        rebasingAccount[user].nShares = shares;
        totalShares += shares;
        emit RebaseEnter(user, shares, block.timestamp);
    }

    function exitRebase()  external {
        require(rebasingAccount[msg.sender].isRebasing, "SimpleERC20Rebase: not rebasing");
        _exitRebase(msg.sender);
    }

    function _exitRebase(address user)  internal {
        uint256 shares = rebasingAccount[user].nShares;
        rebasingAccount[user].isRebasing = false;
        rebasingAccount[user].nShares = 0;
        totalShares -= shares;
        uint256 balance = share2Balance(shares);
        uint256 rawBalance = ERC20.balanceOf(user);
        if (balance > rawBalance) {
            uint256 delta = balance - rawBalance;
            ERC20._mint(user, delta);
            unminted -= delta;
        }
        emit RebaseExit(user, shares, block.timestamp);
    }

    function distribute(uint256 amount) external {
        require(balanceOf(msg.sender)>=amount, "SimpleERC20Rebase: not enough");
        _burn(msg.sender, amount);
        sharePrice += amount*1e30 / totalShares;
        unminted += amount;
    }

    function totalSupply() public view override returns (uint256) {
        return super.totalSupply() + unminted;
    }

    function balanceOf(address account) public view override returns (uint256) {
        uint256 rawBalance = ERC20.balanceOf(account);
        if (rebasingAccount[account].isRebasing) {
            return share2Balance(rebasingAccount[account].nShares);
        } else {
            return rawBalance;
        }
    }

    function mint(address user, uint256 amount) external {
        bool isRebasing = rebasingAccount[user].isRebasing;
        if (isRebasing) {
            _exitRebase(user);
        }
        ERC20._mint(user, amount);
        if (isRebasing) {
            _enterRebase(user);
        }
    }

    function transfer(address to, uint256 amount)  public virtual override returns (bool) {
        bool isFromRebasing = rebasingAccount[msg.sender].isRebasing;
        bool isToRebasing = rebasingAccount[to].isRebasing;
        if (isFromRebasing) {
            _exitRebase(msg.sender);
        }
        if (isToRebasing && to != msg.sender) {
            _exitRebase(to);
        }
        bool result = ERC20.transfer(to, amount);
        if (isFromRebasing) {
            _enterRebase(msg.sender);
        }
        if (isToRebasing && to != msg.sender) {
            _enterRebase(to);
        }
        return result;
    }

    function transferFrom(
        address from,
        address to,
        uint256 amount
    ) public virtual override returns (bool) {
        bool isFromRebasing = rebasingAccount[from].isRebasing;
        bool isToRebasing = rebasingAccount[to].isRebasing;
        if (isFromRebasing) {
            _exitRebase(from);
        }
        if (isToRebasing && to != from) {
            _exitRebase(to);
        }
        bool result = ERC20.transfer(to, amount);
        if (isFromRebasing) {
            _enterRebase(from);
        }
        if (isToRebasing && to != from) {
            _enterRebase(to);
        }
        return result;
    }
}
載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。