Michael.W基于Foundry精读Openzeppelin

2024年08月13日更新 118 人订阅
专栏简介 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第29期——RefundEscrow.sol

  • Michael.W
  • 发布于 2023-08-17 23:09
  • 阅读 2034

RefundEscrow合约继承了ConditionalEscrow合约,是ConditionalEscrow合约的一种功能拓展。RefundEscrow合约提供了基础的存取eth功能,同时合约owner可以将合约切换到Refunding或Closed状态。

0. 版本

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

0.1 RefundEscrow.sol

Github: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v4.8.3/contracts/utils/escrow/RefundEscrow.sol

RefundEscrow合约继承了ConditionalEscrow合约,是ConditionalEscrow合约的一种功能拓展。RefundEscrow合约提供了基础的存取eth功能,同时合约owner可以将合约切换到Refunding或Closed状态。Refunding状态下,owner可以将锁存的eth分发给对应refundee;Closed状态下,beneficiary可以提走合约内全部eth。

1. 目标合约

RefundEscrow本身就是一个contract(非abstract contract),可独立部署和调用。

全部foundry测试合约:

Github: https://github.com/RevelationOfTuring/foundry-openzeppelin-contracts/blob/master/test/utils/escrow/RefundEscrow.t.sol

2. 代码精读

2.1 state() && beneficiary()

  • state():查询本合约当前所处的状态;
  • beneficiary():查询受益人地址。
    // 用于合约eth转账
    using Address for address payable;
    // 定义表示合约存取状态的枚举变量
    enum State {
        // Active状态:1.可以进行deposit
        Active,
        // Refunding状态:1.可以withdraw 2.禁止deposit
        Refunding,
        // Closed状态:1.可以将合约内全部eth转移给受益人 2.禁止deposit
        Closed
    }

    event RefundsClosed();
    event RefundsEnabled();

    // 合约当前存取状态(枚举变量)
    State private _state;
    // 受益人地址,即可以提走所有deposit的eth的地址
    address payable private immutable _beneficiary;

    // 部署合约时设置受益人地址
    constructor(address payable beneficiary_) {
        // 检查受益人地址不可为零地址
        require(beneficiary_ != address(0), "RefundEscrow: beneficiary is the zero address");
        // 设置受益人地址
        _beneficiary = beneficiary_;
        // 合约部署成功后的状态为Active
        _state = State.Active;
    }

    function state() public view virtual returns (State) {
        return _state;
    }

    function beneficiary() public view virtual returns (address payable) {
        return _beneficiary;
    }

foundry代码验证

contract RefundEscrowTest is Test {
    RefundEscrow re;
    address payable beneficiary = payable(address(1 << 1));
    address payable payee = payable(address(1 << 2));
    address payable other = payable(address(1 << 3));

    function setUp() external {
        re = new RefundEscrow(beneficiary);
        // change balance of the owner
        vm.deal(address(this), 10 ether);
        // change balance of other
        vm.deal(other, 10 ether);
    }

    function test_Getter() external {
        // initial state
        require(re.state() == RefundEscrow.State.Active);
        assertEq(beneficiary, re.beneficiary());
    }
}

2.2 close() && enableRefunds()

  • close():owner将合约状态从Active改为Closed。注:当合约处于Closed状态时,beneficiary可以提走合约内全部eth。此时,deposit操作被禁止;
  • enableRefunds():owner将合约状态从Active改为Refunding。注:当合约处于Refunding状态时,owner可以为每个refundee提取eth。此时,deposit操作被禁止。
    function close() public virtual onlyOwner {
        // 检查当前合约状态必须为Active
        require(state() == State.Active, "RefundEscrow: can only close while active");
        // 合约状态设置为Closed
        _state = State.Closed;
        // 抛出event
        emit RefundsClosed();
    }

    function enableRefunds() public virtual onlyOwner {
        // 检查当前合约状态必须为Active
        require(state() == State.Active, "RefundEscrow: can only enable refunds while active");
        // 合约状态设置为Refunding
        _state = State.Refunding;
        // 抛出event
        emit RefundsEnabled();
    }

foundry代码验证

contract RefundEscrowTest is Test {
    RefundEscrow re;
    address payable beneficiary = payable(address(1 << 1));
    address payable payee = payable(address(1 << 2));
    address payable other = payable(address(1 << 3));

    // copy events from RefundEscrow.sol to do event assertion
    event RefundsClosed();
    event RefundsEnabled();

    function setUp() external {
        re = new RefundEscrow(beneficiary);
        // change balance of the owner
        vm.deal(address(this), 10 ether);
        // change balance of other
        vm.deal(other, 10 ether);
    }

    function test_StateChange() external {
        // case 1: Active to Closed
        vm.expectEmit(address(re));
        emit RefundsClosed();
        re.close();
        require(re.state() == RefundEscrow.State.Closed);
        // revert if not owner
        vm.prank(other);
        vm.expectRevert("Ownable: caller is not the owner");
        re.close();
        // revert if not Active state before close()
        vm.expectRevert("RefundEscrow: can only close while active");
        re.close();

        // case 2: Active to Refunding
        re = new RefundEscrow(beneficiary);
        vm.expectEmit(address(re));
        emit RefundsEnabled();
        re.enableRefunds();
        require(re.state() == RefundEscrow.State.Refunding);
        // revert if not owner
        vm.prank(other);
        vm.expectRevert("Ownable: caller is not the owner");
        re.enableRefunds();
        // revert if not Active state before enableRefunds()
        vm.expectRevert("RefundEscrow: can only enable refunds while active");
        re.enableRefunds();
    }
}

