forked from Ithil-protocol/v2-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVault.test.sol
618 lines (552 loc) · 22.2 KB
/
Vault.test.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
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.18;
import { IERC20, IERC20Metadata } from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
import { ERC20PresetMinterPauser } from "@openzeppelin/contracts/token/ERC20/presets/ERC20PresetMinterPauser.sol";
import { Math } from "@openzeppelin/contracts/utils/math/Math.sol";
import { Test } from "forge-std/Test.sol";
import { IVault, Vault } from "../src/Vault.sol";
import { GeneralMath } from "./helpers/GeneralMath.sol";
import { SignUtils } from "./helpers/SignUtils.sol";
import { PermitToken } from "./helpers/PermitToken.sol";
/// @dev Vault native state:
/// - Native:
/// address public immutable manager;
/// uint256 public immutable override creationTime;
/// uint256 public override feeUnlockTime;
/// uint256 public override netLoans;
/// uint256 public override latestRepay;
/// uint256 public override currentProfits;
/// uint256 public override currentLosses;
/// @dev Vault ERC4626 state
/// - Vault is ERC4626: totalSupply(), balanceOf(address(this)), balanceOf(msg.sender),
/// - balanceOf(owner), balanceOf(receiver) (deposit, withdraw, directMint, directBurn)
/// @dev Vault underlying ERC20 state
/// - ERC4626 -> constructor(IERC20 asset_): totalSupply(), balanceOf(address(this)), balanceOf(msg.sender),
/// - balanceOf(owner), balanceOf(receiver) (deposit, withdraw, directMint, directBurn)
contract VaultTest is Test {
using Math for uint256;
using GeneralMath for uint256;
Vault internal immutable vault;
PermitToken internal immutable token;
ERC20PresetMinterPauser internal immutable spuriousToken;
address internal immutable tokenSink;
address internal immutable notOwner;
address internal immutable anyAddress;
address internal immutable depositor;
address internal immutable receiver;
address internal immutable borrower;
address internal immutable repayer;
constructor() {
token = new PermitToken("test", "TEST");
vault = new Vault(IERC20Metadata(address(token)));
tokenSink = address(uint160(uint(keccak256(abi.encodePacked("Sink")))));
notOwner = address(uint160(uint(keccak256(abi.encodePacked("Not Owner")))));
anyAddress = address(uint160(uint(keccak256(abi.encodePacked("Any Address")))));
depositor = address(uint160(uint(keccak256(abi.encodePacked("Depositor")))));
receiver = address(uint160(uint(keccak256(abi.encodePacked("Receiver")))));
repayer = address(uint160(uint(keccak256(abi.encodePacked("Repayer")))));
borrower = address(uint160(uint(keccak256(abi.encodePacked("Borrower")))));
spuriousToken = new ERC20PresetMinterPauser("spurious", "SPURIOUS");
}
function setUp() public {
token.mint(tokenSink, type(uint256).max);
token.approve(address(vault), type(uint256).max);
}
function testBase() public {
// Native state
assertTrue(vault.manager() == address(this));
assertTrue(vault.creationTime() == block.timestamp);
assertTrue(vault.feeUnlockTime() == 21600);
assertTrue(vault.latestRepay() == 0);
assertTrue(vault.netLoans() == 0);
assertTrue(vault.currentProfits() == 0);
assertTrue(vault.currentLosses() == 0);
// ERC4626 state
assertTrue(vault.totalSupply() == 0);
assertTrue(vault.balanceOf(address(this)) == 0);
assertTrue(vault.balanceOf(msg.sender) == 0);
assertTrue(keccak256(bytes(vault.name())) == keccak256(bytes("Ithil test")));
assertTrue(keccak256(bytes(vault.symbol())) == keccak256(bytes("iTEST")));
assertTrue(vault.asset() == address(token));
assertTrue(vault.decimals() == token.decimals());
// Underlying ERC20 state
assertTrue(token.balanceOf(address(vault)) == 0);
assertTrue(token.balanceOf(tokenSink) == type(uint256).max);
}
function testAccess(uint256 shares, uint256 assets, uint256 debt) public {
vm.startPrank(notOwner);
vm.expectRevert(bytes4(keccak256(abi.encodePacked("RestrictedToOwner()"))));
vault.setFeeUnlockTime(1000);
vm.expectRevert(bytes4(keccak256(abi.encodePacked("RestrictedToOwner()"))));
vault.sweep(anyAddress, address(spuriousToken));
vm.expectRevert(bytes4(keccak256(abi.encodePacked("RestrictedToOwner()"))));
vault.borrow(assets, assets, anyAddress);
vm.expectRevert(bytes4(keccak256(abi.encodePacked("RestrictedToOwner()"))));
vault.repay(assets, debt, anyAddress);
vm.stopPrank();
}
function _originalStateCheck(
uint256 feeUnlockTime,
uint256 totalSupply,
uint256 balanceOf,
uint256 netLoans,
uint256 latestRepay,
uint256 currentProfits,
uint256 currentLosses
) internal {
assertTrue(vault.feeUnlockTime() == feeUnlockTime);
assertTrue(vault.totalSupply() == totalSupply);
assertTrue(token.balanceOf(address(vault)) == balanceOf);
assertTrue(vault.netLoans() == netLoans);
assertTrue(vault.latestRepay() == latestRepay);
assertTrue(vault.currentProfits() == currentProfits);
assertTrue(vault.currentLosses() == currentLosses);
}
function _setupArbitraryState(
uint256 feeUnlockTime,
uint256 totalSupply,
uint256 balanceOf,
uint256 netLoans,
uint256 latestRepay,
uint256 currentProfits,
uint256 currentLosses
) internal returns (uint256, uint256) {
// Force fee unlock time to be within range
feeUnlockTime = Math.min((feeUnlockTime % (7 days)) + 30 seconds, 7 days);
vault.setFeeUnlockTime(feeUnlockTime);
// Set totalSupply by depositing
vm.prank(tokenSink);
token.transfer(depositor, totalSupply);
vm.startPrank(depositor);
token.approve(address(vault), totalSupply);
vault.deposit(totalSupply, receiver);
vm.stopPrank();
// Set latestRepay
vm.warp(latestRepay);
vault.repay(0, 0, repayer);
// Set currentProfits (assume this otherwise there are no tokens available)
currentProfits = Math.min(currentProfits, token.totalSupply() - totalSupply);
vm.prank(tokenSink);
token.transfer(repayer, currentProfits);
vm.prank(repayer);
token.approve(address(vault), currentProfits);
vault.repay(currentProfits, 0, repayer);
// Set currentLosses
vm.assume(currentLosses < vault.freeLiquidity());
vault.borrow(currentLosses, currentLosses, borrower);
vault.repay(0, currentLosses, repayer);
// Set netLoans
vm.assume(netLoans < vault.freeLiquidity());
vault.borrow(netLoans, netLoans, borrower);
// Set balanceOf by adjusting
vm.assume(balanceOf > 0); // Otherwise it fails due to unhealthy vault
uint256 initialBalance = token.balanceOf(address(vault));
// TODO: remove first assumption (the second is necessary because totalSupply is fixed)
vm.assume(balanceOf > initialBalance && balanceOf - initialBalance <= token.balanceOf(tokenSink));
vm.prank(tokenSink);
token.transfer(address(vault), balanceOf - initialBalance);
_originalStateCheck(
feeUnlockTime,
totalSupply,
balanceOf,
netLoans,
latestRepay,
currentProfits,
currentLosses
);
return (feeUnlockTime, currentProfits);
}
function testFeeUnlockTime(
uint256 feeUnlockTime,
uint256 totalSupply,
uint256 balanceOf,
uint256 netLoans,
uint256 latestRepay,
uint256 currentProfits,
uint256 currentLosses,
uint256 feeUnlockTimeSet
) public {
(feeUnlockTime, currentProfits) = _setupArbitraryState(
feeUnlockTime,
totalSupply,
balanceOf,
netLoans,
latestRepay,
currentProfits,
currentLosses
);
if (feeUnlockTimeSet < 30 || feeUnlockTimeSet > 7 days) {
vm.expectRevert(bytes4(keccak256(abi.encodePacked("FeeUnlockTimeOutOfRange()"))));
vault.setFeeUnlockTime(feeUnlockTimeSet);
} else {
vault.setFeeUnlockTime(feeUnlockTimeSet);
assertTrue(vault.feeUnlockTime() == feeUnlockTimeSet);
feeUnlockTime = feeUnlockTimeSet;
}
// Recheck the remaining state is unchanged (except feeUnlockTime but it's already reassigned)
_originalStateCheck(
feeUnlockTime,
totalSupply,
balanceOf,
netLoans,
latestRepay,
currentProfits,
currentLosses
);
}
function testSweep(
uint256 feeUnlockTime,
uint256 totalSupply,
uint256 balanceOf,
uint256 netLoans,
uint256 latestRepay,
uint256 currentProfits,
uint256 currentLosses,
uint256 spuriousAmount
) public {
(feeUnlockTime, currentProfits) = _setupArbitraryState(
feeUnlockTime,
totalSupply,
balanceOf,
netLoans,
latestRepay,
currentProfits,
currentLosses
);
spuriousToken.mint(address(vault), spuriousAmount);
assertTrue(spuriousToken.balanceOf(address(vault)) == spuriousAmount);
vault.sweep(anyAddress, address(spuriousToken));
assertTrue(spuriousToken.balanceOf(address(vault)) == 0);
assertTrue(spuriousToken.balanceOf(anyAddress) == spuriousAmount);
// Recheck the remaining state is unchanged (except feeUnlockTime)
_originalStateCheck(
feeUnlockTime,
totalSupply,
balanceOf,
netLoans,
latestRepay,
currentProfits,
currentLosses
);
}
function testDeposit(
uint256 feeUnlockTime,
uint256 totalSupply,
uint256 balanceOf,
uint256 netLoans,
uint256 latestRepay,
uint256 currentProfits,
uint256 currentLosses,
uint256 deposited
) public {
(feeUnlockTime, currentProfits) = _setupArbitraryState(
feeUnlockTime,
totalSupply,
balanceOf,
netLoans,
latestRepay,
currentProfits,
currentLosses
);
uint256 initialReceiverShares = vault.balanceOf(receiver);
uint256 initialDepositorShares = vault.balanceOf(depositor);
uint256 initialDepositorAssets = token.balanceOf(depositor);
uint256 initialVaultAssets = token.balanceOf(address(vault));
uint256 initialVaultTotalSupply = vault.totalSupply();
// Necessary assumption because token totalSupply is fixed
if (deposited > initialDepositorAssets) {
vm.assume(deposited - initialDepositorAssets < token.balanceOf(tokenSink));
vm.prank(tokenSink);
token.transfer(depositor, deposited - initialDepositorAssets);
}
vm.startPrank(depositor);
token.approve(address(vault), deposited);
uint256 shares = vault.deposit(deposited, receiver);
vm.stopPrank();
// Specific addresses' state changes
assertTrue(vault.balanceOf(address(receiver)) == initialReceiverShares + shares);
assertTrue(vault.balanceOf(address(depositor)) == initialDepositorShares);
// Vault state change
_originalStateCheck(
feeUnlockTime,
initialVaultTotalSupply + shares,
initialVaultAssets + deposited,
netLoans,
latestRepay,
currentProfits,
currentLosses
);
}
function testDepositWithPermit(uint256 amount) public {
SignUtils utils = new SignUtils(token.DOMAIN_SEPARATOR());
uint256 signerPrivateKey = 0xA11CE;
address signer = vm.addr(signerPrivateKey);
vm.deal(signer, 1 ether);
SignUtils.Permit memory permit = SignUtils.Permit({
owner: signer,
spender: address(vault),
value: amount,
nonce: 0,
deadline: 1 seconds
});
bytes32 digest = utils.getTypedDataHash(permit);
(uint8 v, bytes32 r, bytes32 s) = vm.sign(signerPrivateKey, digest);
deal({ token: address(token), to: signer, give: amount });
vm.prank(signer);
uint256 shares = vault.depositWithPermit(permit.value, receiver, permit.deadline, v, r, s);
assertTrue(vault.balanceOf(address(receiver)) == shares);
}
function testMint(
uint256 feeUnlockTime,
uint256 totalSupply,
uint256 balanceOf,
uint256 netLoans,
uint256 latestRepay,
uint256 currentProfits,
uint256 currentLosses,
uint256 minted
) public {
(feeUnlockTime, currentProfits) = _setupArbitraryState(
feeUnlockTime,
totalSupply,
balanceOf,
netLoans,
latestRepay,
currentProfits,
currentLosses
);
uint256 initialReceiverShares = vault.balanceOf(receiver);
uint256 initialDepositorShares = vault.balanceOf(depositor);
uint256 initialDepositorAssets = token.balanceOf(depositor);
uint256 initialVaultAssets = token.balanceOf(address(vault));
uint256 initialVaultTotalSupply = vault.totalSupply();
// Necessary to avoid overflow
if (vault.totalAssets() > 0 && vault.totalSupply() > 0)
vm.assume(minted / vault.totalSupply() < (type(uint256).max / vault.totalAssets()));
uint256 deposited = vault.previewMint(minted);
// Necessary assumption because token totalSupply is fixed
if (deposited > initialDepositorAssets) {
vm.assume(deposited - initialDepositorAssets < token.balanceOf(tokenSink));
vm.prank(tokenSink);
token.transfer(depositor, deposited - initialDepositorAssets);
}
vm.startPrank(depositor);
token.approve(address(vault), deposited);
vault.mint(minted, receiver);
vm.stopPrank();
// Specific addresses' state changes
assertTrue(vault.balanceOf(address(receiver)) == initialReceiverShares + minted);
assertTrue(vault.balanceOf(address(depositor)) == initialDepositorShares);
// Vault state change
_originalStateCheck(
feeUnlockTime,
initialVaultTotalSupply + minted,
initialVaultAssets + deposited,
netLoans,
latestRepay,
currentProfits,
currentLosses
);
}
function testWithdraw(
uint256 feeUnlockTime,
uint256 totalSupply,
uint256 balanceOf,
uint256 netLoans,
uint256 latestRepay,
uint256 currentProfits,
uint256 currentLosses,
uint256 withdrawn
) public {
(feeUnlockTime, currentProfits) = _setupArbitraryState(
feeUnlockTime,
totalSupply,
balanceOf,
netLoans,
latestRepay,
currentProfits,
currentLosses
);
// At this stage receiver has the entirety of the supply
assertTrue(vault.balanceOf(receiver) == totalSupply);
uint256 initialReceiverAssets = token.balanceOf(receiver);
uint256 initialReceiverShares = vault.balanceOf(receiver);
uint256 shares = 0;
vm.startPrank(receiver);
if (withdrawn >= vault.freeLiquidity()) {
vm.expectRevert(bytes4(keccak256(abi.encodePacked("InsufficientLiquidity()"))));
vault.withdraw(withdrawn, receiver, receiver);
withdrawn = 0;
}
shares = vault.withdraw(withdrawn, receiver, receiver);
assertTrue(vault.balanceOf(receiver) == initialReceiverShares - shares);
assertTrue(token.balanceOf(receiver) == initialReceiverAssets + withdrawn);
vm.stopPrank();
// Vault state change
_originalStateCheck(
feeUnlockTime,
totalSupply - shares,
balanceOf - withdrawn,
netLoans,
latestRepay,
currentProfits,
currentLosses
);
}
function testRedeem(
uint256 feeUnlockTime,
uint256 totalSupply,
uint256 balanceOf,
uint256 netLoans,
uint256 latestRepay,
uint256 currentProfits,
uint256 currentLosses,
uint256 redeemed
) public {
(feeUnlockTime, currentProfits) = _setupArbitraryState(
feeUnlockTime,
totalSupply,
balanceOf,
netLoans,
latestRepay,
currentProfits,
currentLosses
);
// At this stage receiver has the entirety of the supply
assertTrue(vault.balanceOf(receiver) == totalSupply);
vm.assume(redeemed < totalSupply);
uint256 initialReceiverAssets = token.balanceOf(receiver);
uint256 initialReceiverShares = vault.balanceOf(receiver);
uint256 withdrawn = vault.previewRedeem(redeemed);
vm.startPrank(receiver);
if (withdrawn >= vault.freeLiquidity()) {
vm.expectRevert(bytes4(keccak256(abi.encodePacked("InsufficientLiquidity()"))));
vault.redeem(redeemed, receiver, receiver);
redeemed = 0;
withdrawn = 0;
}
vault.redeem(redeemed, receiver, receiver);
assertTrue(vault.balanceOf(receiver) == initialReceiverShares - redeemed);
assertTrue(token.balanceOf(receiver) == initialReceiverAssets + withdrawn);
vm.stopPrank();
// Vault state change
_originalStateCheck(
feeUnlockTime,
totalSupply - redeemed,
balanceOf - withdrawn,
netLoans,
latestRepay,
currentProfits,
currentLosses
);
}
function testBorrow(
uint256 feeUnlockTime,
uint256 totalSupply,
uint256 balanceOf,
uint256 netLoans,
uint256 latestRepay,
uint256 currentProfits,
uint256 currentLosses,
uint256 borrowed,
uint256 loan
) public {
(feeUnlockTime, currentProfits) = _setupArbitraryState(
feeUnlockTime,
totalSupply,
balanceOf,
netLoans,
latestRepay,
currentProfits,
currentLosses
);
vm.assume(borrowed < vault.freeLiquidity());
uint256 initialBalance = token.balanceOf(receiver);
loan = borrowed == 0 ? 0 : loan % borrowed;
vault.borrow(borrowed, loan, receiver);
assertTrue(token.balanceOf(receiver) == initialBalance + borrowed);
_originalStateCheck(
feeUnlockTime,
totalSupply,
balanceOf - borrowed,
netLoans.safeAdd(loan),
latestRepay,
currentProfits,
currentLosses + (borrowed - loan)
);
}
function testRepay(
uint256 feeUnlockTime,
uint256 totalSupply,
uint256 balanceOf,
uint256 netLoans,
uint256 latestRepay,
uint256 currentProfits,
uint256 currentLosses,
uint256 timePast,
uint256 debt,
uint256 repaid
) public {
(feeUnlockTime, currentProfits) = _setupArbitraryState(
feeUnlockTime,
totalSupply,
balanceOf,
netLoans,
latestRepay,
currentProfits,
currentLosses
);
uint256 newTimestamp = latestRepay.safeAdd(timePast);
vm.warp(newTimestamp);
uint256 lockedProfits;
uint256 lockedLosses;
(lockedProfits, lockedLosses, , ) = vault.getFeeStatus();
lockedProfits = lockedProfits.mulDiv(
feeUnlockTime - Math.min(block.timestamp - latestRepay, feeUnlockTime),
feeUnlockTime
);
lockedLosses = lockedLosses.mulDiv(
feeUnlockTime - Math.min(block.timestamp - latestRepay, feeUnlockTime),
feeUnlockTime
);
if (repaid > token.balanceOf(repayer)) {
vm.assume(repaid - token.balanceOf(repayer) <= token.balanceOf(tokenSink));
vm.startPrank(tokenSink);
token.transfer(repayer, repaid - token.balanceOf(repayer));
vm.stopPrank();
}
if (debt > netLoans) debt = netLoans;
vm.startPrank(repayer);
token.approve(address(vault), repaid);
vm.stopPrank();
vault.repay(repaid, debt, repayer);
_originalStateCheck(
feeUnlockTime,
totalSupply,
balanceOf + repaid,
netLoans - debt,
newTimestamp,
lockedProfits + (debt < repaid ? repaid - debt : 0),
lockedLosses + (debt > repaid ? debt - repaid : 0)
);
}
function testCannotWithdrawMoreThanFreeLiquidity(uint256 amount) public {
vm.startPrank(tokenSink);
token.approve(address(vault), amount);
vault.deposit(amount, receiver);
vm.stopPrank();
// withdraw without leaving 1 token unit
uint256 vaultBalance = token.balanceOf(address(vault));
vm.expectRevert(IVault.InsufficientLiquidity.selector);
vault.withdraw(vaultBalance, tokenSink, receiver);
}
function testCannotBorrowMoreThanFreeLiquidity(uint256 amount) public {
vm.startPrank(tokenSink);
token.approve(address(vault), amount);
vault.deposit(amount, receiver);
vm.stopPrank();
uint256 vaultBalance = token.balanceOf(address(vault));
vm.expectRevert(IVault.InsufficientFreeLiquidity.selector);
vault.borrow(vaultBalance, vaultBalance, address(this));
}
}