-
Notifications
You must be signed in to change notification settings - Fork 26
/
ERC4626.test.sol
349 lines (300 loc) · 12.4 KB
/
ERC4626.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
// SPDX-License-Identifier: AGPL-3.0
pragma solidity >=0.8.0 <0.9.0;
import "./ERC4626.prop.sol";
interface IMockERC20 is IERC20 {
function mint(address to, uint value) external;
function burn(address from, uint value) external;
}
abstract contract ERC4626Test is ERC4626Prop {
function setUp() public virtual;
uint constant N = 4;
struct Init {
address[N] user;
uint[N] share;
uint[N] asset;
int yield;
}
// setup initial vault state as follows:
//
// totalAssets == sum(init.share) + init.yield
// totalShares == sum(init.share)
//
// init.user[i]'s assets == init.asset[i]
// init.user[i]'s shares == init.share[i]
function setUpVault(Init memory init) public virtual {
// setup initial shares and assets for individual users
for (uint i = 0; i < N; i++) {
address user = init.user[i];
vm.assume(_isEOA(user));
// shares
uint shares = init.share[i];
try IMockERC20(_underlying_).mint(user, shares) {} catch { vm.assume(false); }
_approve(_underlying_, user, _vault_, shares);
vm.prank(user); try IERC4626(_vault_).deposit(shares, user) {} catch { vm.assume(false); }
// assets
uint assets = init.asset[i];
try IMockERC20(_underlying_).mint(user, assets) {} catch { vm.assume(false); }
}
// setup initial yield for vault
setUpYield(init);
}
// setup initial yield
function setUpYield(Init memory init) public virtual {
if (init.yield >= 0) { // gain
uint gain = uint(init.yield);
try IMockERC20(_underlying_).mint(_vault_, gain) {} catch { vm.assume(false); } // this can be replaced by calling yield generating functions if provided by the vault
} else { // loss
vm.assume(init.yield > type(int).min); // avoid overflow in conversion
uint loss = uint(-1 * init.yield);
try IMockERC20(_underlying_).burn(_vault_, loss) {} catch { vm.assume(false); } // this can be replaced by calling yield generating functions if provided by the vault
}
}
//
// asset
//
function test_asset(Init memory init) public virtual {
setUpVault(init);
address caller = init.user[0];
prop_asset(caller);
}
function test_totalAssets(Init memory init) public virtual {
setUpVault(init);
address caller = init.user[0];
prop_totalAssets(caller);
}
//
// convert
//
function test_convertToShares(Init memory init, uint assets) public virtual {
setUpVault(init);
address caller1 = init.user[0];
address caller2 = init.user[1];
prop_convertToShares(caller1, caller2, assets);
}
function test_convertToAssets(Init memory init, uint shares) public virtual {
setUpVault(init);
address caller1 = init.user[0];
address caller2 = init.user[1];
prop_convertToAssets(caller1, caller2, shares);
}
//
// deposit
//
function test_maxDeposit(Init memory init) public virtual {
setUpVault(init);
address caller = init.user[0];
address receiver = init.user[1];
prop_maxDeposit(caller, receiver);
}
function test_previewDeposit(Init memory init, uint assets) public virtual {
setUpVault(init);
address caller = init.user[0];
address receiver = init.user[1];
address other = init.user[2];
assets = bound(assets, 0, _max_deposit(caller));
_approve(_underlying_, caller, _vault_, type(uint).max);
prop_previewDeposit(caller, receiver, other, assets);
}
function test_deposit(Init memory init, uint assets, uint allowance) public virtual {
setUpVault(init);
address caller = init.user[0];
address receiver = init.user[1];
assets = bound(assets, 0, _max_deposit(caller));
_approve(_underlying_, caller, _vault_, allowance);
prop_deposit(caller, receiver, assets);
}
//
// mint
//
function test_maxMint(Init memory init) public virtual {
setUpVault(init);
address caller = init.user[0];
address receiver = init.user[1];
prop_maxMint(caller, receiver);
}
function test_previewMint(Init memory init, uint shares) public virtual {
setUpVault(init);
address caller = init.user[0];
address receiver = init.user[1];
address other = init.user[2];
shares = bound(shares, 0, _max_mint(caller));
_approve(_underlying_, caller, _vault_, type(uint).max);
prop_previewMint(caller, receiver, other, shares);
}
function test_mint(Init memory init, uint shares, uint allowance) public virtual {
setUpVault(init);
address caller = init.user[0];
address receiver = init.user[1];
shares = bound(shares, 0, _max_mint(caller));
_approve(_underlying_, caller, _vault_, allowance);
prop_mint(caller, receiver, shares);
}
//
// withdraw
//
function test_maxWithdraw(Init memory init) public virtual {
setUpVault(init);
address caller = init.user[0];
address owner = init.user[1];
prop_maxWithdraw(caller, owner);
}
function test_previewWithdraw(Init memory init, uint assets) public virtual {
setUpVault(init);
address caller = init.user[0];
address receiver = init.user[1];
address owner = init.user[2];
address other = init.user[3];
assets = bound(assets, 0, _max_withdraw(owner));
_approve(_vault_, owner, caller, type(uint).max);
prop_previewWithdraw(caller, receiver, owner, other, assets);
}
function test_withdraw(Init memory init, uint assets, uint allowance) public virtual {
setUpVault(init);
address caller = init.user[0];
address receiver = init.user[1];
address owner = init.user[2];
assets = bound(assets, 0, _max_withdraw(owner));
_approve(_vault_, owner, caller, allowance);
prop_withdraw(caller, receiver, owner, assets);
}
function testFail_withdraw(Init memory init, uint assets) public virtual {
setUpVault(init);
address caller = init.user[0];
address receiver = init.user[1];
address owner = init.user[2];
assets = bound(assets, 0, _max_withdraw(owner));
vm.assume(caller != owner);
vm.assume(assets > 0);
_approve(_vault_, owner, caller, 0);
vm.prank(caller); uint shares = IERC4626(_vault_).withdraw(assets, receiver, owner);
assertGt(shares, 0); // this assert is expected to fail
}
//
// redeem
//
function test_maxRedeem(Init memory init) public virtual {
setUpVault(init);
address caller = init.user[0];
address owner = init.user[1];
prop_maxRedeem(caller, owner);
}
function test_previewRedeem(Init memory init, uint shares) public virtual {
setUpVault(init);
address caller = init.user[0];
address receiver = init.user[1];
address owner = init.user[2];
address other = init.user[3];
shares = bound(shares, 0, _max_redeem(owner));
_approve(_vault_, owner, caller, type(uint).max);
prop_previewRedeem(caller, receiver, owner, other, shares);
}
function test_redeem(Init memory init, uint shares, uint allowance) public virtual {
setUpVault(init);
address caller = init.user[0];
address receiver = init.user[1];
address owner = init.user[2];
shares = bound(shares, 0, _max_redeem(owner));
_approve(_vault_, owner, caller, allowance);
prop_redeem(caller, receiver, owner, shares);
}
function testFail_redeem(Init memory init, uint shares) public virtual {
setUpVault(init);
address caller = init.user[0];
address receiver = init.user[1];
address owner = init.user[2];
shares = bound(shares, 0, _max_redeem(owner));
vm.assume(caller != owner);
vm.assume(shares > 0);
_approve(_vault_, owner, caller, 0);
vm.prank(caller); IERC4626(_vault_).redeem(shares, receiver, owner);
}
//
// round trip tests
//
function test_RT_deposit_redeem(Init memory init, uint assets) public virtual {
setUpVault(init);
address caller = init.user[0];
assets = bound(assets, 0, _max_deposit(caller));
_approve(_underlying_, caller, _vault_, type(uint).max);
prop_RT_deposit_redeem(caller, assets);
}
function test_RT_deposit_withdraw(Init memory init, uint assets) public virtual {
setUpVault(init);
address caller = init.user[0];
assets = bound(assets, 0, _max_deposit(caller));
_approve(_underlying_, caller, _vault_, type(uint).max);
prop_RT_deposit_withdraw(caller, assets);
}
function test_RT_redeem_deposit(Init memory init, uint shares) public virtual {
setUpVault(init);
address caller = init.user[0];
shares = bound(shares, 0, _max_redeem(caller));
_approve(_underlying_, caller, _vault_, type(uint).max);
prop_RT_redeem_deposit(caller, shares);
}
function test_RT_redeem_mint(Init memory init, uint shares) public virtual {
setUpVault(init);
address caller = init.user[0];
shares = bound(shares, 0, _max_redeem(caller));
_approve(_underlying_, caller, _vault_, type(uint).max);
prop_RT_redeem_mint(caller, shares);
}
function test_RT_mint_withdraw(Init memory init, uint shares) public virtual {
setUpVault(init);
address caller = init.user[0];
shares = bound(shares, 0, _max_mint(caller));
_approve(_underlying_, caller, _vault_, type(uint).max);
prop_RT_mint_withdraw(caller, shares);
}
function test_RT_mint_redeem(Init memory init, uint shares) public virtual {
setUpVault(init);
address caller = init.user[0];
shares = bound(shares, 0, _max_mint(caller));
_approve(_underlying_, caller, _vault_, type(uint).max);
prop_RT_mint_redeem(caller, shares);
}
function test_RT_withdraw_mint(Init memory init, uint assets) public virtual {
setUpVault(init);
address caller = init.user[0];
assets = bound(assets, 0, _max_withdraw(caller));
_approve(_underlying_, caller, _vault_, type(uint).max);
prop_RT_withdraw_mint(caller, assets);
}
function test_RT_withdraw_deposit(Init memory init, uint assets) public virtual {
setUpVault(init);
address caller = init.user[0];
assets = bound(assets, 0, _max_withdraw(caller));
_approve(_underlying_, caller, _vault_, type(uint).max);
prop_RT_withdraw_deposit(caller, assets);
}
//
// utils
//
function _isContract(address account) internal view returns (bool) { return account.code.length > 0; }
function _isEOA (address account) internal view returns (bool) { return account.code.length == 0; }
function _approve(address token, address owner, address spender, uint amount) internal {
vm.prank(owner); _safeApprove(token, spender, 0);
vm.prank(owner); _safeApprove(token, spender, amount);
}
function _safeApprove(address token, address spender, uint amount) internal {
(bool success, bytes memory retdata) = token.call(abi.encodeWithSelector(IERC20.approve.selector, spender, amount));
vm.assume(success);
if (retdata.length > 0) vm.assume(abi.decode(retdata, (bool)));
}
function _max_deposit(address from) internal virtual returns (uint) {
if (_unlimitedAmount) return type(uint).max;
return IERC20(_underlying_).balanceOf(from);
}
function _max_mint(address from) internal virtual returns (uint) {
if (_unlimitedAmount) return type(uint).max;
return vault_convertToShares(IERC20(_underlying_).balanceOf(from));
}
function _max_withdraw(address from) internal virtual returns (uint) {
if (_unlimitedAmount) return type(uint).max;
return vault_convertToAssets(IERC20(_vault_).balanceOf(from)); // may be different from maxWithdraw(from)
}
function _max_redeem(address from) internal virtual returns (uint) {
if (_unlimitedAmount) return type(uint).max;
return IERC20(_vault_).balanceOf(from); // may be different from maxRedeem(from)
}
}