Michael.W基于Foundry精读Openzeppelin

2024年08月13日更新 117 人订阅
专栏简介 Michael.W基于Foundry精读Openzeppelin第8期——Context.sol Michael.W基于Foundry精读Openzeppelin第1期——Address.sol Michael.W基于Foundry精读Openzeppelin第2期——StorageSlot.sol Michael.W基于Foundry精读Openzeppelin第3期——Arrays.sol Michael.W基于Foundry精读Openzeppelin第4期——Base64.sol Michael.W基于Foundry精读Openzeppelin第5期——Counters.sol Michael.W基于Foundry精读Openzeppelin第6期——Strings.sol Michael.W基于Foundry精读Openzeppelin第7期——Timers.sol Michael.W基于Foundry精读Openzeppelin第9期——Multicall.sol Michael.W基于Foundry精读Openzeppelin第10期——Create2.sol Michael.W基于Foundry精读Openzeppelin第11期——Math.sol Michael.W基于Foundry精读Openzeppelin第12期——SafeCast.sol Michael.W基于Foundry精读Openzeppelin第13期——Checkpoints.sol Michael.W基于Foundry精读Openzeppelin第14期——SafeMath.sol Michael.W基于Foundry精读Openzeppelin第15期——SignedMath.sol Michael.W基于Foundry精读Openzeppelin第16期——SignedSafeMath.sol Michael.W基于Foundry精读Openzeppelin第17期——BitMaps.sol Michael.W基于Foundry精读Openzeppelin第18期——DoubleEndedQueue.sol Michael.W基于Foundry精读Openzeppelin第19期——EnumerableSet.sol Michael.W基于Foundry精读Openzeppelin第20期——EnumerableMap.sol Michael.W基于Foundry精读Openzeppelin第21期——ERC165.sol (番外篇)Michael.W基于Foundry精读Openzeppelin第22期——内联汇编staticcall Michael.W基于Foundry精读Openzeppelin第23期——ERC165Checker.sol Michael.W基于Foundry精读Openzeppelin第24期——ERC165Storage.sol Michael.W基于Foundry精读Openzeppelin第25期——IERC1820Registry.sol Michael.W基于Foundry精读Openzeppelin第26期——ERC1820Implementer.sol Michael.W基于Foundry精读Openzeppelin第27期——Escrow.sol Michael.W基于Foundry精读Openzeppelin第28期——ConditionalEscrow.sol Michael.W基于Foundry精读Openzeppelin第29期——RefundEscrow.sol Michael.W基于Foundry精读Openzeppelin第30期——ECDSA.sol Michael.W基于Foundry精读Openzeppelin第31期——IERC1271.sol Michael.W基于Foundry精读Openzeppelin第32期——SignatureChecker.sol Michael.W基于Foundry精读Openzeppelin第33期——EIP712.sol Michael.W基于Foundry精读Openzeppelin第34期——MerkleProof.sol Michael.W基于Foundry精读Openzeppelin第35期——Ownable.sol Michael.W基于Foundry精读Openzeppelin第36期——Ownable2Step.sol Michael.W基于Foundry精读Openzeppelin第37期——AccessControl.sol Michael.W基于Foundry精读Openzeppelin第38期——AccessControlEnumerable.sol Michael.W基于Foundry精读Openzeppelin第39期——ERC20.sol Michael.W基于Foundry精读Openzeppelin第40期——ERC20Burnable.sol Michael.W基于Foundry精读Openzeppelin第41期——ERC20Capped.sol Michael.W基于Foundry精读Openzeppelin第42期——draft-ERC20Permit.sol Michael.W基于Foundry精读Openzeppelin第43期——Pausable.sol Michael.W基于Foundry精读Openzeppelin第44期——ERC20Pausable.sol Michael.W基于Foundry精读Openzeppelin第45期——ERC20FlashMint.sol Michael.W基于Foundry精读Openzeppelin第46期——ERC20Snapshot.sol Michael.W基于Foundry精读Openzeppelin第47期——SafeERC20.sol Michael.W基于Foundry精读Openzeppelin第48期——TokenTimelock.sol Michael.W基于Foundry精读Openzeppelin第49期——ERC20Wrapper.sol Michael.W基于Foundry精读Openzeppelin第50期——ERC20Votes.sol Michael.W基于Foundry精读Openzeppelin第51期——ERC20VotesComp.sol Michael.W基于Foundry精读Openzeppelin第52期——ERC4626.sol Michael.W基于Foundry精读Openzeppelin第53期——ERC20PresetFixedSupply.sol Michael.W基于Foundry精读Openzeppelin第54期——ERC20PresetMinterPauser.sol Michael.W基于Foundry精读Openzeppelin第55期——PaymentSplitter.sol Michael.W基于Foundry精读Openzeppelin第56期——VestingWallet.sol Michael.W基于Foundry精读Openzeppelin第57期——ReentrancyGuard.sol Michael.W基于Foundry精读Openzeppelin第58期——PullPayment.sol Michael.W基于Foundry精读Openzeppelin第59期——Proxy.sol Michael.W基于Foundry精读Openzeppelin第60期——Clones.sol Michael.W基于Foundry精读Openzeppelin第61期——ERC1967Upgrade.sol Michael.W基于Foundry精读Openzeppelin第62期——ERC1967Proxy.sol Michael.W基于Foundry精读Openzeppelin第63期——Initializable.sol Michael.W基于Foundry精读Openzeppelin第64期——UUPSUpgradeable.sol Michael.W基于Foundry精读Openzeppelin第65期——TransparentUpgradeableProxy.sol Michael.W基于Foundry精读Openzeppelin第66期——ProxyAdmin.sol Michael.W基于Foundry精读Openzeppelin第67期——BeaconProxy.sol Michael.W基于Foundry精读Openzeppelin第68期——UpgradeableBeacon.sol

