Escrow合约是Openzeppelin中所有escrow拓展库的基础合约,用于为指定地址锁存eth和提取eth的托管场景。该合约中存入和取出eth的方法都被virtual修饰,开发者可以通过继承重写的方式来做相关修改。
[openzeppelin]:v4.8.3,[forge-std]:v1.5.6
Github: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v4.8.3/contracts/utils/escrow/Escrow.sol
Escrow合约是Openzeppelin中所有escrow拓展库的基础合约,用于为指定地址锁存eth和提取eth的托管场景。该合约中存入和取出eth的方法都被virtual修饰,开发者可以通过继承重写的方式来做相关修改。
Escrow本身就是一个contract(非abstract contract),可独立部署和调用。
全部foundry测试合约:
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);
}
合约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);
}
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神。 以下是我个人的公众号,如果有技术问题可以关注我的公众号来跟我交流。 同时我也会在这个公众号上每周更新我的原创文章,喜欢的小伙伴或者老伙计可以支持一下! 如果需要转发,麻烦注明作者。十分感谢!
公众号名称:后现代泼痞浪漫主义奠基人
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!