-
Notifications
You must be signed in to change notification settings - Fork 706
Expand file tree
/
Copy pathERC4626.t.sol
More file actions
446 lines (355 loc) · 18.3 KB
/
ERC4626.t.sol
File metadata and controls
446 lines (355 loc) · 18.3 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity 0.8.15;
import {DSTestPlus} from "./utils/DSTestPlus.sol";
import {MockERC20} from "./utils/mocks/MockERC20.sol";
import {MockERC4626} from "./utils/mocks/MockERC4626.sol";
contract ERC4626Test is DSTestPlus {
MockERC20 underlying;
MockERC4626 vault;
function setUp() public virtual {
underlying = new MockERC20("Mock Token", "TKN", 18);
vault = new MockERC4626(underlying, "Mock Token Vault", "vwTKN");
}
function invariantMetadata() public {
assertEq(vault.name(), "Mock Token Vault");
assertEq(vault.symbol(), "vwTKN");
assertEq(vault.decimals(), 18);
}
function testMetadata(string calldata name, string calldata symbol) public {
MockERC4626 vlt = new MockERC4626(underlying, name, symbol);
assertEq(vlt.name(), name);
assertEq(vlt.symbol(), symbol);
assertEq(address(vlt.asset()), address(underlying));
}
function testSingleDepositWithdraw(uint128 amount) public {
if (amount == 0) amount = 1;
uint256 aliceUnderlyingAmount = amount;
address alice = address(0xABCD);
underlying.mint(alice, aliceUnderlyingAmount);
hevm.prank(alice);
underlying.approve(address(vault), aliceUnderlyingAmount);
assertEq(underlying.allowance(alice, address(vault)), aliceUnderlyingAmount);
uint256 alicePreDepositBal = underlying.balanceOf(alice);
hevm.prank(alice);
uint256 aliceShareAmount = vault.deposit(aliceUnderlyingAmount, alice);
assertEq(vault.afterDepositHookCalledCounter(), 1);
// Expect exchange rate to be 1:1 on initial deposit.
assertEq(aliceUnderlyingAmount, aliceShareAmount);
assertEq(vault.previewWithdraw(aliceShareAmount), aliceUnderlyingAmount);
assertEq(vault.previewDeposit(aliceUnderlyingAmount), aliceShareAmount);
assertEq(vault.totalSupply(), aliceShareAmount);
assertEq(vault.totalAssets(), aliceUnderlyingAmount);
assertEq(vault.balanceOf(alice), aliceShareAmount);
assertEq(vault.convertToAssets(vault.balanceOf(alice)), aliceUnderlyingAmount);
assertEq(underlying.balanceOf(alice), alicePreDepositBal - aliceUnderlyingAmount);
hevm.prank(alice);
vault.withdraw(aliceUnderlyingAmount, alice, alice);
assertEq(vault.beforeWithdrawHookCalledCounter(), 1);
assertEq(vault.totalAssets(), 0);
assertEq(vault.balanceOf(alice), 0);
assertEq(vault.convertToAssets(vault.balanceOf(alice)), 0);
assertEq(underlying.balanceOf(alice), alicePreDepositBal);
}
function testSingleMintRedeem(uint128 amount) public {
if (amount == 0) amount = 1;
uint256 aliceShareAmount = amount;
address alice = address(0xABCD);
underlying.mint(alice, aliceShareAmount);
hevm.prank(alice);
underlying.approve(address(vault), aliceShareAmount);
assertEq(underlying.allowance(alice, address(vault)), aliceShareAmount);
uint256 alicePreDepositBal = underlying.balanceOf(alice);
hevm.prank(alice);
uint256 aliceUnderlyingAmount = vault.mint(aliceShareAmount, alice);
assertEq(vault.afterDepositHookCalledCounter(), 1);
// Expect exchange rate to be 1:1 on initial mint.
assertEq(aliceShareAmount, aliceUnderlyingAmount);
assertEq(vault.previewWithdraw(aliceShareAmount), aliceUnderlyingAmount);
assertEq(vault.previewDeposit(aliceUnderlyingAmount), aliceShareAmount);
assertEq(vault.totalSupply(), aliceShareAmount);
assertEq(vault.totalAssets(), aliceUnderlyingAmount);
assertEq(vault.balanceOf(alice), aliceUnderlyingAmount);
assertEq(vault.convertToAssets(vault.balanceOf(alice)), aliceUnderlyingAmount);
assertEq(underlying.balanceOf(alice), alicePreDepositBal - aliceUnderlyingAmount);
hevm.prank(alice);
vault.redeem(aliceShareAmount, alice, alice);
assertEq(vault.beforeWithdrawHookCalledCounter(), 1);
assertEq(vault.totalAssets(), 0);
assertEq(vault.balanceOf(alice), 0);
assertEq(vault.convertToAssets(vault.balanceOf(alice)), 0);
assertEq(underlying.balanceOf(alice), alicePreDepositBal);
}
function testMultipleMintDepositRedeemWithdraw() public {
// Scenario:
// A = Alice, B = Bob
// ________________________________________________________
// | Vault shares | A share | A assets | B share | B assets |
// |========================================================|
// | 1. Alice mints 2000 shares (costs 2000 tokens) |
// |--------------|---------|----------|---------|----------|
// | 2000 | 2000 | 2000 | 0 | 0 |
// |--------------|---------|----------|---------|----------|
// | 2. Bob deposits 4000 tokens (mints 4000 shares) |
// |--------------|---------|----------|---------|----------|
// | 6000 | 2000 | 2000 | 4000 | 4000 |
// |--------------|---------|----------|---------|----------|
// | 3. Vault mutates by +3000 tokens... |
// | (simulated yield returned from strategy)... |
// |--------------|---------|----------|---------|----------|
// | 6000 | 2000 | 3000 | 4000 | 6000 |
// |--------------|---------|----------|---------|----------|
// | 4. Alice deposits 2000 tokens (mints 1333 shares) |
// |--------------|---------|----------|---------|----------|
// | 7333 | 3333 | 4999 | 4000 | 6000 |
// |--------------|---------|----------|---------|----------|
// | 5. Bob mints 2000 shares (costs 3001 assets) |
// | NOTE: Bob's assets spent got rounded up |
// | NOTE: Alice's vault assets got rounded up |
// |--------------|---------|----------|---------|----------|
// | 9333 | 3333 | 5000 | 6000 | 9000 |
// |--------------|---------|----------|---------|----------|
// | 6. Vault mutates by +3000 tokens... |
// | (simulated yield returned from strategy) |
// | NOTE: Vault holds 17001 tokens, but sum of |
// | assetsOf() is 17000. |
// |--------------|---------|----------|---------|----------|
// | 9333 | 3333 | 6071 | 6000 | 10929 |
// |--------------|---------|----------|---------|----------|
// | 7. Alice redeem 1333 shares (2428 assets) |
// |--------------|---------|----------|---------|----------|
// | 8000 | 2000 | 3643 | 6000 | 10929 |
// |--------------|---------|----------|---------|----------|
// | 8. Bob withdraws 2928 assets (1608 shares) |
// |--------------|---------|----------|---------|----------|
// | 6392 | 2000 | 3643 | 4392 | 8000 |
// |--------------|---------|----------|---------|----------|
// | 9. Alice withdraws 3643 assets (2000 shares) |
// | NOTE: Bob's assets have been rounded back up |
// |--------------|---------|----------|---------|----------|
// | 4392 | 0 | 0 | 4392 | 8001 |
// |--------------|---------|----------|---------|----------|
// | 10. Bob redeem 4392 shares (8001 tokens) |
// |--------------|---------|----------|---------|----------|
// | 0 | 0 | 0 | 0 | 0 |
// |______________|_________|__________|_________|__________|
address alice = address(0xABCD);
address bob = address(0xDCBA);
uint256 mutationUnderlyingAmount = 3000;
underlying.mint(alice, 4000);
hevm.prank(alice);
underlying.approve(address(vault), 4000);
assertEq(underlying.allowance(alice, address(vault)), 4000);
underlying.mint(bob, 7001);
hevm.prank(bob);
underlying.approve(address(vault), 7001);
assertEq(underlying.allowance(bob, address(vault)), 7001);
// 1. Alice mints 2000 shares (costs 2000 tokens)
hevm.prank(alice);
uint256 aliceUnderlyingAmount = vault.mint(2000, alice);
uint256 aliceShareAmount = vault.previewDeposit(aliceUnderlyingAmount);
assertEq(vault.afterDepositHookCalledCounter(), 1);
// Expect to have received the requested mint amount.
assertEq(aliceShareAmount, 2000);
assertEq(vault.balanceOf(alice), aliceShareAmount);
assertEq(vault.convertToAssets(vault.balanceOf(alice)), aliceUnderlyingAmount);
assertEq(vault.convertToShares(aliceUnderlyingAmount), vault.balanceOf(alice));
// Expect a 1:1 ratio before mutation.
assertEq(aliceUnderlyingAmount, 2000);
// Sanity check.
assertEq(vault.totalSupply(), aliceShareAmount);
assertEq(vault.totalAssets(), aliceUnderlyingAmount);
// 2. Bob deposits 4000 tokens (mints 4000 shares)
hevm.prank(bob);
uint256 bobShareAmount = vault.deposit(4000, bob);
uint256 bobUnderlyingAmount = vault.previewWithdraw(bobShareAmount);
assertEq(vault.afterDepositHookCalledCounter(), 2);
// Expect to have received the requested underlying amount.
assertEq(bobUnderlyingAmount, 4000);
assertEq(vault.balanceOf(bob), bobShareAmount);
assertEq(vault.convertToAssets(vault.balanceOf(bob)), bobUnderlyingAmount);
assertEq(vault.convertToShares(bobUnderlyingAmount), vault.balanceOf(bob));
// Expect a 1:1 ratio before mutation.
assertEq(bobShareAmount, bobUnderlyingAmount);
// Sanity check.
uint256 preMutationShareBal = aliceShareAmount + bobShareAmount;
uint256 preMutationBal = aliceUnderlyingAmount + bobUnderlyingAmount;
assertEq(vault.totalSupply(), preMutationShareBal);
assertEq(vault.totalAssets(), preMutationBal);
assertEq(vault.totalSupply(), 6000);
assertEq(vault.totalAssets(), 6000);
// 3. Vault mutates by +3000 tokens... |
// (simulated yield returned from strategy)...
// The Vault now contains more tokens than deposited which causes the exchange rate to change.
// Alice share is 33.33% of the Vault, Bob 66.66% of the Vault.
// Alice's share count stays the same but the underlying amount changes from 2000 to 3000.
// Bob's share count stays the same but the underlying amount changes from 4000 to 6000.
underlying.mint(address(vault), mutationUnderlyingAmount);
assertEq(vault.totalSupply(), preMutationShareBal);
assertEq(vault.totalAssets(), preMutationBal + mutationUnderlyingAmount);
assertEq(vault.balanceOf(alice), aliceShareAmount);
assertEq(
vault.convertToAssets(vault.balanceOf(alice)),
aliceUnderlyingAmount + (mutationUnderlyingAmount / 3) * 1
);
assertEq(vault.balanceOf(bob), bobShareAmount);
assertEq(vault.convertToAssets(vault.balanceOf(bob)), bobUnderlyingAmount + (mutationUnderlyingAmount / 3) * 2);
// 4. Alice deposits 2000 tokens (mints 1333 shares)
hevm.prank(alice);
vault.deposit(2000, alice);
assertEq(vault.totalSupply(), 7333);
assertEq(vault.balanceOf(alice), 3333);
assertEq(vault.convertToAssets(vault.balanceOf(alice)), 4999);
assertEq(vault.balanceOf(bob), 4000);
assertEq(vault.convertToAssets(vault.balanceOf(bob)), 6000);
// 5. Bob mints 2000 shares (costs 3001 assets)
// NOTE: Bob's assets spent got rounded up
// NOTE: Alices's vault assets got rounded up
hevm.prank(bob);
vault.mint(2000, bob);
assertEq(vault.totalSupply(), 9333);
assertEq(vault.balanceOf(alice), 3333);
assertEq(vault.convertToAssets(vault.balanceOf(alice)), 5000);
assertEq(vault.balanceOf(bob), 6000);
assertEq(vault.convertToAssets(vault.balanceOf(bob)), 9000);
// Sanity checks:
// Alice and bob should have spent all their tokens now
assertEq(underlying.balanceOf(alice), 0);
assertEq(underlying.balanceOf(bob), 0);
// Assets in vault: 4k (alice) + 7k (bob) + 3k (yield) + 1 (round up)
assertEq(vault.totalAssets(), 14001);
// 6. Vault mutates by +3000 tokens
// NOTE: Vault holds 17001 tokens, but sum of assetsOf() is 17000.
underlying.mint(address(vault), mutationUnderlyingAmount);
assertEq(vault.totalAssets(), 17001);
assertEq(vault.convertToAssets(vault.balanceOf(alice)), 6071);
assertEq(vault.convertToAssets(vault.balanceOf(bob)), 10929);
// 7. Alice redeem 1333 shares (2428 assets)
hevm.prank(alice);
vault.redeem(1333, alice, alice);
assertEq(underlying.balanceOf(alice), 2428);
assertEq(vault.totalSupply(), 8000);
assertEq(vault.totalAssets(), 14573);
assertEq(vault.balanceOf(alice), 2000);
assertEq(vault.convertToAssets(vault.balanceOf(alice)), 3643);
assertEq(vault.balanceOf(bob), 6000);
assertEq(vault.convertToAssets(vault.balanceOf(bob)), 10929);
// 8. Bob withdraws 2929 assets (1608 shares)
hevm.prank(bob);
vault.withdraw(2929, bob, bob);
assertEq(underlying.balanceOf(bob), 2929);
assertEq(vault.totalSupply(), 6392);
assertEq(vault.totalAssets(), 11644);
assertEq(vault.balanceOf(alice), 2000);
assertEq(vault.convertToAssets(vault.balanceOf(alice)), 3643);
assertEq(vault.balanceOf(bob), 4392);
assertEq(vault.convertToAssets(vault.balanceOf(bob)), 8000);
// 9. Alice withdraws 3643 assets (2000 shares)
// NOTE: Bob's assets have been rounded back up
hevm.prank(alice);
vault.withdraw(3643, alice, alice);
assertEq(underlying.balanceOf(alice), 6071);
assertEq(vault.totalSupply(), 4392);
assertEq(vault.totalAssets(), 8001);
assertEq(vault.balanceOf(alice), 0);
assertEq(vault.convertToAssets(vault.balanceOf(alice)), 0);
assertEq(vault.balanceOf(bob), 4392);
assertEq(vault.convertToAssets(vault.balanceOf(bob)), 8001);
// 10. Bob redeem 4392 shares (8001 tokens)
hevm.prank(bob);
vault.redeem(4392, bob, bob);
assertEq(underlying.balanceOf(bob), 10930);
assertEq(vault.totalSupply(), 0);
assertEq(vault.totalAssets(), 0);
assertEq(vault.balanceOf(alice), 0);
assertEq(vault.convertToAssets(vault.balanceOf(alice)), 0);
assertEq(vault.balanceOf(bob), 0);
assertEq(vault.convertToAssets(vault.balanceOf(bob)), 0);
// Sanity check
assertEq(underlying.balanceOf(address(vault)), 0);
}
function testFailDepositWithNotEnoughApproval() public {
underlying.mint(address(this), 0.5e18);
underlying.approve(address(vault), 0.5e18);
assertEq(underlying.allowance(address(this), address(vault)), 0.5e18);
vault.deposit(1e18, address(this));
}
function testFailWithdrawWithNotEnoughUnderlyingAmount() public {
underlying.mint(address(this), 0.5e18);
underlying.approve(address(vault), 0.5e18);
vault.deposit(0.5e18, address(this));
vault.withdraw(1e18, address(this), address(this));
}
function testFailRedeemWithNotEnoughShareAmount() public {
underlying.mint(address(this), 0.5e18);
underlying.approve(address(vault), 0.5e18);
vault.deposit(0.5e18, address(this));
vault.redeem(1e18, address(this), address(this));
}
function testFailWithdrawWithNoUnderlyingAmount() public {
vault.withdraw(1e18, address(this), address(this));
}
function testFailRedeemWithNoShareAmount() public {
vault.redeem(1e18, address(this), address(this));
}
function testFailDepositWithNoApproval() public {
vault.deposit(1e18, address(this));
}
function testFailMintWithNoApproval() public {
vault.mint(1e18, address(this));
}
function testFailDepositZero() public {
vault.deposit(0, address(this));
}
function testMintZero() public {
vault.mint(0, address(this));
assertEq(vault.balanceOf(address(this)), 0);
assertEq(vault.convertToAssets(vault.balanceOf(address(this))), 0);
assertEq(vault.totalSupply(), 0);
assertEq(vault.totalAssets(), 0);
}
function testFailRedeemZero() public {
vault.redeem(0, address(this), address(this));
}
function testWithdrawZero() public {
vault.withdraw(0, address(this), address(this));
assertEq(vault.balanceOf(address(this)), 0);
assertEq(vault.convertToAssets(vault.balanceOf(address(this))), 0);
assertEq(vault.totalSupply(), 0);
assertEq(vault.totalAssets(), 0);
}
function testVaultInteractionsForSomeoneElse() public {
// init 2 users with a 1e18 balance
address alice = address(0xABCD);
address bob = address(0xDCBA);
underlying.mint(alice, 1e18);
underlying.mint(bob, 1e18);
hevm.prank(alice);
underlying.approve(address(vault), 1e18);
hevm.prank(bob);
underlying.approve(address(vault), 1e18);
// alice deposits 1e18 for bob
hevm.prank(alice);
vault.deposit(1e18, bob);
assertEq(vault.balanceOf(alice), 0);
assertEq(vault.balanceOf(bob), 1e18);
assertEq(underlying.balanceOf(alice), 0);
// bob mint 1e18 for alice
hevm.prank(bob);
vault.mint(1e18, alice);
assertEq(vault.balanceOf(alice), 1e18);
assertEq(vault.balanceOf(bob), 1e18);
assertEq(underlying.balanceOf(bob), 0);
// alice redeem 1e18 for bob
hevm.prank(alice);
vault.redeem(1e18, bob, alice);
assertEq(vault.balanceOf(alice), 0);
assertEq(vault.balanceOf(bob), 1e18);
assertEq(underlying.balanceOf(bob), 1e18);
// bob withdraw 1e18 for alice
hevm.prank(bob);
vault.withdraw(1e18, alice, bob);
assertEq(vault.balanceOf(alice), 0);
assertEq(vault.balanceOf(bob), 0);
assertEq(underlying.balanceOf(alice), 1e18);
}
}