forked from Layr-Labs/eigenlayer-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntegrationBase.t.sol
791 lines (643 loc) · 30.4 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
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
// 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 "@openzeppelin/contracts/utils/Strings.sol";
import "src/test/integration/IntegrationDeployer.t.sol";
import "src/test/integration/TimeMachine.t.sol";
import "src/test/integration/User.t.sol";
abstract contract IntegrationBase is IntegrationDeployer {
using Strings for *;
uint numStakers = 0;
uint numOperators = 0;
/**
* 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 _newRandomStaker() internal returns (User, IStrategy[] memory, uint[] memory) {
string memory stakerName = string.concat("- Staker", numStakers.toString());
numStakers++;
(User staker, IStrategy[] memory strategies, uint[] memory tokenBalances) = _randUser(stakerName);
assert_HasUnderlyingTokenBalances(staker, strategies, tokenBalances, "_newRandomStaker: failed to award token balances");
return (staker, strategies, tokenBalances);
}
function _newRandomOperator() internal returns (User, IStrategy[] memory, uint[] memory) {
string memory operatorName = string.concat("- Operator", numOperators.toString());
numOperators++;
(User operator, IStrategy[] memory strategies, uint[] memory tokenBalances) = _randUser(operatorName);
operator.registerAsOperator();
operator.depositIntoEigenlayer(strategies, tokenBalances);
assert_Snap_Added_StakerShares(operator, strategies, tokenBalances, "_newRandomOperator: failed to add delegatable shares");
assert_Snap_Added_OperatorShares(operator, strategies, tokenBalances, "_newRandomOperator: failed to award shares to operator");
assertTrue(delegationManager.isOperator(address(operator)), "_newRandomOperator: 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) {
// This method should only be used for tests that handle positive
// balances. Negative balances are an edge case that require
// the own tests and helper methods.
int shares = eigenPodManager.podOwnerShares(address(user));
if (shares < 0) {
revert("assert_HasExpectedShares: negative shares");
}
actualShares = uint(shares);
} 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);
}
}
/// @dev Asserts that ALL of the `withdrawalRoots` is in `delegationManager.pendingWithdrawals`
function assert_AllWithdrawalsPending(bytes32[] memory withdrawalRoots, string memory err) internal {
for (uint i = 0; i < withdrawalRoots.length; i++) {
assert_WithdrawalPending(withdrawalRoots[i], err);
}
}
/// @dev Asserts that NONE of the `withdrawalRoots` is in `delegationManager.pendingWithdrawals`
function assert_NoWithdrawalsPending(bytes32[] memory withdrawalRoots, string memory err) internal {
for (uint i = 0; i < withdrawalRoots.length; i++) {
assert_WithdrawalNotPending(withdrawalRoots[i], err);
}
}
/// @dev Asserts that the hash of each withdrawal corresponds to the provided withdrawal root
function assert_WithdrawalPending(bytes32 withdrawalRoot, string memory err) internal {
assertTrue(delegationManager.pendingWithdrawals(withdrawalRoot), err);
}
function assert_WithdrawalNotPending(bytes32 withdrawalRoot, string memory err) internal {
assertFalse(delegationManager.pendingWithdrawals(withdrawalRoot), err);
}
function assert_ValidWithdrawalHashes(
IDelegationManager.Withdrawal[] memory withdrawals,
bytes32[] memory withdrawalRoots,
string memory err
) internal {
for (uint i = 0; i < withdrawals.length; i++) {
assert_ValidWithdrawalHash(withdrawals[i], withdrawalRoots[i], err);
}
}
function assert_ValidWithdrawalHash(
IDelegationManager.Withdrawal memory withdrawal,
bytes32 withdrawalRoot,
string memory err
) internal {
assertEq(withdrawalRoot, delegationManager.calculateWithdrawalRoot(withdrawal), err);
}
/*******************************************************************************
SNAPSHOT ASSERTIONS
TIME TRAVELERS ONLY BEYOND THIS POINT
*******************************************************************************/
/// Snapshot assertions for delegationManager.operatorShares:
/// @dev Check that the operator has `addedShares` additional operator shares
// for each strategy since the last snapshot
function assert_Snap_Added_OperatorShares(
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` fewer operator shares
/// for each strategy since the last snapshot
function assert_Snap_Removed_OperatorShares(
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 operator's shares in ALL strategies have not changed
/// since the last snapshot
function assert_Snap_Unchanged_OperatorShares(
User operator,
string memory err
) internal {
IStrategy[] memory strategies = allStrats;
uint[] memory curShares = _getOperatorShares(operator, strategies);
// Use timewarp to get previous operator shares
uint[] memory prevShares = _getPrevOperatorShares(operator, strategies);
// For each strategy, check (prev == cur)
for (uint i = 0; i < strategies.length; i++) {
assertEq(prevShares[i], curShares[i], err);
}
}
function assert_Snap_Delta_OperatorShares(
User operator,
IStrategy[] memory strategies,
int[] memory shareDeltas,
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++) {
uint expectedShares;
if (shareDeltas[i] < 0) {
expectedShares = prevShares[i] - uint(-shareDeltas[i]);
} else {
expectedShares = prevShares[i] + uint(shareDeltas[i]);
}
assertEq(expectedShares, curShares[i], err);
}
}
/// Snapshot assertions for strategyMgr.stakerStrategyShares and eigenPodMgr.podOwnerShares:
/// @dev Check that the staker has `addedShares` additional delegatable shares
/// for each strategy since the last snapshot
function assert_Snap_Added_StakerShares(
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` fewer delegatable shares
/// for each strategy since the last snapshot
function assert_Snap_Removed_StakerShares(
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);
}
}
/// @dev Check that the staker's delegatable shares in ALL strategies have not changed
/// since the last snapshot
function assert_Snap_Unchanged_StakerShares(
User staker,
string memory err
) internal {
IStrategy[] memory strategies = allStrats;
uint[] memory curShares = _getStakerShares(staker, strategies);
// Use timewarp to get previous staker shares
uint[] memory prevShares = _getPrevStakerShares(staker, strategies);
// For each strategy, check (prev == cur)
for (uint i = 0; i < strategies.length; i++) {
assertEq(prevShares[i], curShares[i], err);
}
}
function assert_Snap_Removed_StrategyShares(
IStrategy[] memory strategies,
uint[] memory removedShares,
string memory err
) internal {
uint[] memory curShares = _getTotalStrategyShares(strategies);
// Use timewarp to get previous strategy shares
uint[] memory prevShares = _getPrevTotalStrategyShares(strategies);
for (uint i = 0; i < strategies.length; i++) {
// Ignore BeaconChainETH strategy since it doesn't keep track of global strategy shares
if (strategies[i] == BEACONCHAIN_ETH_STRAT) {
continue;
}
uint prevShare = prevShares[i];
uint curShare = curShares[i];
assertEq(prevShare - removedShares[i], curShare, err);
}
}
function assert_Snap_Unchanged_StrategyShares(
IStrategy[] memory strategies,
string memory err
) internal {
uint[] memory curShares = _getTotalStrategyShares(strategies);
// Use timewarp to get previous strategy shares
uint[] memory prevShares = _getPrevTotalStrategyShares(strategies);
for (uint i = 0; i < strategies.length; i++) {
uint prevShare = prevShares[i];
uint curShare = curShares[i];
assertEq(prevShare, curShare, err);
}
}
function assert_Snap_Delta_StakerShares(
User staker,
IStrategy[] memory strategies,
int[] memory shareDeltas,
string memory err
) internal {
int[] memory curShares = _getStakerSharesInt(staker, strategies);
// Use timewarp to get previous staker shares
int[] memory prevShares = _getPrevStakerSharesInt(staker, strategies);
// For each strategy, check (prev + added == cur)
for (uint i = 0; i < strategies.length; i++) {
assertEq(prevShares[i] + shareDeltas[i], curShares[i], err);
}
}
/// Snapshot assertions for underlying token balances:
/// @dev Check that the staker has `addedTokens` additional underlying tokens
// since the last snapshot
function assert_Snap_Added_TokenBalances(
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);
}
}
/// @dev Check that the staker has `removedTokens` fewer underlying tokens
// since the last snapshot
function assert_Snap_Removed_TokenBalances(
User staker,
IStrategy[] memory strategies,
uint[] memory removedTokens,
string memory err
) internal {
IERC20[] memory tokens = _getUnderlyingTokens(strategies);
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 - removedTokens[i], curBalance, err);
}
}
/// @dev Check that the staker's underlying token balance for ALL tokens have
/// not changed since the last snapshot
function assert_Snap_Unchanged_TokenBalances(
User staker,
string memory err
) internal {
IERC20[] memory tokens = allTokens;
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++) {
assertEq(prevTokenBalances[i], curTokenBalances[i], err);
}
}
/// Other snapshot assertions:
function assert_Snap_Added_QueuedWithdrawals(
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_Added_QueuedWithdrawal(
User staker,
IDelegationManager.Withdrawal memory withdrawal,
string memory err
) internal {
uint curQueuedWithdrawal = _getCumulativeWithdrawals(staker);
// Use timewarp to get previous cumulative withdrawals
uint prevQueuedWithdrawal = _getPrevCumulativeWithdrawals(staker);
assertEq(prevQueuedWithdrawal + 1, curQueuedWithdrawal, err);
}
/*******************************************************************************
UTILITY METHODS
*******************************************************************************/
function _randWithdrawal(
IStrategy[] memory strategies,
uint[] memory shares
) internal returns (IStrategy[] memory, uint[] memory) {
uint stratsToWithdraw = _randUint({ min: 1, max: strategies.length });
IStrategy[] memory withdrawStrats = new IStrategy[](stratsToWithdraw);
uint[] memory withdrawShares = new uint[](stratsToWithdraw);
for (uint i = 0; i < stratsToWithdraw; i++) {
uint sharesToWithdraw;
if (strategies[i] == BEACONCHAIN_ETH_STRAT) {
// For native eth, withdraw a random amount of gwei (at least 1)
uint portion = _randUint({ min: 1, max: shares[i] / GWEI_TO_WEI });
portion *= GWEI_TO_WEI;
sharesToWithdraw = shares[i] - portion;
} else {
// For LSTs, withdraw a random amount of shares (at least 1)
uint portion = _randUint({ min: 1, max: shares[i] });
sharesToWithdraw = shares[i] - portion;
}
withdrawStrats[i] = strategies[i];
withdrawShares[i] = sharesToWithdraw;
}
return (withdrawStrats, withdrawShares);
}
/**
* Helpful getters:
*/
function _randBalanceUpdate(
User staker,
IStrategy[] memory strategies
) internal returns (int[] memory, int[] memory, int[] memory) {
int[] memory tokenDeltas = new int[](strategies.length);
int[] memory stakerShareDeltas = new int[](strategies.length);
int[] memory operatorShareDeltas = new int[](strategies.length);
for (uint i = 0; i < strategies.length; i++) {
IStrategy strat = strategies[i];
if (strat == BEACONCHAIN_ETH_STRAT) {
// TODO - could choose and set a "next updatable validator" at random here
uint40 validator = staker.getUpdatableValidator();
uint64 beaconBalanceGwei = beaconChain.balanceOfGwei(validator);
// For native eth, add or remove a random amount of Gwei - minimum 1
// and max of the current beacon chain balance
int64 deltaGwei = int64(int(_randUint({ min: 1, max: beaconBalanceGwei })));
bool addTokens = _randBool();
deltaGwei = addTokens ? deltaGwei : -deltaGwei;
tokenDeltas[i] = int(deltaGwei) * int(GWEI_TO_WEI);
// stakerShareDeltas[i] = _calculateSharesDelta(newPodBalanceGwei, oldPodBalanceGwei);
stakerShareDeltas[i] = _calcNativeETHStakerShareDelta(staker, validator, beaconBalanceGwei, deltaGwei);
operatorShareDeltas[i] = _calcNativeETHOperatorShareDelta(staker, stakerShareDeltas[i]);
emit log_named_uint("current beacon balance (gwei): ", beaconBalanceGwei);
// emit log_named_uint("current validator pod balance (gwei): ", oldPodBalanceGwei);
emit log_named_int("beacon balance delta (gwei): ", deltaGwei);
emit log_named_int("staker share delta (gwei): ", stakerShareDeltas[i] / int(GWEI_TO_WEI));
emit log_named_int("operator share delta (gwei): ", operatorShareDeltas[i] / int(GWEI_TO_WEI));
} else {
// For LSTs, mint a random token amount
uint portion = _randUint({ min: MIN_BALANCE, max: MAX_BALANCE });
StdCheats.deal(address(strat.underlyingToken()), address(staker), portion);
int delta = int(portion);
tokenDeltas[i] = delta;
stakerShareDeltas[i] = int(strat.underlyingToShares(uint(delta)));
operatorShareDeltas[i] = int(strat.underlyingToShares(uint(delta)));
}
}
return (tokenDeltas, stakerShareDeltas, operatorShareDeltas);
}
function _calcNativeETHStakerShareDelta(
User staker,
uint40 validatorIndex,
uint64 beaconBalanceGwei,
int64 deltaGwei
) internal view returns (int) {
uint64 oldPodBalanceGwei =
staker
.pod()
.validatorPubkeyHashToInfo(beaconChain.pubkeyHash(validatorIndex))
.restakedBalanceGwei;
uint64 newPodBalanceGwei = _calcPodBalance(beaconBalanceGwei, deltaGwei);
return (int(uint(newPodBalanceGwei)) - int(uint(oldPodBalanceGwei))) * int(GWEI_TO_WEI);
}
function _calcPodBalance(uint64 beaconBalanceGwei, int64 deltaGwei) internal pure returns (uint64) {
uint64 podBalanceGwei;
if (deltaGwei < 0) {
podBalanceGwei = beaconBalanceGwei - uint64(uint(int(-deltaGwei)));
} else {
podBalanceGwei = beaconBalanceGwei + uint64(uint(int(deltaGwei)));
}
if (podBalanceGwei > MAX_RESTAKED_BALANCE_GWEI_PER_VALIDATOR) {
podBalanceGwei = MAX_RESTAKED_BALANCE_GWEI_PER_VALIDATOR;
}
return podBalanceGwei;
}
function _calcNativeETHOperatorShareDelta(User staker, int shareDelta) internal view returns (int) {
int curPodOwnerShares = eigenPodManager.podOwnerShares(address(staker));
int newPodOwnerShares = curPodOwnerShares + shareDelta;
if (curPodOwnerShares <= 0) {
// if the shares started negative and stayed negative, then there cannot have been an increase in delegateable shares
if (newPodOwnerShares <= 0) {
return 0;
// if the shares started negative and became positive, then the increase in delegateable shares is the ending share amount
} else {
return newPodOwnerShares;
}
} else {
// if the shares started positive and became negative, then the decrease in delegateable shares is the starting share amount
if (newPodOwnerShares <= 0) {
return (-curPodOwnerShares);
// if the shares started positive and stayed positive, then the change in delegateable shares
// is the difference between starting and ending amounts
} else {
return (newPodOwnerShares - curPodOwnerShares);
}
}
}
/// @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) {
expectedShares[i] = tokenBalance;
} 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) {
expectedTokens[i] = shares[i];
} else {
expectedTokens[i] = strat.sharesToUnderlying(shares[i]);
}
}
return expectedTokens;
}
function _getWithdrawalHashes(
IDelegationManager.Withdrawal[] memory withdrawals
) internal view returns (bytes32[] memory) {
bytes32[] memory withdrawalRoots = new bytes32[](withdrawals.length);
for (uint i = 0; i < withdrawals.length; i++) {
withdrawalRoots[i] = delegationManager.calculateWithdrawalRoot(withdrawals[i]);
}
return withdrawalRoots;
}
/// @dev Converts a list of strategies to underlying tokens
function _getUnderlyingTokens(IStrategy[] memory strategies) internal view returns (IERC20[] memory) {
IERC20[] memory tokens = new IERC20[](strategies.length);
for (uint i = 0; i < tokens.length; i++) {
IStrategy strat = strategies[i];
if (strat == BEACONCHAIN_ETH_STRAT) {
tokens[i] = NATIVE_ETH;
} else {
tokens[i] = strat.underlyingToken();
}
}
return tokens;
}
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) {
// This method should only be used for tests that handle positive
// balances. Negative balances are an edge case that require
// the own tests and helper methods.
int shares = eigenPodManager.podOwnerShares(address(staker));
if (shares < 0) {
revert("_getStakerShares: negative shares");
}
curShares[i] = uint(shares);
} else {
curShares[i] = strategyManager.stakerStrategyShares(address(staker), strat);
}
}
return curShares;
}
/// @dev Uses timewarp modifier to get staker shares at the last snapshot
function _getPrevStakerSharesInt(
User staker,
IStrategy[] memory strategies
) internal timewarp() returns (int[] memory) {
return _getStakerSharesInt(staker, strategies);
}
/// @dev Looks up each strategy and returns a list of the staker's shares
function _getStakerSharesInt(User staker, IStrategy[] memory strategies) internal view returns (int[] memory) {
int[] memory curShares = new int[](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));
} else {
curShares[i] = int(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++) {
if (tokens[i] == NATIVE_ETH) {
balances[i] = address(staker).balance;
} else {
balances[i] = tokens[i].balanceOf(address(staker));
}
}
return balances;
}
function _getPrevTotalStrategyShares(IStrategy[] memory strategies) internal timewarp() returns (uint[] memory) {
return _getTotalStrategyShares(strategies);
}
function _getTotalStrategyShares(IStrategy[] memory strategies) internal view returns (uint[] memory) {
uint[] memory shares = new uint[](strategies.length);
for (uint i = 0; i < strategies.length; i++) {
if (strategies[i] != BEACONCHAIN_ETH_STRAT) {
shares[i] = strategies[i].totalShares();
}
// BeaconChainETH strategy doesn't keep track of global strategy shares, so we ignore
}
return shares;
}
}