-
Notifications
You must be signed in to change notification settings - Fork 706
Expand file tree
/
Copy pathERC20Vainilla.t.sol
More file actions
300 lines (222 loc) · 8 KB
/
ERC20Vainilla.t.sol
File metadata and controls
300 lines (222 loc) · 8 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity 0.8.15;
import {DSTestPlus} from "./utils/DSTestPlus.sol";
import {DSInvariantTest} from "./utils/DSInvariantTest.sol";
import {MockERC20Vainilla} from "./utils/mocks/MockERC20Vainilla.sol";
contract ERC20VainillaTest is DSTestPlus {
MockERC20Vainilla token;
function setUp() public {
token = new MockERC20Vainilla("Token", "TKN", 18);
}
function invariantMetadata() public {
assertEq(token.name(), "Token");
assertEq(token.symbol(), "TKN");
assertEq(token.decimals(), 18);
}
function testMint() public {
token.mint(address(0xBEEF), 1e18);
assertEq(token.totalSupply(), 1e18);
assertEq(token.balanceOf(address(0xBEEF)), 1e18);
}
function testBurn() public {
token.mint(address(0xBEEF), 1e18);
token.burn(address(0xBEEF), 0.9e18);
assertEq(token.totalSupply(), 1e18 - 0.9e18);
assertEq(token.balanceOf(address(0xBEEF)), 0.1e18);
}
function testApprove() public {
assertTrue(token.approve(address(0xBEEF), 1e18));
assertEq(token.allowance(address(this), address(0xBEEF)), 1e18);
}
function testTransfer() public {
token.mint(address(this), 1e18);
assertTrue(token.transfer(address(0xBEEF), 1e18));
assertEq(token.totalSupply(), 1e18);
assertEq(token.balanceOf(address(this)), 0);
assertEq(token.balanceOf(address(0xBEEF)), 1e18);
}
function testTransferFrom() public {
address from = address(0xABCD);
token.mint(from, 1e18);
hevm.prank(from);
token.approve(address(this), 1e18);
assertTrue(token.transferFrom(from, address(0xBEEF), 1e18));
assertEq(token.totalSupply(), 1e18);
assertEq(token.allowance(from, address(this)), 0);
assertEq(token.balanceOf(from), 0);
assertEq(token.balanceOf(address(0xBEEF)), 1e18);
}
function testInfiniteApproveTransferFrom() public {
address from = address(0xABCD);
token.mint(from, 1e18);
hevm.prank(from);
token.approve(address(this), type(uint256).max);
assertTrue(token.transferFrom(from, address(0xBEEF), 1e18));
assertEq(token.totalSupply(), 1e18);
assertEq(token.allowance(from, address(this)), type(uint256).max);
assertEq(token.balanceOf(from), 0);
assertEq(token.balanceOf(address(0xBEEF)), 1e18);
}
function testFailTransferInsufficientBalance() public {
token.mint(address(this), 0.9e18);
token.transfer(address(0xBEEF), 1e18);
}
function testFailTransferFromInsufficientAllowance() public {
address from = address(0xABCD);
token.mint(from, 1e18);
hevm.prank(from);
token.approve(address(this), 0.9e18);
token.transferFrom(from, address(0xBEEF), 1e18);
}
function testFailTransferFromInsufficientBalance() public {
address from = address(0xABCD);
token.mint(from, 0.9e18);
hevm.prank(from);
token.approve(address(this), 1e18);
token.transferFrom(from, address(0xBEEF), 1e18);
}
function testMetadata(
string calldata name,
string calldata symbol,
uint8 decimals
) public {
MockERC20Vainilla tkn = new MockERC20Vainilla(name, symbol, decimals);
assertEq(tkn.name(), name);
assertEq(tkn.symbol(), symbol);
assertEq(tkn.decimals(), decimals);
}
function testMint(address from, uint256 amount) public {
token.mint(from, amount);
assertEq(token.totalSupply(), amount);
assertEq(token.balanceOf(from), amount);
}
function testBurn(
address from,
uint256 mintAmount,
uint256 burnAmount
) public {
burnAmount = bound(burnAmount, 0, mintAmount);
token.mint(from, mintAmount);
token.burn(from, burnAmount);
assertEq(token.totalSupply(), mintAmount - burnAmount);
assertEq(token.balanceOf(from), mintAmount - burnAmount);
}
function testApprove(address to, uint256 amount) public {
assertTrue(token.approve(to, amount));
assertEq(token.allowance(address(this), to), amount);
}
function testTransfer(address from, uint256 amount) public {
token.mint(address(this), amount);
assertTrue(token.transfer(from, amount));
assertEq(token.totalSupply(), amount);
if (address(this) == from) {
assertEq(token.balanceOf(address(this)), amount);
} else {
assertEq(token.balanceOf(address(this)), 0);
assertEq(token.balanceOf(from), amount);
}
}
function testTransferFrom(
address to,
uint256 approval,
uint256 amount
) public {
amount = bound(amount, 0, approval);
address from = address(0xABCD);
token.mint(from, amount);
hevm.prank(from);
token.approve(address(this), approval);
assertTrue(token.transferFrom(from, to, amount));
assertEq(token.totalSupply(), amount);
uint256 app = from == address(this) || approval == type(uint256).max ? approval : approval - amount;
assertEq(token.allowance(from, address(this)), app);
if (from == to) {
assertEq(token.balanceOf(from), amount);
} else {
assertEq(token.balanceOf(from), 0);
assertEq(token.balanceOf(to), amount);
}
}
function testFailBurnInsufficientBalance(
address to,
uint256 mintAmount,
uint256 burnAmount
) public {
burnAmount = bound(burnAmount, mintAmount + 1, type(uint256).max);
token.mint(to, mintAmount);
token.burn(to, burnAmount);
}
function testFailTransferInsufficientBalance(
address to,
uint256 mintAmount,
uint256 sendAmount
) public {
sendAmount = bound(sendAmount, mintAmount + 1, type(uint256).max);
token.mint(address(this), mintAmount);
token.transfer(to, sendAmount);
}
function testFailTransferFromInsufficientAllowance(
address to,
uint256 approval,
uint256 amount
) public {
amount = bound(amount, approval + 1, type(uint256).max);
address from = address(0xABCD);
token.mint(from, amount);
hevm.prank(from);
token.approve(address(this), approval);
token.transferFrom(from, to, amount);
}
function testFailTransferFromInsufficientBalance(
address to,
uint256 mintAmount,
uint256 sendAmount
) public {
sendAmount = bound(sendAmount, mintAmount + 1, type(uint256).max);
address from = address(0xABCD);
token.mint(from, mintAmount);
hevm.prank(from);
token.approve(address(this), sendAmount);
token.transferFrom(from, to, sendAmount);
}
}
contract ERC20VainillaInvariants is DSTestPlus, DSInvariantTest {
VainillaBalanceSum balanceSum;
MockERC20Vainilla token;
function setUp() public {
token = new MockERC20Vainilla("Token", "TKN", 18);
balanceSum = new VainillaBalanceSum(token);
addTargetContract(address(balanceSum));
}
function invariantBalanceSum() public {
assertEq(token.totalSupply(), balanceSum.sum());
}
}
contract VainillaBalanceSum {
MockERC20Vainilla token;
uint256 public sum;
constructor(MockERC20Vainilla _token) {
token = _token;
}
function mint(address from, uint256 amount) public {
token.mint(from, amount);
sum += amount;
}
function burn(address from, uint256 amount) public {
token.burn(from, amount);
sum -= amount;
}
function approve(address to, uint256 amount) public {
token.approve(to, amount);
}
function transferFrom(
address from,
address to,
uint256 amount
) public {
token.transferFrom(from, to, amount);
}
function transfer(address to, uint256 amount) public {
token.transfer(to, amount);
}
}