2.3 deposit(address refundee)

owner为指定的refundee deposit eth。

    function deposit(address refundee) public payable virtual override {
        // 只有合约状态为Active,才可以进行deposit
        require(state() == State.Active, "RefundEscrow: can only deposit while active");
        // 调用Escrow.deposit()
        super.deposit(refundee);
    }

foundry代码验证

contract RefundEscrowTest is Test {
    RefundEscrow re;
    address payable beneficiary = payable(address(1 << 1));
    address payable payee = payable(address(1 << 2));
    address payable other = payable(address(1 << 3));

    function setUp() external {
        re = new RefundEscrow(beneficiary);
        // change balance of the owner
        vm.deal(address(this), 10 ether);
        // change balance of other
        vm.deal(other, 10 ether);
    }

    function test_Deposit() external {
        // revert if not at Active state
        // case 1: at Closed state
        re.close();
        vm.expectRevert("RefundEscrow: can only deposit while active");
        re.deposit{value : 1 ether}(payee);

        // case 2: at Refunding state
        re = new RefundEscrow(beneficiary);
        re.enableRefunds();
        vm.expectRevert("RefundEscrow: can only deposit while active");
        re.deposit{value : 1 ether}(payee);

        // revert if not owner
        re = new RefundEscrow(beneficiary);
        vm.expectRevert("Ownable: caller is not the owner");
        vm.prank(other);
        re.deposit{value : 1 ether}(payee);

        // success at Active state
        re.deposit{value : 1 ether}(payee);
        assertEq(1 ether, address(re).balance);
        assertEq(1 ether, re.depositsOf(payee));

        // deposit again
        re.deposit{value : 2 ether}(payee);
        assertEq(1 ether + 2 ether, address(re).balance);
        assertEq(1 ether + 2 ether, re.depositsOf(payee));

        // deposit to other
        re.deposit{value : 3 ether}(other);
        assertEq(1 ether + 2 ether + 3 ether, address(re).balance);
        assertEq(1 ether + 2 ether, re.depositsOf(payee));
        assertEq(3 ether, re.depositsOf(other));
    }
}

2.4 beneficiaryWithdraw() && withdrawalAllowed(address)

  • beneficiaryWithdraw():将本合约内全部eth转移给受益人。注:任何人都可以触发该操作;
  • withdrawalAllowed(address):实现ConditionalEscrow.withdrawalAllowed(),允许withdraw的条件:合约状态为Refunding。
    function beneficiaryWithdraw() public virtual {
    // 检查当前合约状态必须为Closed
        require(state() == State.Closed, "RefundEscrow: beneficiary can only withdraw while closed");
        // 将本合约内全部eth转移给受益人
        beneficiary().sendValue(address(this).balance);
    }

    function withdrawalAllowed(address) public view override returns (bool) {
        // 允许withdraw的条件:合约状态为Refunding
        return state() == State.Refunding;
    }
}

foundry代码验证

contract RefundEscrowTest is Test {
    RefundEscrow re;
    address payable beneficiary = payable(address(1 << 1));
    address payable payee = payable(address(1 << 2));
    address payable other = payable(address(1 << 3));

    function setUp() external {
        re = new RefundEscrow(beneficiary);
        // change balance of the owner
        vm.deal(address(this), 10 ether);
        // change balance of other
        vm.deal(other, 10 ether);
    }

    function test_WithdrawalAllowed() external {
        // case 1: at Active state
        assertFalse(re.withdrawalAllowed(address(0)));
        // case 2: at Closed state
        re.close();
        assertFalse(re.withdrawalAllowed(address(0)));
        // case 3: at Refunding state
        re = new RefundEscrow(beneficiary);
        re.enableRefunds();
        assertTrue(re.withdrawalAllowed(address(0)));
    }

    function test_BeneficiaryWithdraw() external {
        // case 1: revert at Refunding state
        re.enableRefunds();
        vm.expectRevert("RefundEscrow: beneficiary can only withdraw while closed");
        re.beneficiaryWithdraw();

        // case 2: revert at Active state
        re = new RefundEscrow(beneficiary);
        vm.expectRevert("RefundEscrow: beneficiary can only withdraw while closed");
        re.beneficiaryWithdraw();

        // case 3: success at Closed state
        vm.deal(address(re), 1 ether);
        re.close();
        assertEq(0, beneficiary.balance);
        re.beneficiaryWithdraw();
        assertEq(1 ether, beneficiary.balance);
    }
}

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

1.jpeg

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

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

0 条评论

请先 登录 后评论