|
| 1 | +// SPDX-License-Identifier: BUSL-1.1 |
| 2 | +pragma solidity ^0.8.27; |
| 3 | + |
| 4 | +import "src/test/integration/UpgradeTest.t.sol"; |
| 5 | + |
| 6 | +contract Integration_Upgrade_EigenPod_Negative_Shares is UpgradeTest { |
| 7 | + function _init() internal override { |
| 8 | + _configAssetTypes(HOLDS_ETH); |
| 9 | + _configUserTypes(DEFAULT); |
| 10 | + } |
| 11 | + |
| 12 | + function testFuzz_deposit_delegate_updateBalance_upgrade_completeAsShares( |
| 13 | + uint24 _rand |
| 14 | + ) public rand(_rand) { |
| 15 | + /// 0. Create an operator and staker with some underlying assets |
| 16 | + (User staker, IStrategy[] memory strategies, uint256[] memory tokenBalances) = _newRandomStaker(); |
| 17 | + (User operator,,) = _newRandomOperator(); |
| 18 | + uint256[] memory shares = _calculateExpectedShares(strategies, tokenBalances); |
| 19 | + |
| 20 | + /// 1. Deposit into strategies |
| 21 | + staker.depositIntoEigenlayer(strategies, tokenBalances); |
| 22 | + |
| 23 | + /// 2. Delegate to operator |
| 24 | + staker.delegateTo(operator); |
| 25 | + |
| 26 | + /// 3. Queue a withdrawal for all shares |
| 27 | + IDelegationManagerTypes.Withdrawal[] memory withdrawals = staker.queueWithdrawals(strategies, shares); |
| 28 | + IDelegationManagerTypes.Withdrawal memory withdrawal = withdrawals[0]; |
| 29 | + |
| 30 | + /// 4. Update balance randomly (can be positive or negative) |
| 31 | + (int256[] memory tokenDeltas, int256[] memory balanceUpdateShareDelta,) = _randBalanceUpdate(staker, strategies); |
| 32 | + staker.updateBalances(strategies, tokenDeltas); |
| 33 | + |
| 34 | + /// 5. Upgrade contracts |
| 35 | + _upgradeEigenLayerContracts(); |
| 36 | + |
| 37 | + /// 6. Complete the withdrawal as shares |
| 38 | + _rollBlocksForCompleteWithdrawals(withdrawals); |
| 39 | + staker.completeWithdrawalAsShares(withdrawal); |
| 40 | + |
| 41 | + // Manually complete checks since we could still negative shares prior to the upgrade, causing a revert in the share check |
| 42 | + (uint256[] memory expectedOperatorShareDelta, int256[] memory expectedStakerShareDelta) = |
| 43 | + _getPostWithdrawalExpectedShareDeltas(balanceUpdateShareDelta[0], withdrawal); |
| 44 | + assert_WithdrawalNotPending(delegationManager.calculateWithdrawalRoot(withdrawal), "staker withdrawal should no longer be pending"); |
| 45 | + assert_Snap_Unchanged_TokenBalances(staker, "staker should not have any change in underlying token balances"); |
| 46 | + assert_Snap_Added_OperatorShares(operator, withdrawal.strategies, expectedOperatorShareDelta, "operator should have received shares"); |
| 47 | + assert_Snap_Delta_StakerShares(staker, strategies, expectedStakerShareDelta, "staker should have received expected shares"); |
| 48 | + } |
| 49 | + |
| 50 | + function testFuzz_deposit_delegate_updateBalance_upgrade_completeAsTokens( |
| 51 | + uint24 _rand |
| 52 | + ) public rand(_rand) { |
| 53 | + /// 0. Create an operator and staker with some underlying assets |
| 54 | + (User staker, IStrategy[] memory strategies, uint256[] memory tokenBalances) = _newRandomStaker(); |
| 55 | + (User operator,,) = _newRandomOperator(); |
| 56 | + uint256[] memory shares = _calculateExpectedShares(strategies, tokenBalances); |
| 57 | + |
| 58 | + /// 1. Deposit into strategies |
| 59 | + staker.depositIntoEigenlayer(strategies, tokenBalances); |
| 60 | + |
| 61 | + /// 2. Delegate to operator |
| 62 | + staker.delegateTo(operator); |
| 63 | + |
| 64 | + /// 3. Queue a withdrawal for all shares |
| 65 | + IDelegationManagerTypes.Withdrawal[] memory withdrawals = staker.queueWithdrawals(strategies, shares); |
| 66 | + IDelegationManagerTypes.Withdrawal memory withdrawal = withdrawals[0]; |
| 67 | + |
| 68 | + /// 4. Update balance randomly (can be positive or negative) |
| 69 | + (int256[] memory tokenDeltas, int256[] memory balanceUpdateShareDelta,) = _randBalanceUpdate(staker, strategies); |
| 70 | + staker.updateBalances(strategies, tokenDeltas); |
| 71 | + |
| 72 | + /// 5. Upgrade contracts |
| 73 | + _upgradeEigenLayerContracts(); |
| 74 | + |
| 75 | + /// 6. Complete the withdrawal as shares |
| 76 | + _rollBlocksForCompleteWithdrawals(withdrawals); |
| 77 | + IERC20[] memory tokens = staker.completeWithdrawalAsTokens(withdrawal); |
| 78 | + uint256[] memory expectedTokens = _getPostWithdrawalExpectedTokenDeltas(balanceUpdateShareDelta[0], withdrawal); |
| 79 | + |
| 80 | + // Manually complete checks since we could still negative shares prior to the upgrade, causing a revert in the share check |
| 81 | + assert_WithdrawalNotPending(delegationManager.calculateWithdrawalRoot(withdrawal), "staker withdrawal should no longer be pending"); |
| 82 | + assert_Snap_Added_TokenBalances(staker, tokens, expectedTokens, "staker should have received expected tokens"); |
| 83 | + assert_Snap_Unchanged_OperatorShares(operator, "operator shares should not have changed"); |
| 84 | + |
| 85 | + // If we had a positive balance update, then the staker shares should not have changed |
| 86 | + if (balanceUpdateShareDelta[0] > 0) { |
| 87 | + assert_Snap_Unchanged_Staker_DepositShares(staker, "staker shares should not have changed"); |
| 88 | + } |
| 89 | + // Else, the staker shares should have increased by the magnitude of the negative share delta |
| 90 | + else { |
| 91 | + int256[] memory expectedStakerShareDelta = new int256[](1); |
| 92 | + expectedStakerShareDelta[0] = -balanceUpdateShareDelta[0]; |
| 93 | + assert_Snap_Delta_StakerShares(staker, strategies, expectedStakerShareDelta, "staker should have received expected shares"); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + function _getPostWithdrawalExpectedShareDeltas( |
| 100 | + int256 balanceUpdateShareDelta, |
| 101 | + IDelegationManagerTypes.Withdrawal memory withdrawal |
| 102 | + ) internal pure returns (uint256[] memory, int256[] memory) { |
| 103 | + uint256[] memory operatorShareDelta = new uint256[](1); |
| 104 | + int256[] memory stakerShareDelta = new int256[](1); |
| 105 | + // The staker share delta is the withdrawal scaled shares since it can go from negative to positive |
| 106 | + stakerShareDelta[0] = int256(withdrawal.scaledShares[0]); |
| 107 | + |
| 108 | + if (balanceUpdateShareDelta > 0) { |
| 109 | + // If balanceUpdateShareDelta is positive, then the operator delta is the withdrawal scaled shares |
| 110 | + operatorShareDelta[0] = withdrawal.scaledShares[0]; |
| 111 | + } else { |
| 112 | + // Operator shares never went negative, so we can just add the withdrawal scaled shares and the negative share delta |
| 113 | + operatorShareDelta[0] = uint256(int256(withdrawal.scaledShares[0]) + balanceUpdateShareDelta); |
| 114 | + } |
| 115 | + |
| 116 | + return (operatorShareDelta, stakerShareDelta); |
| 117 | + } |
| 118 | + |
| 119 | + function _getPostWithdrawalExpectedTokenDeltas( |
| 120 | + int256 balanceUpdateShareDelta, |
| 121 | + IDelegationManagerTypes.Withdrawal memory withdrawal |
| 122 | + ) internal pure returns (uint256[] memory) { |
| 123 | + uint256[] memory expectedTokenDeltas = new uint256[](1); |
| 124 | + if (balanceUpdateShareDelta > 0) { |
| 125 | + // If we had a positive balance update, then the expected token delta is the withdrawal scaled shares |
| 126 | + expectedTokenDeltas[0] = withdrawal.scaledShares[0]; |
| 127 | + } else { |
| 128 | + // If we had a negative balance update, then the expected token delta is the withdrawal scaled shares plus the negative share delta |
| 129 | + expectedTokenDeltas[0] = uint256(int256(withdrawal.scaledShares[0]) + balanceUpdateShareDelta); |
| 130 | + } |
| 131 | + return expectedTokenDeltas; |
| 132 | + } |
| 133 | +} |
0 commit comments