-
Notifications
You must be signed in to change notification settings - Fork 411
/
Copy pathIntegrationBase.t.sol
357 lines (291 loc) · 13.2 KB
/
IntegrationBase.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
// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.12;
import "forge-std/Test.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
import "src/test/integration/IntegrationDeployer.t.sol";
import "src/test/integration/TimeMachine.t.sol";
import "src/test/integration/User.sol";
abstract contract IntegrationBase is IntegrationDeployer {
/**
* Gen/Init methods:
*/
/**
* @dev Create a new user according to configured random variants.
* This user is ready to deposit into some strategies and has some underlying token balances
*/
function _newStaker() internal returns (User, IStrategy[] memory, uint[] memory) {
(User staker, IStrategy[] memory strategies, uint[] memory tokenBalances) = _randUser();
assert_HasUnderlyingTokenBalances(staker, strategies, tokenBalances, "_newStaker: failed to award token balances");
return (staker, strategies, tokenBalances);
}
function _newOperator() internal returns (User, IStrategy[] memory, uint[] memory) {
(User operator, IStrategy[] memory strategies, uint[] memory tokenBalances) = _randUser();
operator.registerAsOperator();
operator.depositIntoEigenlayer(strategies, tokenBalances);
assert_Snap_AddedStakerShares(operator, strategies, tokenBalances, "_newOperator: failed to add delegatable shares");
assert_Snap_AddedOperatorShares(operator, strategies, tokenBalances, "_newOperator: failed to award shares to operator");
assertTrue(delegationManager.isOperator(address(operator)), "_newOperator: operator should be registered");
return (operator, strategies, tokenBalances);
}
/**
* Common assertions:
*/
function assert_HasNoDelegatableShares(User user, string memory err) internal {
(IStrategy[] memory strategies, uint[] memory shares) =
delegationManager.getDelegatableShares(address(user));
assertEq(strategies.length, 0, err);
assertEq(strategies.length, shares.length, "assert_HasNoDelegatableShares: return length mismatch");
}
function assert_HasUnderlyingTokenBalances(
User user,
IStrategy[] memory strategies,
uint[] memory expectedBalances,
string memory err
) internal {
for (uint i = 0; i < strategies.length; i++) {
IStrategy strat = strategies[i];
uint expectedBalance = expectedBalances[i];
uint tokenBalance;
if (strat == BEACONCHAIN_ETH_STRAT) {
tokenBalance = address(user).balance;
} else {
tokenBalance = strat.underlyingToken().balanceOf(address(user));
}
assertEq(expectedBalance, tokenBalance, err);
}
}
function assert_HasNoUnderlyingTokenBalance(User user, IStrategy[] memory strategies, string memory err) internal {
assert_HasUnderlyingTokenBalances(user, strategies, new uint[](strategies.length), err);
}
function assert_HasExpectedShares(
User user,
IStrategy[] memory strategies,
uint[] memory expectedShares,
string memory err
) internal {
for (uint i = 0; i < strategies.length; i++) {
IStrategy strat = strategies[i];
uint actualShares;
if (strat == BEACONCHAIN_ETH_STRAT) {
// TODO
// actualShares = eigenPodManager.podOwnerShares(address(user));
revert("unimplemented");
} else {
actualShares = strategyManager.stakerStrategyShares(address(user), strat);
}
assertEq(expectedShares[i], actualShares, err);
}
}
function assert_HasOperatorShares(
User user,
IStrategy[] memory strategies,
uint[] memory expectedShares,
string memory err
) internal {
for (uint i = 0; i < strategies.length; i++) {
IStrategy strat = strategies[i];
uint actualShares = delegationManager.operatorShares(address(user), strat);
assertEq(expectedShares[i], actualShares, err);
}
}
function assert_AllWithdrawalsPending(bytes32[] memory withdrawalRoots, string memory err) internal {
for (uint i = 0; i < withdrawalRoots.length; i++) {
assertTrue(delegationManager.pendingWithdrawals(withdrawalRoots[i]), err);
}
}
function assert_ValidWithdrawalHashes(
IDelegationManager.Withdrawal[] memory withdrawals,
bytes32[] memory withdrawalRoots,
string memory err
) internal {
for (uint i = 0; i < withdrawals.length; i++) {
assertEq(withdrawalRoots[i], delegationManager.calculateWithdrawalRoot(withdrawals[i]), err);
}
}
/**
* Snapshot assertions combine Timemachine's snapshots with assertions
* that allow easy comparisons between prev/cur values
*/
/// @dev Check that the operator has `addedShares` additional shares for each
/// strategy since the last snapshot
function assert_Snap_AddedOperatorShares(
User operator,
IStrategy[] memory strategies,
uint[] memory addedShares,
string memory err
) internal {
uint[] memory curShares = _getOperatorShares(operator, strategies);
// Use timewarp to get previous operator shares
uint[] memory prevShares = _getPrevOperatorShares(operator, strategies);
// For each strategy, check (prev + added == cur)
for (uint i = 0; i < strategies.length; i++) {
assertEq(prevShares[i] + addedShares[i], curShares[i], err);
}
}
/// @dev Check that the operator has `removedShares` prior shares for each
/// strategy since the last snapshot
function assert_Snap_RemovedOperatorShares(
User operator,
IStrategy[] memory strategies,
uint[] memory removedShares,
string memory err
) internal {
uint[] memory curShares = _getOperatorShares(operator, strategies);
// Use timewarp to get previous operator shares
uint[] memory prevShares = _getPrevOperatorShares(operator, strategies);
// For each strategy, check (prev - removed == cur)
for (uint i = 0; i < strategies.length; i++) {
assertEq(prevShares[i] - removedShares[i], curShares[i], err);
}
}
/// @dev Check that the staker has `addedShares` additional shares for each
/// strategy since the last snapshot
function assert_Snap_AddedStakerShares(
User staker,
IStrategy[] memory strategies,
uint[] memory addedShares,
string memory err
) internal {
uint[] memory curShares = _getStakerShares(staker, strategies);
// Use timewarp to get previous staker shares
uint[] memory prevShares = _getPrevStakerShares(staker, strategies);
// For each strategy, check (prev + added == cur)
for (uint i = 0; i < strategies.length; i++) {
assertEq(prevShares[i] + addedShares[i], curShares[i], err);
}
}
/// @dev Check that the staker has `removedShares` prior shares for each
/// strategy since the last snapshot
function assert_Snap_RemovedStakerShares(
User staker,
IStrategy[] memory strategies,
uint[] memory removedShares,
string memory err
) internal {
uint[] memory curShares = _getStakerShares(staker, strategies);
// Use timewarp to get previous staker shares
uint[] memory prevShares = _getPrevStakerShares(staker, strategies);
// For each strategy, check (prev - removed == cur)
for (uint i = 0; i < strategies.length; i++) {
assertEq(prevShares[i] - removedShares[i], curShares[i], err);
}
}
function assert_Snap_IncreasedQueuedWithdrawals(
User staker,
IDelegationManager.Withdrawal[] memory withdrawals,
string memory err
) internal {
uint curQueuedWithdrawals = _getCumulativeWithdrawals(staker);
// Use timewarp to get previous cumulative withdrawals
uint prevQueuedWithdrawals = _getPrevCumulativeWithdrawals(staker);
assertEq(prevQueuedWithdrawals + withdrawals.length, curQueuedWithdrawals, err);
}
function assert_Snap_IncreasedTokenBalances(
User staker,
IERC20[] memory tokens,
uint[] memory addedTokens,
string memory err
) internal {
uint[] memory curTokenBalances = _getTokenBalances(staker, tokens);
// Use timewarp to get previous token balances
uint[] memory prevTokenBalances = _getPrevTokenBalances(staker, tokens);
for (uint i = 0; i < tokens.length; i++) {
uint prevBalance = prevTokenBalances[i];
uint curBalance = curTokenBalances[i];
assertEq(prevBalance + addedTokens[i], curBalance, err);
}
}
/**
* Helpful getters:
*/
/// @dev For some strategies/underlying token balances, calculate the expected shares received
/// from depositing all tokens
function _calculateExpectedShares(IStrategy[] memory strategies, uint[] memory tokenBalances) internal returns (uint[] memory) {
uint[] memory expectedShares = new uint[](strategies.length);
for (uint i = 0; i < strategies.length; i++) {
IStrategy strat = strategies[i];
uint tokenBalance = tokenBalances[i];
if (strat == BEACONCHAIN_ETH_STRAT) {
// TODO - need to calculate this
// expectedShares[i] = eigenPodManager.underlyingToShares(tokenBalance);
revert("_calculateExpectedShares: unimplemented for native eth");
} else {
expectedShares[i] = strat.underlyingToShares(tokenBalance);
}
}
return expectedShares;
}
/// @dev For some strategies/underlying token balances, calculate the expected shares received
/// from depositing all tokens
function _calculateExpectedTokens(IStrategy[] memory strategies, uint[] memory shares) internal returns (uint[] memory) {
uint[] memory expectedTokens = new uint[](strategies.length);
for (uint i = 0; i < strategies.length; i++) {
IStrategy strat = strategies[i];
if (strat == BEACONCHAIN_ETH_STRAT) {
// TODO - need to calculate this
// expectedTokens[i] = eigenPodManager.underlyingToShares(tokenBalance);
revert("_calculateExpectedShares: unimplemented for native eth");
} else {
expectedTokens[i] = strat.sharesToUnderlying(shares[i]);
}
}
return expectedTokens;
}
modifier timewarp() {
uint curState = timeMachine.warpToLast();
_;
timeMachine.warpToPresent(curState);
}
/// @dev Uses timewarp modifier to get operator shares at the last snapshot
function _getPrevOperatorShares(
User operator,
IStrategy[] memory strategies
) internal timewarp() returns (uint[] memory) {
return _getOperatorShares(operator, strategies);
}
/// @dev Looks up each strategy and returns a list of the operator's shares
function _getOperatorShares(User operator, IStrategy[] memory strategies) internal view returns (uint[] memory) {
uint[] memory curShares = new uint[](strategies.length);
for (uint i = 0; i < strategies.length; i++) {
curShares[i] = delegationManager.operatorShares(address(operator), strategies[i]);
}
return curShares;
}
/// @dev Uses timewarp modifier to get staker shares at the last snapshot
function _getPrevStakerShares(
User staker,
IStrategy[] memory strategies
) internal timewarp() returns (uint[] memory) {
return _getStakerShares(staker, strategies);
}
/// @dev Looks up each strategy and returns a list of the staker's shares
function _getStakerShares(User staker, IStrategy[] memory strategies) internal view returns (uint[] memory) {
uint[] memory curShares = new uint[](strategies.length);
for (uint i = 0; i < strategies.length; i++) {
IStrategy strat = strategies[i];
if (strat == BEACONCHAIN_ETH_STRAT) {
// curShares[i] = eigenPodManager.podOwnerShares(address(staker));
revert("TODO: unimplemented");
} else {
curShares[i] = strategyManager.stakerStrategyShares(address(staker), strat);
}
}
return curShares;
}
function _getPrevCumulativeWithdrawals(User staker) internal timewarp() returns (uint) {
return _getCumulativeWithdrawals(staker);
}
function _getCumulativeWithdrawals(User staker) internal view returns (uint) {
return delegationManager.cumulativeWithdrawalsQueued(address(staker));
}
function _getPrevTokenBalances(User staker, IERC20[] memory tokens) internal timewarp() returns (uint[] memory) {
return _getTokenBalances(staker, tokens);
}
function _getTokenBalances(User staker, IERC20[] memory tokens) internal view returns (uint[] memory) {
uint[] memory balances = new uint[](tokens.length);
for (uint i = 0; i < tokens.length; i++) {
balances[i] = tokens[i].balanceOf(address(staker));
}
return balances;
}
}