Michael.W基于Foundry精读Openzeppelin第54期——ERC20PresetMinterPauser.sol

  • Michael.W
  • 更新于 2024-04-01 17:13
  • 阅读 1166

ERC20PresetMinterPauser库是一种带mint和pause权限管理的ERC20实现,同时支持销毁和委托销毁的功能。当合约进入paused状态后,全部token的转移功能都将被停止。权限管理功能是直接继承AccessControlEnumerable库。

Michael.W基于Foundry精读Openzeppelin第54期——ERC20PresetMinterPauser.sol

0. 版本

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

0.1 ERC20PresetMinterPauser.sol

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

ERC20PresetMinterPauser库是一种带mint和pause权限管理的ERC20实现,同时支持销毁和委托销毁的功能。当合约进入paused状态后,全部token的转移功能都将被停止。权限管理功能是直接继承AccessControlEnumerable库,该库详解可参见:https://learnblockchain.cn/article/6918

注:目前ERC20PresetMinterPauser库已弃用,推荐使用:https://wizard.openzeppelin.com

1. 目标合约

ERC20PresetMinterPauser合约可直接部署。

全部foundry测试合约:

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

2. 代码精读

2.1 constructor()

    // mint权限的角色识别符
    bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
    // pause权限的角色识别符
    bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE");

    constructor(string memory name, string memory symbol) ERC20(name, symbol) {
        // 为部署者设置默认admin权限
        _setupRole(DEFAULT_ADMIN_ROLE, _msgSender());
        // 为部署者设置mint权限
        _setupRole(MINTER_ROLE, _msgSender());
        // 为部署者设置pause权限
        _setupRole(PAUSER_ROLE, _msgSender());
    }

foundry代码验证:

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

    function test_Constructor() external {
        address deployer = address(this);

        // check the auth grant
        assertTrue(_testing.hasRole(_testing.DEFAULT_ADMIN_ROLE(), deployer));
        assertTrue(_testing.hasRole(_testing.MINTER_ROLE(), deployer));
        assertTrue(_testing.hasRole(_testing.PAUSER_ROLE(), deployer));
    }
}

2.2 mint() && _beforeTokenTransfer(address from, address to, uint256 amount) internal

  • mint():具有mint权限的地址为to地址增发amount数量的token;
  • _beforeTokenTransfer(address from, address to, uint256 amount) internal:重写ERC20和ERC20Pausable的{_beforeTokenTransfer}。
    function mint(address to, uint256 amount) public virtual {
        // 检查调用者拥有mint权限
        require(hasRole(MINTER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have minter role to mint");
        // 调用ERC20._mint(),为to地址增发amount数量的token
        _mint(to, amount);
    }

    function _beforeTokenTransfer(
        address from,
        address to,
        uint256 amount
    ) internal virtual override(ERC20, ERC20Pausable) {
        // 调用ERC20Pausable._beforeTokenTransfer()
        super._beforeTokenTransfer(from, to, amount);
    }

foundry代码验证:

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

    function test_Mint() external {
        address user = address(1);
        assertEq(_testing.balanceOf(user), 0);
        // pass mint
        _testing.mint(user, 100);
        assertEq(_testing.balanceOf(user), 100);

        // revert if the caller has no auth of MINTER_ROLE
        assertFalse(_testing.hasRole(_testing.MINTER_ROLE(), user));
        vm.prank(user);
        vm.expectRevert("ERC20PresetMinterPauser: must have minter role to mint");
        _testing.mint(user, 100);
    }
}

2.3 pause() && unpause()

  • pause():具有pause权限的地址暂停本token的全部转移行为;
  • unpause():具有pause权限的地址恢复本token的全部转移行为。
    function pause() public virtual {
        // 检查调用者拥有pause权限
        require(hasRole(PAUSER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have pauser role to pause");
        // 调用Pausable._pause(),启动本合约的暂停状态
        _pause();
    }

    function unpause() public virtual {
        // 检查调用者拥有pause权限
        require(hasRole(PAUSER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have pauser role to unpause");
        // 调用Pausable._unpause(),恢复本合约的暂停状态
        _unpause();
    }

foundry代码验证:

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

    function test_PauseAndUnpause() external {
        address user = address(1);

        // test for {pause}
        assertFalse(_testing.paused());
        _testing.pause();
        assertTrue(_testing.paused());

        // revert if caller has no auth of PAUSER_ROLE
        assertFalse(_testing.hasRole(_testing.PAUSER_ROLE(), user));
        vm.prank(user);
        vm.expectRevert("ERC20PresetMinterPauser: must have pauser role to pause");
        _testing.pause();

        // revert with {mint}
        vm.expectRevert("ERC20Pausable: token transfer while paused");
        _testing.mint(user, 1);
        // revert with {transfer}
        vm.expectRevert("ERC20Pausable: token transfer while paused");
        _testing.transfer(user, 1);
        // revert with {transferFrom}
        _testing.approve(user, 1024);
        vm.expectRevert("ERC20Pausable: token transfer while paused");
        vm.prank(user);
        _testing.transferFrom(address(this), user, 1024);
        // revert with {burn}
        vm.expectRevert("ERC20Pausable: token transfer while paused");
        _testing.burn(1);
        // revert with {burnFrom}
        vm.expectRevert("ERC20Pausable: token transfer while paused");
        vm.prank(user);
        _testing.burnFrom(address(this), 1024);

        // test for {unpause}
        _testing.unpause();
        assertFalse(_testing.paused());

        // revert if caller has no auth of PAUSER_ROLE
        vm.prank(user);
        vm.expectRevert("ERC20PresetMinterPauser: must have pauser role to unpause");
        _testing.unpause();

        // available on {mint}
        assertEq(_testing.balanceOf(user), 0);
        _testing.mint(user, 100);
        assertEq(_testing.balanceOf(user), 100);
        // available on {transfer}
        assertEq(_testing.balanceOf(address(this)), 0);
        vm.prank(user);
        _testing.transfer(address(this), 1);
        assertEq(_testing.balanceOf(address(this)), 1);
        // available on {transferFrom}
        vm.prank(user);
        _testing.approve(address(this), 100);
        _testing.transferFrom(user, address(this), 1);
        assertEq(_testing.balanceOf(address(this)), 1 + 1);
        // available on {burn}
        _testing.burn(1);
        assertEq(_testing.balanceOf(address(this)), 2 - 1);
        // available on {burnFrom}
        _testing.approve(user, 1);
        vm.prank(user);
        _testing.burnFrom(address(this), 1);
        assertEq(_testing.balanceOf(address(this)), 1 - 1);
    }
}

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

1.jpeg

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

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

0 条评论

请先 登录 后评论
Michael.W
Michael.W
0x93E7...0000
狂热的区块链爱好者