forked from dragonfly-xyz/useful-solidity-patterns
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ProxyWallet.t.sol
79 lines (67 loc) · 2.53 KB
/
ProxyWallet.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
71
72
73
74
75
76
77
78
79
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.17;
import "../patterns/basic-proxies/ProxyWallet.sol";
import "./TestUtils.sol";
contract ProxyWalletTest is TestUtils {
WalletProxy proxy;
WalletLogicV1 logicV1 = new WalletLogicV1();
WalletLogicV2 logicV2 = new WalletLogicV2();
DummyERC20 erc20 = new DummyERC20();
constructor() {
proxy = new WalletProxy(address(logicV1));
}
function test_canUpgrade() external {
WalletLogicV1 wallet = WalletLogicV1(payable(proxy));
assertEq(wallet.version(), 'V1');
proxy.upgrade(address(logicV2));
assertEq(wallet.version(), 'V2');
}
function test_onlyOwnerCanUpgrade() external {
vm.prank(_randomAddress());
vm.expectRevert('only owner');
proxy.upgrade(address(logicV2));
}
function test_v1CanTransferETH() external {
WalletLogicV1 wallet = WalletLogicV1(payable(proxy));
payable(address(wallet)).transfer(100);
address payable recipient = _randomAddress();
wallet.transferETH(recipient, 1);
assertEq(recipient.balance, 1);
}
function test_v1CanOnlyTransferETHAsOwner() external {
WalletLogicV1 wallet = WalletLogicV1(payable(proxy));
payable(address(wallet)).transfer(100);
address payable recipient = _randomAddress();
vm.prank(_randomAddress());
vm.expectRevert('only owner');
wallet.transferETH(recipient, 1);
}
function test_v1CanTransferERC20() external {
proxy.upgrade(address(logicV2));
WalletLogicV2 wallet = WalletLogicV2(payable(proxy));
erc20.mint(address(wallet), 100);
address recipient = _randomAddress();
wallet.transferERC20(erc20, recipient, 1);
assertEq(erc20.balanceOf(recipient), 1);
}
function test_v1CanOnlyTransferERC20AsOwner() external {
proxy.upgrade(address(logicV2));
WalletLogicV2 wallet = WalletLogicV2(payable(proxy));
erc20.mint(address(wallet), 100);
address recipient = _randomAddress();
vm.prank(_randomAddress());
vm.expectRevert('only owner');
wallet.transferERC20(erc20, recipient, 1);
}
}
contract DummyERC20 is IERC20 {
mapping (address => uint256) public balanceOf;
function mint(address owner, uint256 amount) external {
balanceOf[owner] += amount;
}
function transfer(address to, uint256 amount) external returns (bool) {
balanceOf[msg.sender] -= amount;
balanceOf[to] += amount;
return true;
}
}