-
Notifications
You must be signed in to change notification settings - Fork 45
/
ERC4626StataTokenUpgradeable.t.sol
518 lines (435 loc) · 18.6 KB
/
ERC4626StataTokenUpgradeable.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
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
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.10;
import {IERC20Errors} from 'openzeppelin-contracts/contracts/interfaces/draft-IERC6093.sol';
import {IERC20} from 'openzeppelin-contracts/contracts/token/ERC20/extensions/IERC20Metadata.sol';
import {IERC20Permit} from 'openzeppelin-contracts/contracts/token/ERC20/extensions/IERC20Permit.sol';
import {IPool} from '../../../src/contracts/interfaces/IPool.sol';
import {TestnetProcedures, TestnetERC20} from '../../utils/TestnetProcedures.sol';
import {ERC4626Upgradeable, ERC4626StataTokenUpgradeable, IERC4626StataToken} from '../../../src/contracts/extensions/stata-token/ERC4626StataTokenUpgradeable.sol';
import {DataTypes} from '../../../src/contracts/protocol/libraries/configuration/ReserveConfiguration.sol';
import {SigUtils} from '../../utils/SigUtils.sol';
// Minimal mock as contract is abstract
contract MockERC4626StataTokenUpgradeable is ERC4626StataTokenUpgradeable {
constructor(IPool pool) ERC4626StataTokenUpgradeable(pool) {}
function mockInit(address aToken) external initializer {
__ERC4626StataToken_init(aToken);
}
}
contract ERC4626StataTokenUpgradeableTest is TestnetProcedures {
MockERC4626StataTokenUpgradeable internal erc4626Upgradeable;
address internal underlying;
address internal aToken;
address public user;
uint256 internal userPrivateKey;
function setUp() public virtual {
initTestEnvironment(false);
userPrivateKey = 0xA11CE;
user = address(vm.addr(userPrivateKey));
DataTypes.ReserveDataLegacy memory reserveData = contracts.poolProxy.getReserveData(
tokenList.usdx
);
underlying = address(tokenList.usdx);
aToken = reserveData.aTokenAddress;
erc4626Upgradeable = new MockERC4626StataTokenUpgradeable(contracts.poolProxy);
erc4626Upgradeable.mockInit(address(reserveData.aTokenAddress));
}
function test_7201() external pure {
assertEq(
keccak256(abi.encode(uint256(keccak256('aave-dao.storage.ERC4626StataToken')) - 1)) &
~bytes32(uint256(0xff)),
0x55029d3f54709e547ed74b2fc842d93107ab1490ab7555dd9dd0bf6451101900
);
}
// ### GETTERS TESTS ###
function test_convertersAndPreviews(uint128 assets) public view {
uint256 shares = erc4626Upgradeable.convertToShares(assets);
assertEq(shares, erc4626Upgradeable.previewDeposit(assets));
assertEq(shares, erc4626Upgradeable.previewWithdraw(assets));
assertEq(erc4626Upgradeable.convertToAssets(shares), assets);
assertEq(erc4626Upgradeable.previewMint(shares), assets);
assertEq(erc4626Upgradeable.previewRedeem(shares), assets);
}
function test_totalAssets_shouldbeZeroOnZeroSupply() external view {
assertEq(erc4626Upgradeable.totalAssets(), 0);
}
// ### DEPOSIT TESTS ###
function test_depositATokens(
uint128 unboundedUnderlyingBalance,
uint256 unboundedAmountToDeposit,
address receiver
) public {
_validateReceiver(receiver);
TestEnv memory env = _setupTestEnv(unboundedUnderlyingBalance, unboundedAmountToDeposit);
_fundAToken(env.underlyingBalance, user);
vm.startPrank(user);
IERC20(aToken).approve(address(erc4626Upgradeable), env.amountToDeposit);
uint256 shares = erc4626Upgradeable.depositATokens(env.amountToDeposit, receiver);
vm.stopPrank();
assertEq(erc4626Upgradeable.balanceOf(receiver), shares);
assertEq(IERC20(aToken).balanceOf(address(erc4626Upgradeable)), env.actualAmountToDeposit);
assertEq(IERC20(aToken).balanceOf(user), env.underlyingBalance - env.actualAmountToDeposit);
assertEq(erc4626Upgradeable.totalAssets(), env.actualAmountToDeposit);
}
function test_depositATokens_self() external {
test_depositATokens(1 ether, 1 ether, user);
}
function test_deposit_shouldRevert_insufficientAllowance(uint128 unboundedAssets) external {
TestEnv memory env = _setupTestEnv(unboundedAssets);
_fundAToken(env.underlyingBalance, user);
vm.expectRevert(); // underflows
vm.prank(user);
erc4626Upgradeable.depositATokens(env.underlyingBalance, user);
}
function test_depositWithPermit_shouldRevert_emptyPermit_noPreApproval(
uint128 unboundedAssets
) external {
TestEnv memory env = _setupTestEnv(unboundedAssets);
_fundAToken(env.underlyingBalance, user);
IERC4626StataToken.SignatureParams memory sig;
vm.expectRevert(); // will underflow
vm.prank(user);
erc4626Upgradeable.depositWithPermit(
env.underlyingBalance,
user,
block.timestamp + 1000,
sig,
false
);
}
function test_depositWithPermit_emptyPermit_underlying_preApproval(
uint128 unboundedAssets,
uint256 unboundedAmountToDeposit,
address receiver
) external {
_validateReceiver(receiver);
TestEnv memory env = _setupTestEnv(unboundedAssets, unboundedAmountToDeposit);
_fundUnderlying(env.underlyingBalance, user);
IERC4626StataToken.SignatureParams memory sig;
vm.prank(user);
IERC20(underlying).approve(address(erc4626Upgradeable), env.amountToDeposit);
vm.prank(user);
uint256 shares = erc4626Upgradeable.depositWithPermit(
env.amountToDeposit,
receiver,
block.timestamp + 1000,
sig,
true
);
assertEq(erc4626Upgradeable.balanceOf(receiver), shares);
assertEq(IERC20(aToken).balanceOf(address(erc4626Upgradeable)), env.actualAmountToDeposit);
assertEq(IERC20(aToken).balanceOf(user), 0);
assertEq(IERC20(underlying).balanceOf(user), env.underlyingBalance - env.actualAmountToDeposit);
}
function test_depositWithPermit_emptyPermit_aToken_preApproval(
uint128 unboundedAssets,
uint256 unboundedAmountToDeposit,
address receiver
) external {
_validateReceiver(receiver);
TestEnv memory env = _setupTestEnv(unboundedAssets, unboundedAmountToDeposit);
_fundAToken(env.underlyingBalance, user);
IERC4626StataToken.SignatureParams memory sig;
vm.prank(user);
IERC20(aToken).approve(address(erc4626Upgradeable), env.amountToDeposit);
vm.prank(user);
uint256 shares = erc4626Upgradeable.depositWithPermit(
env.amountToDeposit,
receiver,
block.timestamp + 1000,
sig,
false
);
assertEq(erc4626Upgradeable.balanceOf(receiver), shares);
assertEq(IERC20(aToken).balanceOf(address(erc4626Upgradeable)), env.actualAmountToDeposit);
assertEq(IERC20(aToken).balanceOf(user), env.underlyingBalance - env.actualAmountToDeposit);
}
function test_depositWithPermit_underlying(
uint128 unboundedAssets,
uint256 unboundedAmountToDeposit,
address receiver
) external {
_validateReceiver(receiver);
TestEnv memory env = _setupTestEnv(unboundedAssets, unboundedAmountToDeposit);
_fundUnderlying(env.underlyingBalance, user);
SigUtils.Permit memory permit = SigUtils.Permit({
owner: user,
spender: address(erc4626Upgradeable),
value: env.amountToDeposit,
nonce: IERC20Permit(underlying).nonces(user),
deadline: block.timestamp + 100
});
bytes32 permitDigest = SigUtils.getTypedDataHash(
permit,
SigUtils.PERMIT_TYPEHASH,
IERC20Permit(underlying).DOMAIN_SEPARATOR()
);
(uint8 v, bytes32 r, bytes32 s) = vm.sign(userPrivateKey, permitDigest);
IERC4626StataToken.SignatureParams memory sig = IERC4626StataToken.SignatureParams(v, r, s);
vm.prank(user);
uint256 shares = erc4626Upgradeable.depositWithPermit(
env.amountToDeposit,
receiver,
permit.deadline,
sig,
true
);
assertEq(erc4626Upgradeable.balanceOf(receiver), shares);
assertEq(IERC20(aToken).balanceOf(address(erc4626Upgradeable)), env.actualAmountToDeposit);
assertEq(IERC20(underlying).balanceOf(user), env.underlyingBalance - env.actualAmountToDeposit);
}
function test_depositWithPermit_aToken(
uint128 unboundedAssets,
uint256 unboundedAmountToDeposit,
address receiver
) external {
_validateReceiver(receiver);
TestEnv memory env = _setupTestEnv(unboundedAssets, unboundedAmountToDeposit);
_fundAToken(env.underlyingBalance, user);
SigUtils.Permit memory permit = SigUtils.Permit({
owner: user,
spender: address(erc4626Upgradeable),
value: env.amountToDeposit,
nonce: IERC20Permit(aToken).nonces(user),
deadline: block.timestamp + 100
});
bytes32 permitDigest = SigUtils.getTypedDataHash(
permit,
SigUtils.PERMIT_TYPEHASH,
IERC20Permit(aToken).DOMAIN_SEPARATOR()
);
(uint8 v, bytes32 r, bytes32 s) = vm.sign(userPrivateKey, permitDigest);
IERC4626StataToken.SignatureParams memory sig = IERC4626StataToken.SignatureParams(v, r, s);
vm.prank(user);
uint256 shares = erc4626Upgradeable.depositWithPermit(
env.amountToDeposit,
receiver,
permit.deadline,
sig,
false
);
assertEq(erc4626Upgradeable.balanceOf(receiver), shares);
assertEq(IERC20(aToken).balanceOf(address(erc4626Upgradeable)), env.actualAmountToDeposit);
assertEq(IERC20(aToken).balanceOf(user), env.underlyingBalance - env.actualAmountToDeposit);
}
// ### REDEEM TESTS ###
function test_redeemATokens(uint256 assets, address receiver) public {
_validateReceiver(receiver);
TestEnv memory env = _setupTestEnv(assets);
uint256 shares = _fund4626(env.underlyingBalance, user);
vm.prank(user);
uint256 redeemedAssets = erc4626Upgradeable.redeemATokens(shares, receiver, user);
assertEq(erc4626Upgradeable.balanceOf(user), 0);
assertEq(IERC20(aToken).balanceOf(receiver), redeemedAssets);
}
function test_redeemATokens_onBehalf_shouldRevert_insufficientAllowance(
uint256 assets,
uint256 allowance
) external {
TestEnv memory env = _setupTestEnv(assets);
uint256 shares = _fund4626(env.underlyingBalance, user);
allowance = bound(allowance, 0, shares - 1);
vm.prank(user);
erc4626Upgradeable.approve(address(this), allowance);
vm.expectRevert(
abi.encodeWithSelector(
IERC20Errors.ERC20InsufficientAllowance.selector,
address(this),
allowance,
env.underlyingBalance
)
);
erc4626Upgradeable.redeemATokens(env.underlyingBalance, address(this), user);
}
function test_redeemATokens_onBehalf(uint256 assets) external {
TestEnv memory env = _setupTestEnv(assets);
uint256 shares = _fund4626(env.underlyingBalance, user);
vm.prank(user);
erc4626Upgradeable.approve(address(this), shares);
uint256 redeemedAssets = erc4626Upgradeable.redeemATokens(shares, address(this), user);
assertEq(erc4626Upgradeable.balanceOf(user), 0);
assertEq(IERC20(aToken).balanceOf(address(this)), redeemedAssets);
}
function test_redeem(uint256 assets, address receiver) external {
_validateReceiver(receiver);
TestEnv memory env = _setupTestEnv(assets);
uint256 shares = _fund4626(env.underlyingBalance, user);
vm.prank(user);
uint256 redeemedAssets = erc4626Upgradeable.redeem(shares, receiver, user);
assertEq(erc4626Upgradeable.balanceOf(user), 0);
assertLe(IERC20(underlying).balanceOf(receiver), redeemedAssets);
}
// ### withdraw TESTS ###
function test_withdraw(uint256 assets, address receiver) public {
_validateReceiver(receiver);
TestEnv memory env = _setupTestEnv(assets);
uint256 shares = _fund4626(env.underlyingBalance, user);
vm.prank(user);
uint256 withdrawnShares = erc4626Upgradeable.withdraw(env.underlyingBalance, receiver, user);
assertEq(withdrawnShares, shares);
assertEq(erc4626Upgradeable.balanceOf(user), 0);
assertLe(IERC20(underlying).balanceOf(receiver), env.underlyingBalance);
assertApproxEqAbs(IERC20(underlying).balanceOf(receiver), env.underlyingBalance, 1);
}
function test_withdraw_shouldRevert_moreThenAvailable(uint256 assets, address receiver) public {
_validateReceiver(receiver);
TestEnv memory env = _setupTestEnv(assets);
_fund4626(env.underlyingBalance, user);
vm.prank(user);
vm.expectRevert(
abi.encodeWithSelector(
ERC4626Upgradeable.ERC4626ExceededMaxWithdraw.selector,
address(user),
env.underlyingBalance + 1,
env.underlyingBalance
)
);
erc4626Upgradeable.withdraw(env.underlyingBalance + 1, receiver, user);
}
// ### mint TESTS ###
function test_mint(uint256 assets, address receiver) public {
_validateReceiver(receiver);
TestEnv memory env = _setupTestEnv(assets);
_fundUnderlying(env.underlyingBalance, user);
vm.startPrank(user);
IERC20(underlying).approve(address(erc4626Upgradeable), env.underlyingBalance);
uint256 shares = erc4626Upgradeable.previewDeposit(env.underlyingBalance);
uint256 assetsUsedForMinting = erc4626Upgradeable.mint(shares, receiver);
assertEq(assetsUsedForMinting, env.underlyingBalance);
assertEq(erc4626Upgradeable.balanceOf(receiver), shares);
}
function test_mint_shouldRevert_mintMoreThenBalance(uint256 assets, address receiver) public {
_validateReceiver(receiver);
TestEnv memory env = _setupTestEnv(assets);
_fundUnderlying(env.underlyingBalance, user);
vm.startPrank(user);
IERC20(underlying).approve(address(erc4626Upgradeable), type(uint256).max);
uint256 shares = erc4626Upgradeable.previewDeposit(env.underlyingBalance);
vm.expectRevert();
erc4626Upgradeable.mint(shares + 1, receiver);
}
// ### maxDeposit TESTS ###
function test_maxDeposit_freeze() public {
vm.prank(roleList.marketOwner);
contracts.poolConfiguratorProxy.setReserveFreeze(underlying, true);
uint256 max = erc4626Upgradeable.maxDeposit(address(0));
assertEq(max, 0);
}
function test_maxDeposit_paused() public {
vm.prank(address(roleList.marketOwner));
contracts.poolConfiguratorProxy.setReservePause(underlying, true);
uint256 max = erc4626Upgradeable.maxDeposit(address(0));
assertEq(max, 0);
}
function test_maxDeposit_noCap() public {
vm.prank(address(roleList.marketOwner));
contracts.poolConfiguratorProxy.setSupplyCap(underlying, 0);
uint256 maxDeposit = erc4626Upgradeable.maxDeposit(address(0));
uint256 maxMint = erc4626Upgradeable.maxMint(address(0));
assertEq(maxDeposit, type(uint256).max);
assertEq(maxMint, type(uint256).max);
}
function test_maxDeposit_cap(uint256 cap) public {
cap = bound(cap, 1, type(uint32).max);
vm.prank(address(roleList.marketOwner));
contracts.poolConfiguratorProxy.setSupplyCap(underlying, cap);
uint256 max = erc4626Upgradeable.maxDeposit(address(0));
assertEq(max, cap * 10 ** erc4626Upgradeable.decimals());
}
// TODO: perhaps makes sense to add maxDeposit test with accruedToTreasury etc
// ### maxRedeem TESTS ###
function test_maxRedeem_paused(uint128 assets) public {
TestEnv memory env = _setupTestEnv(assets);
_fund4626(env.underlyingBalance, user);
vm.prank(address(roleList.marketOwner));
contracts.poolConfiguratorProxy.setReservePause(underlying, true);
uint256 max = erc4626Upgradeable.maxRedeem(address(user));
assertEq(max, 0);
}
function test_maxRedeem_sufficientAvailableLiquidity(uint128 assets) public {
TestEnv memory env = _setupTestEnv(assets);
uint256 shares = _fund4626(env.underlyingBalance, user);
uint256 max = erc4626Upgradeable.maxRedeem(address(user));
assertEq(max, shares);
}
function test_maxRedeem_inSufficientAvailableLiquidity(uint256 amountToBorrow) public {
uint128 assets = 1e8;
amountToBorrow = bound(amountToBorrow, 1, assets);
_fund4626(assets, user);
// borrow out some assets
address borrowUser = address(99);
vm.startPrank(borrowUser);
deal(address(weth), borrowUser, 2_000 ether);
weth.approve(address(contracts.poolProxy), 2_000 ether);
contracts.poolProxy.deposit(address(weth), 2_000 ether, borrowUser, 0);
contracts.poolProxy.borrow(underlying, amountToBorrow, 2, 0, borrowUser);
uint256 max = erc4626Upgradeable.maxRedeem(address(user));
assertEq(max, erc4626Upgradeable.previewRedeem(assets - amountToBorrow));
}
// ### lastestAnswer TESTS ###
function test_latestAnswer_priceShouldBeEqualOnDefaultIndex() public {
vm.mockCall(
address(contracts.poolProxy),
abi.encodeWithSelector(IPool.getReserveNormalizedIncome.selector),
abi.encode(1e27)
);
uint256 stataPrice = uint256(erc4626Upgradeable.latestAnswer());
uint256 underlyingPrice = contracts.aaveOracle.getAssetPrice(underlying);
assertEq(stataPrice, underlyingPrice);
}
function test_latestAnswer_priceShouldReflectIndexAccrual(uint256 liquidityIndex) public {
liquidityIndex = bound(liquidityIndex, 1e27, 1e29);
vm.mockCall(
address(contracts.poolProxy),
abi.encodeWithSelector(IPool.getReserveNormalizedIncome.selector),
abi.encode(liquidityIndex)
);
uint256 stataPrice = uint256(erc4626Upgradeable.latestAnswer());
uint256 underlyingPrice = contracts.aaveOracle.getAssetPrice(underlying);
uint256 expectedStataPrice = (underlyingPrice * liquidityIndex) / 1e27;
assertEq(stataPrice, expectedStataPrice);
// reverse the math to ensure precision loss is within bounds
uint256 reversedUnderlying = (stataPrice * 1e27) / liquidityIndex;
assertApproxEqAbs(underlyingPrice, reversedUnderlying, 1);
}
struct TestEnv {
uint256 underlyingBalance;
uint256 amountToDeposit;
uint256 actualAmountToDeposit;
}
function _validateReceiver(address receiver) internal view {
vm.assume(receiver != address(0) && receiver != address(aToken));
}
function _setupTestEnv(
uint256 underlyingBalance,
uint256 amountToDeposit
) internal pure returns (TestEnv memory) {
TestEnv memory env;
env.underlyingBalance = bound(underlyingBalance, 1, type(uint96).max);
env.amountToDeposit = bound(amountToDeposit, 1, type(uint256).max);
env.actualAmountToDeposit = env.amountToDeposit > env.underlyingBalance
? env.underlyingBalance
: env.amountToDeposit;
return env;
}
function _setupTestEnv(uint256 underlyingBalance) internal pure returns (TestEnv memory) {
return _setupTestEnv(underlyingBalance, underlyingBalance);
}
function _fundUnderlying(uint256 assets, address receiver) internal {
deal(underlying, receiver, assets);
}
function _fundAToken(uint256 assets, address receiver) internal {
_fundUnderlying(assets, receiver);
vm.startPrank(receiver);
IERC20(underlying).approve(address(contracts.poolProxy), assets);
contracts.poolProxy.deposit(underlying, assets, receiver, 0);
vm.stopPrank();
}
function _fund4626(uint256 assets, address receiver) internal returns (uint256) {
_fundAToken(assets, receiver);
vm.startPrank(receiver);
IERC20(aToken).approve(address(erc4626Upgradeable), assets);
uint256 shares = erc4626Upgradeable.depositATokens(assets, receiver);
vm.stopPrank();
return shares;
}
}