Michael.W基于Foundry精读Openzeppelin第27期——Escrow.sol

  • Michael.W
  • 更新于 2023-08-15 21:32
  • 阅读 801

Escrow合约是Openzeppelin中所有escrow拓展库的基础合约,用于为指定地址锁存eth和提取eth的托管场景。该合约中存入和取出eth的方法都被virtual修饰,开发者可以通过继承重写的方式来做相关修改。

0. 版本

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

0.1 Escrow.sol

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

Escrow合约是Openzeppelin中所有escrow拓展库的基础合约,用于为指定地址锁存eth和提取eth的托管场景。该合约中存入和取出eth的方法都被virtual修饰,开发者可以通过继承重写的方式来做相关修改。

1. 目标合约

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

全部foundry测试合约:

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

2. 代码精读

2.1 depositsOf(address payee) && deposit(address payee)

  • depositsOf(address payee):查询输入的payee当前名下的deposit累计值;
  • deposit(address payee):合约owner将eth转入合约作为输入的payee的本次deposit值。合约owner可以多次给相同的payee进行deposit操作,合约内部会进行累积存储。
    // 使用Address库,用于合约向地址转eth
    using Address for address payable;

    event Deposited(address indexed payee, uint256 weiAmount);
    event Withdrawn(address indexed payee, uint256 weiAmount);
    // 用于存储每个payee名下累计deposit值的mapping
    mapping(address => uint256) private _deposits;

    function depositsOf(address payee) public view returns (uint256) {
        // _deposits中key为payee对应的value即是待查询payee名下累计的deposit值
        return _deposits[payee];
    }

    function deposit(address payee) public payable virtual onlyOwner {
        // 本次转入合约的eth值
        uint256 amount = msg.value;
        // 在_deposits中做累加
        _deposits[payee] += amount;
        // 抛出event
        emit Deposited(payee, amount);
    }

2.2 withdraw(address payable payee)

合约owner将合约内payee名下的全部deposit累计值提给payee。

注:该方法内部从合约向payee地址发送eth的过程没有做防重入的限制,可以根据需求添加防重入锁ReentrancyGuard

    function withdraw(address payable payee) public virtual onlyOwner {
        // 获取该payee的deposit累计值
        uint256 payment = _deposits[payee];
    // 合约内部记录的该payee的deposit累计值清零
        _deposits[payee] = 0;
    // 从合约向payee发送对应数额的eth
        payee.sendValue(payment);
    // 抛出event
        emit Withdrawn(payee, payment);
    }

2.3 foundry代码验证

contract EscrowTest is Test {
    Escrow e = new Escrow();
    address payable payee = payable(address(1024));
    address other = address(2048);

    // copy events from Escrow.sol to do event assertion
    event Deposited(address indexed payee, uint256 weiAmount);
    event Withdrawn(address indexed payee, uint256 weiAmount);

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

    function test_DepositAndDepositsOf() external {
        // check owner
        address owner = address(this);
        assertEq(owner, e.owner());

        // check depositOf() before deposit
        assertEq(0, e.depositsOf(payee));

        // check event
        vm.expectEmit(address(e));
        emit Deposited(payee, 1 ether);
        // deposit
        e.deposit{value : 1 ether}(payee);
        // check balances
        assertEq(address(e).balance, 1 ether);
        assertEq(owner.balance, 10 ether - 1 ether);
        // check depositOf() after deposit
        assertEq(1 ether, e.depositsOf(payee));

        // deposit again
        e.deposit{value : 2 ether}(payee);
        // check balances
        assertEq(address(e).balance, 1 ether + 2 ether);
        assertEq(owner.balance, 10 ether - 1 ether - 2 ether);
        assertEq(1 ether + 2 ether, e.depositsOf(payee));

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

    function test_Withdraw() external {
        e.deposit{value : 1 ether}(payee);
        // check before withdraw
        assertEq(1 ether, address(e).balance);
        assertEq(0, payee.balance);

        // check event
        vm.expectEmit(address(e));
        emit Withdrawn(payee, 1 ether);
        // withdraw
        e.withdraw(payee);

        // check before withdraw
        assertEq(0, address(e).balance);
        assertEq(1 ether, payee.balance);

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

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

1.jpeg

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

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

0 条评论

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