Michael.W基于Foundry精读Openzeppelin第51期——ERC20VotesComp.sol

  • Michael.W
  • 发布于 2024-03-19 15:46
  • 阅读 1934

ERC20VotesComp库是专门适配Compound投票和委托的ERC20拓展库。本库可对接Compound的接口,同时也存在缺点(总发行量上限为2^96 - 1)。如果业务上确定需要兼容Compound,那必须弄清楚总发行量上限是否符合业务需求。否则可使用ERC20Votes库。

0. 版本

[openzeppelin]:v4.8.3,[forge-std]:v1.5.6

0.1 ERC20VotesComp.sol

Github: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v4.8.3/contracts/token/ERC20/extensions/ERC20VotesComp.sol

ERC20VotesComp库是专门适配Compound投票和委托的ERC20拓展库。本库可对接Compound的接口,同时也存在缺点(总发行量上限为2^96 - 1)。如果业务上确定需要兼容Compound(如:要在Compound Governor Alpha或Bravo中使用该ERC20),那必须弄清楚总发行量上限是否符合业务需求。否则可使用ERC20Votes库。

注:ERC20Votes库详解见:https://learnblockchain.cn/article/7616

1. 目标合约

继承ERC20VotesComp合约:

Github: https://github.com/RevelationOfTuring/foundry-openzeppelin-contracts/blob/master/src/token/ERC20/extensions/MockERC20VotesComp.sol

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

import "openzeppelin-contracts/contracts/token/ERC20/extensions/ERC20VotesComp.sol";

contract MockERC20VotesComp is ERC20VotesComp {
    constructor(
        string memory name,
        string memory symbol
    )
    ERC20Permit(name)
    ERC20(name, symbol)
    {}

    function maxSupply() external view returns (uint224){
        return _maxSupply();
    }

    function mint(address account, uint amount) external {
        _mint(account, amount);
    }
}

全部foundry测试合约:

Github: https://github.com/RevelationOfTuring/foundry-openzeppelin-contracts/blob/master/test/token/ERC20/extensions/ERC20VotesComp.t.sol

2. 代码精读

2.1 _maxSupply() internal

获取本token的最大供应量。

需要注意的是:ERC20VotesComp库支持的最大供应量为2^96-1,而ERC20Votes库为2^224-1。

    function _maxSupply() internal view virtual override returns (uint224) {
        // 返回2^96-1
        return type(uint96).max;
    }

foundry代码验证:

contract ERC20VotesTest is Test {
    MockERC20VotesComp private _testing = new MockERC20VotesComp("test name", "test symbol");

    function test_MaxSupply() external {
        assertEq(_testing.maxSupply(), type(uint96).max);
    }
}

2.2 getCurrentVotes(address account)

