Michael.W基于Foundry精读Openzeppelin第29期——RefundEscrow.sol
0. 版本
[openzeppelin]:v4.8.3,[forge-std]:v1.5.6
0.1 RefundEscrow.sol
RefundEscrow合约继承了ConditionalEscrow合约,是ConditionalEscrow合约的一种功能拓展。RefundEscrow合约提供了基础的存取eth功能,同时合约owner可以将合约切换到Refunding或Closed状态。Refunding状态下,owner可以将锁存的eth分发给对应refundee;Closed状态下,beneficiary可以提走合约内全部eth。
1. 目标合约
RefundEscrow本身就是一个contract(非abstract contract),可独立部署和调用。
全部foundry测试合约:
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神。
以下是我个人的公众号,如果有技术问题可以关注我的公众号来跟我交流。
同时我也会在这个公众号上每周更新我的原创文章,喜欢的小伙伴或者老伙计可以支持一下!
如果需要转发,麻烦注明作者。十分感谢!
公众号名称:后现代泼痞浪漫主义奠基人