Michael.W基于Foundry精读Openzeppelin第28期——ConditionalEscrow.sol

  • Michael.W
  • 更新于 2023-08-16 20:33
  • 阅读 1559

ConditionalEscrow合约继承了Escrow合约,是其的一种功能拓展。ConditionalEscrow的框架中提供设置可withdraw条件,并且只有在满足该条件时才允许owner为对应payee提取eth。

0. 版本

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

0.1 ConditionalEscrow.sol

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

ConditionalEscrow合约继承了Escrow合约,是其的一种功能拓展。ConditionalEscrow的框架中提供设置可withdraw条件,并且只有在满足该条件时才允许owner为对应payee提取eth。

1. 目标合约

ConditionalEscrow是abstract合约,实现其中的virtual方法使其成为一个可部署调用合约:

Github: https://github.com/RevelationOfTuring/foundry-openzeppelin-contracts/blob/master/src/utils/escrow/MockConditionalEscrow.sol

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

import "openzeppelin-contracts/contracts/utils/escrow/ConditionalEscrow.sol";

contract MockConditionalEscrow is ConditionalEscrow {
    // 记录最近一次owner为payee deposit时的区块高度
    mapping(address => uint) _latestDepositBlockNumber;

    // 重写Escrow.deposit(),使得每次owner调用deposit时记录当前的区块高度
    function deposit(address payee) public payable override {
        // 记录owner本次为payee deposit时的区块高度
        _latestDepositBlockNumber[payee] = block.number;
        // 调用Escrow.deposit()
        super.deposit(payee);
    }

    // 实现ConditionalEscrow.withdrawalAllowed(),设置允许withdraw的条件:当前区块高度 >= 最近一次owner为payee deposit时的区块高度 1000个区块
    function withdrawalAllowed(address payee) public view override returns (bool){
        // 当前区块高度 >= 最近一次owner为该payee deposit时的区块高度 1000个区块时,返回true。否则返回false
        return block.number - _latestDepositBlockNumber[payee] >= 1000;
    }
}

全部foundry测试合约:

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

2. 代码精读

2.1 withdrawalAllowed(address payee) virtual

允许withdraw的条件。virtual函数,需要开发者在子合约中自行实现逻辑。

    function withdrawalAllowed(address payee) public view virtual returns (bool);

foundry代码验证

contract ConditionalEscrowTest is Test {
    MockConditionalEscrow mce = new MockConditionalEscrow();
    address payable payee = payable(address(1024));
    address other = address(2048);

    function setUp() external {
        vm.deal(address(this), 10 ether);
        vm.deal(other, 10 ether);
    }

    function test_Deposit() external {
        mce.deposit{value : 1 ether}(payee);
        assertEq(1 ether, mce.depositsOf(payee));
        // check balance
        assertEq(1 ether, address(mce).balance);

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

    function test_WithdrawalAllowed() external {
        // not deposit
        assertFalse(mce.withdrawalAllowed(payee));

        // deposit at block number 1024
        vm.roll(1024);
        mce.deposit{value : 1 ether}(payee);

        // return false when query on the block number < 1024+1000
        assertFalse(mce.withdrawalAllowed(payee));
        vm.roll(1024 + 1);
        assertFalse(mce.withdrawalAllowed(payee));
        vm.roll(1024 + 999);
        assertFalse(mce.withdrawalAllowed(payee));
        vm.roll(1024 + 1000);
        assertTrue(mce.withdrawalAllowed(payee));
    }
}

2.2 withdraw(address payable payee)

在满足可withdraw的条件后,owner调用该函数为对应payee提取eth。

    function withdraw(address payable payee) public virtual override {
        // 要求满足可withdraw条件,否则revert
        require(withdrawalAllowed(payee), "ConditionalEscrow: payee is not allowed to withdraw");
        // 调用Escrow.withdraw(),即owner为payee提取eth
        super.withdraw(payee);
    }

foundry代码验证

contract ConditionalEscrowTest is Test {
    MockConditionalEscrow mce = new MockConditionalEscrow();
    address payable payee = payable(address(1024));
    address other = address(2048);

    function setUp() external {
        vm.deal(address(this), 10 ether);
        vm.deal(other, 10 ether);
    }

    function test_Withdraw() external {
        // deposit at block number 1024
        vm.roll(1024);
        mce.deposit{value : 1 ether}(payee);

        // owner can't withdraw to 'payee' before the block number 1024+1000
        vm.expectRevert("ConditionalEscrow: payee is not allowed to withdraw");
        mce.withdraw(payee);

        vm.roll(1024 + 1);
        vm.expectRevert("ConditionalEscrow: payee is not allowed to withdraw");
        mce.withdraw(payee);

        vm.roll(1024 + 999);
        vm.expectRevert("ConditionalEscrow: payee is not allowed to withdraw");
        mce.withdraw(payee);

        // owner withdraw to 'payee'
        vm.roll(1024 + 1000);
        assertEq(0, payee.balance);
        mce.withdraw(payee);
        // check balance of payee
        assertEq(1 ether, payee.balance);
        assertEq(0, address(mce).balance);

        // revert if not owner
        vm.prank(other);
        vm.expectRevert("Ownable: caller is not the owner");
        mce.withdraw(payee);
    }
}

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

1.jpeg

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

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

0 条评论

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