获取地址account当前得到的总票数。本方法是对ERC20Votes.getVotes()的封装,返回值类型为uint96。

    function getCurrentVotes(address account) external view virtual returns (uint96) {
        // 调用ERC20Votes.getVotes()并将返回值从uint256安全转成uint96(如果转换过程发生溢出会revert)
        return SafeCast.toUint96(getVotes(account));
    }

foundry代码验证:

contract ERC20VotesTest is Test {
    MockERC20VotesComp private _testing = new MockERC20VotesComp("test name", "test symbol");
    address private user1 = address(1);

    function test_GetCurrentVotes() external {
        _testing.mint(address(this), 100);
        assertEq(_testing.getCurrentVotes(user1), 0);
        _testing.delegate(user1);
        assertEq(_testing.getCurrentVotes(user1), 100);

        _testing.transfer(user1, 1);
        assertEq(_testing.getCurrentVotes(user1), 100 - 1);
        _testing.transfer(user1, 2);
        assertEq(_testing.getCurrentVotes(user1), 100 - 1 - 2);
        _testing.transfer(user1, 3);
        assertEq(_testing.getCurrentVotes(user1), 100 - 1 - 2 - 3);
        _testing.transfer(user1, 4);
        assertEq(_testing.getCurrentVotes(user1), 100 - 1 - 2 - 3 - 4);
        _testing.transfer(user1, 5);
        assertEq(_testing.getCurrentVotes(user1), 100 - 1 - 2 - 3 - 4 - 5);
    }
}

2.3 getPriorVotes(address account, uint256 blockNumber)

获取地址account在blockNumber区块高度时得到的总票数。本方法是对ERC20Votes.getPastVotes()的封装,返回值类型为uint96。

    function getPriorVotes(address account, uint256 blockNumber) external view virtual returns (uint96) {
        // 调用ERC20Votes.getPastVotes()并将返回值从uint256安全转成uint96(如果转换过程发生溢出会revert)
        return SafeCast.toUint96(getPastVotes(account, blockNumber));
    }

foundry代码验证:

contract ERC20VotesTest is Test {
    MockERC20VotesComp private _testing = new MockERC20VotesComp("test name", "test symbol");
    address private user1 = address(1);

    function test_GetPriorVotes() external {
        // 6 Checkpoints of user1:
        //       block             votes             index
        //          2                10                0
        //          3                15                1
        //          6                19                2
        //          10               20                3
        //          11               23                4
        //          13               31                5

        _testing.delegate(user1);
        vm.roll(2);
        _testing.mint(address(this), 10);
        vm.roll(3);
        _testing.mint(address(this), 15 - 10);
        vm.roll(6);
        _testing.mint(address(this), 19 - 15);
        vm.roll(10);
        _testing.mint(address(this), 20 - 19);
        vm.roll(11);
        _testing.mint(address(this), 23 - 20);
        vm.roll(13);
        _testing.mint(address(this), 31 - 23);
        vm.roll(20);

        assertEq(_testing.getPriorVotes(user1, 1), 0);
        assertEq(_testing.getPriorVotes(user1, 2), 10);
        assertEq(_testing.getPriorVotes(user1, 3), 15);
        assertEq(_testing.getPriorVotes(user1, 5), 15);
        assertEq(_testing.getPriorVotes(user1, 6), 19);
        assertEq(_testing.getPriorVotes(user1, 7), 19);
        assertEq(_testing.getPriorVotes(user1, 9), 19);
        assertEq(_testing.getPriorVotes(user1, 10), 20);
        assertEq(_testing.getPriorVotes(user1, 11), 23);
        assertEq(_testing.getPriorVotes(user1, 12), 23);
        assertEq(_testing.getPriorVotes(user1, 13), 31);
        assertEq(_testing.getPriorVotes(user1, 19), 31);

        // revert if block not mined
        vm.expectRevert("ERC20Votes: block not yet mined");
        _testing.getPriorVotes(user1, 20);
    }
}

ps: 本人热爱图灵,热爱中本聪,热爱V神。 以下是我个人的公众号,如果有技术问题可以关注我的公众号来跟我交流。 同时我也会在这个公众号上每周更新我的原创文章,喜欢的小伙伴或者老伙计可以支持一下! 如果需要转发,麻烦注明作者。十分感谢!

1.jpeg

公众号名称:后现代泼痞浪漫主义奠基人

点赞 1
收藏 0
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

0 条评论

请先 登录 后评论