-
Notifications
You must be signed in to change notification settings - Fork 163
/
ERC20Compatibility.t.sol
197 lines (173 loc) · 6.88 KB
/
ERC20Compatibility.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.23;
import "../patterns/erc20-compatibility/ERC20Compatibility.sol";
import "./TestUtils.sol";
interface IERC20Mintable is IERC20 {
function mint(address owner, uint256 amount) external;
}
contract ERC20CompatibilityTest is TestUtils {
using LibERC20Compat for IERC20Mintable;
IERC20Mintable goodToken = IERC20Mintable(address(new GoodERC20()));
IERC20Mintable badToken = IERC20Mintable(address(new BadERC20()));
function test_good_canMint() external {
goodToken.mint(address(this), 1e18);
assertEq(goodToken.balanceOf(address(this)), 1e18);
}
function test_good_canSafeTransfer() external {
address recipient = _randomAddress();
goodToken.mint(address(this), 1e18);
goodToken.safeTransfer(recipient, 0.25e18);
assertEq(goodToken.balanceOf(address(this)), 0.75e18);
assertEq(goodToken.balanceOf(recipient), 0.25e18);
}
function test_good_canSafeApprove() external {
address spender = _randomAddress();
goodToken.mint(address(this), 1e18);
goodToken.safeApprove(spender, 0.25e18);
assertEq(goodToken.allowance(address(this), spender), 0.25e18);
goodToken.safeApprove(spender, 0.5e18);
assertEq(goodToken.allowance(address(this), spender), 0.5e18);
}
function test_good_canSafeTransferFrom() external {
address spender = _randomAddress();
address recipient = _randomAddress();
goodToken.mint(address(this), 1e18);
goodToken.safeApprove(spender, 0.25e18);
vm.prank(spender);
goodToken.safeTransferFrom(address(this), recipient, 0.25e18);
assertEq(goodToken.balanceOf(address(this)), 0.75e18);
assertEq(goodToken.balanceOf(recipient), 0.25e18);
assertEq(goodToken.allowance(address(this), spender), 0);
}
function test_good_safeTransferWithBadRecipientFails() external {
goodToken.mint(address(this), 1e18);
vm.expectRevert('call failed');
this._extCallSafeTransfer(address(this), goodToken, address(0), 0.25e18);
}
function test_bad_canMint() external {
badToken.mint(address(this), 1e18);
assertEq(badToken.balanceOf(address(this)), 1e18);
}
function test_bad_rawTransferFails() external {
address recipient = _randomAddress();
badToken.mint(address(this), 1e18);
// Call in a new call context to capture the revert.
vm.expectRevert();
this._extCallTransfer(address(this), badToken, recipient, 0.25e18);
}
function test_bad_rawApproveFails() external {
address spender = _randomAddress();
badToken.mint(address(this), 1e18);
vm.expectRevert();
// Call in a new call context to capture the revert.
this._extCallApprove(address(this), badToken, spender, 0.25e18);
}
function test_bad_rawTransferFromFails() external {
address spender = _randomAddress();
address recipient = _randomAddress();
badToken.mint(address(this), 1e18);
BadERC20(address(badToken)).approve(spender, 0.25e18);
// Call in a new call context to capture the revert.
vm.expectRevert();
this._extCallTransferFrom(spender, badToken, address(this), recipient, 0.25e18);
}
function test_bad_approveWithoutResetFails() external {
address spender = _randomAddress();
badToken.mint(address(this), 1e18);
BadERC20(address(badToken)).approve(spender, 0.25e18);
// Call in a new call context to capture the revert.
vm.expectRevert('allowance must be reset');
this._extCallApprove(address(this), badToken, spender, 0.33e18);
}
function test_bad_canApproveWithReset() external {
address spender = _randomAddress();
badToken.mint(address(this), 1e18);
assertEq(goodToken.allowance(address(this), spender), 0);
BadERC20(address(badToken)).approve(spender, 0.25e18);
assertEq(badToken.allowance(address(this), spender), 0.25e18);
BadERC20(address(badToken)).approve(spender, 0);
assertEq(badToken.allowance(address(this), spender), 0);
BadERC20(address(badToken)).approve(spender, 0.5e18);
assertEq(badToken.allowance(address(this), spender), 0.5e18);
}
function test_bad_canSafeTransfer() external {
address recipient = _randomAddress();
badToken.mint(address(this), 1e18);
badToken.safeTransfer(recipient, 0.25e18);
assertEq(badToken.balanceOf(address(this)), 0.75e18);
assertEq(badToken.balanceOf(recipient), 0.25e18);
}
function test_bad_canSafeApprove() external {
address spender = _randomAddress();
badToken.mint(address(this), 1e18);
badToken.safeApprove(spender, 0.5e18);
assertEq(badToken.allowance(address(this), spender), 0.5e18);
}
function test_bad_canSafeApproveWithoutExplicitReset() external {
address spender = _randomAddress();
badToken.mint(address(this), 1e18);
badToken.safeApprove(spender, 0.5e18);
assertEq(badToken.allowance(address(this), spender), 0.5e18);
badToken.safeApprove(spender, 0.6e18);
assertEq(badToken.allowance(address(this), spender), 0.6e18);
}
function test_bad_canSafeTransferFrom() external {
address spender = _randomAddress();
address recipient = _randomAddress();
badToken.mint(address(this), 1e18);
badToken.safeApprove(spender, 0.25e18);
vm.prank(spender);
badToken.safeTransferFrom(address(this), recipient, 0.25e18);
assertEq(badToken.balanceOf(address(this)), 0.75e18);
assertEq(badToken.balanceOf(recipient), 0.25e18);
assertEq(badToken.allowance(address(this), spender), 0);
}
// External indirect call functions. These allows us to call an ERC20 function
// in a new call context, so the test can capture any revert that occurs without
// immediately failing the test function.
function _extCallApprove(
address caller,
IERC20Mintable token,
address spender,
uint256 allowance
)
external
{
vm.prank(caller);
token.approve(spender, allowance);
}
function _extCallTransfer(
address caller,
IERC20Mintable token,
address to,
uint256 amount
)
external
{
vm.prank(caller);
token.transfer(to, amount);
}
function _extCallTransferFrom(
address caller,
IERC20Mintable token,
address owner,
address to,
uint256 amount
)
external
{
vm.prank(caller);
token.transferFrom(owner, to, amount);
}
function _extCallSafeTransfer(
address caller,
IERC20Mintable token,
address to,
uint256 amount
)
external
{
vm.prank(caller);
token.safeTransfer(to, amount);
}
}