-
Notifications
You must be signed in to change notification settings - Fork 163
/
ApproveRestrictedWallet.t.sol
70 lines (57 loc) · 2.29 KB
/
ApproveRestrictedWallet.t.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;
import "../patterns/abi-decode-with-selector/ApproveRestrictedWallet.sol";
import "./TestUtils.sol";
interface IMockEvents {
event ApproveCalled(address spender, uint256 allowance);
event PayCalled(uint256 value);
}
abstract contract ApproveRestrictedWalletTestBase is IMockEvents, TestUtils {
ApproveRestrictedWalletBase wallet;
TestERC20 erc20 = new TestERC20();
address allowedSpender;
function setUp() external {
wallet = _createWallet();
allowedSpender = _randomAddress();
wallet.setAllowedSpender(allowedSpender, true);
}
function pay() external payable {
emit PayCalled(msg.value);
}
function _createWallet() internal virtual returns (ApproveRestrictedWalletBase);
function test_canCallWithValue() external {
_expectNonIndexedEmit();
emit PayCalled(1);
wallet.exec{value: 1}(payable(address(this)), abi.encodeCall(this.pay, ()), 1);
}
function test_onlyOwnerCanExec() external {
vm.expectRevert('only owner');
vm.prank(_randomAddress());
wallet.exec(payable(address(this)), abi.encodeCall(this.pay, ()), 0);
}
function test_canExecApprove() external {
_expectNonIndexedEmit();
emit ApproveCalled(allowedSpender, 1);
wallet.exec(payable(address(erc20)), abi.encodeCall(IERC20.approve, (allowedSpender, 1)), 0);
}
function test_canRejectApprove() external {
vm.expectRevert('not an allowed spender');
wallet.exec(payable(address(erc20)), abi.encodeCall(IERC20.approve, (_randomAddress(), 1)), 0);
}
}
contract ApproveRestrictedWalletTest is ApproveRestrictedWalletTestBase {
function _createWallet() internal override returns (ApproveRestrictedWalletBase) {
return new ApproveRestrictedWallet(address(this));
}
}
contract ApproveRestrictedWallet_MemoryTest is ApproveRestrictedWalletTestBase {
function _createWallet() internal override returns (ApproveRestrictedWalletBase) {
return new ApproveRestrictedWallet_Memory(address(this));
}
}
contract TestERC20 is IERC20, IMockEvents {
function approve(address spender, uint256 allowance) external returns (bool) {
emit ApproveCalled(spender, allowance);
return true;
}
}