Skip to content

Commit

Permalink
test: deposit, delegate, redelegate, complete
Browse files Browse the repository at this point in the history
  • Loading branch information
Gajesh2007 authored and ypatil12 committed Dec 4, 2023
1 parent 01cd429 commit c017fa2
Showing 1 changed file with 141 additions and 1 deletion.
142 changes: 141 additions & 1 deletion src/test/integration/tests/Deposit_Delegate_Redelegate_Complete.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,147 @@ contract Integration_Deposit_Delegate_Redelegate_Complete is IntegrationCheckUti
/// 5. delegate to a new operator
/// 5. queueWithdrawal
/// 7. complete their queued withdrawal as tokens
function testFuzz_deposit_delegate_reDelegate_completeAsTokens(uint24 _random) public {
function testFuzz_deposit_delegate_reDelegate_completeLastWithdrawalAsShares(uint24 _random) public {
// When new Users are created, they will choose a random configuration from these params:
_configRand({
_randomSeed: _random,
_assetTypes: HOLDS_LST | HOLDS_ETH | HOLDS_ALL,
_userTypes: DEFAULT | ALT_METHODS
});

/// 0. Create an operator and a staker with:
// - some nonzero underlying token balances
// - corresponding to a random number of strategies
//
// ... check that the staker has no deleagatable shares and isn't delegated

(
User staker,
IStrategy[] memory strategies,
uint[] memory tokenBalances
) = _newRandomStaker();
(User operator1, ,) = _newRandomOperator();
(User operator2, ,) = _newRandomOperator();
uint[] memory shares = _calculateExpectedShares(strategies, tokenBalances);

assert_HasNoDelegatableShares(staker, "staker should not have delegatable shares before depositing");
assertFalse(delegationManager.isDelegated(address(staker)), "staker should not be delegated");

/// 1. Deposit Into Strategies
staker.depositIntoEigenlayer(strategies, tokenBalances);
assertDepositState(staker, strategies, shares);

// 2. Delegate to an operator
staker.delegateTo(operator1);
assertDelegationState(staker, operator1, strategies, shares);

// 3. Undelegate from an operator
IDelegationManager.Withdrawal memory expectedWithdrawal = _getExpectedWithdrawalStruct(staker);
bytes32 withdrawalRoot = staker.undelegate();
assertUndelegateState(staker, operator1, expectedWithdrawal, withdrawalRoot, strategies, shares);

// 4. Complete withdrawal as shares
// Fast forward to when we can complete the withdrawal
cheats.roll(block.number + delegationManager.withdrawalDelayBlocks());
staker.completeQueuedWithdrawal(expectedWithdrawal, false);

assertWithdrawalAsSharesState(staker, expectedWithdrawal, strategies, shares);

// 5. Delegate to a new operator
staker.delegateTo(operator2);
assertDelegationState(staker, operator2, strategies, shares);
assertNotEq(address(operator1), delegationManager.delegatedTo(address(staker)), "staker should not be delegated to operator1");

// 6. Queue Withdrawal
IDelegationManager.Withdrawal[] memory withdrawals;
bytes32[] memory withdrawalRoots;
(withdrawals, withdrawalRoots) = staker.queueWithdrawals(strategies, shares);
assertQueuedWithdrawalState(staker, operator2, strategies, shares, withdrawals, withdrawalRoots);

// 7. Complete withdrawal
// Fast forward to when we can complete the withdrawal
cheats.roll(block.number + delegationManager.withdrawalDelayBlocks());

// Complete all but last withdrawal as tokens
for (uint i = 0; i < withdrawals.length - 1; i++) {
staker.completeQueuedWithdrawal(withdrawals[i], true);
}

// Complete last withdrawal as shares
staker.completeQueuedWithdrawal(withdrawals[withdrawals.length - 1], false);
assertWithdrawalAsSharesState(staker, withdrawals[withdrawals.length - 1], strategies, shares);
}

function testFuzz_deposit_delegate_reDelegate_withAdditionalDepositBefore(uint24 _random) public {
// When new Users are created, they will choose a random configuration from these params:
_configRand({
_randomSeed: _random,
_assetTypes: HOLDS_LST | HOLDS_ETH | HOLDS_ALL,
_userTypes: DEFAULT | ALT_METHODS
});

/// 0. Create an operator and a staker with:
// - some nonzero underlying token balances
// - corresponding to a random number of strategies
//
// ... check that the staker has no deleagatable shares and isn't delegated

(
User staker,
IStrategy[] memory strategies,
uint[] memory tokenBalances
) = _newRandomStaker();
(User operator1, ,) = _newRandomOperator();
(User operator2, ,) = _newRandomOperator();
uint[] memory shares = _calculateExpectedShares(strategies, tokenBalances);

assert_HasNoDelegatableShares(staker, "staker should not have delegatable shares before depositing");
assertFalse(delegationManager.isDelegated(address(staker)), "staker should not be delegated");

/// 1. Deposit Into Strategies
staker.depositIntoEigenlayer(strategies, tokenBalances);
assertDepositState(staker, strategies, shares);

// 2. Delegate to an operator
staker.delegateTo(operator1);
assertDelegationState(staker, operator1, strategies, shares);

// 3. Undelegate from an operator
IDelegationManager.Withdrawal memory expectedWithdrawal = _getExpectedWithdrawalStruct(staker);
bytes32 withdrawalRoot = staker.undelegate();
assertUndelegateState(staker, operator1, expectedWithdrawal, withdrawalRoot, strategies, shares);

// 4. Complete withdrawal as shares
// Fast forward to when we can complete the withdrawal
cheats.roll(block.number + delegationManager.withdrawalDelayBlocks());
staker.completeQueuedWithdrawal(expectedWithdrawal, false);

assertWithdrawalAsSharesState(staker, expectedWithdrawal, strategies, shares);

// 5. Delegate to a new operator
staker.delegateTo(operator2);
assertDelegationState(staker, operator2, strategies, shares);
assertNotEq(address(operator1), delegationManager.delegatedTo(address(staker)), "staker should not be delegated to operator1");

// 6. Queue Withdrawal
IDelegationManager.Withdrawal[] memory withdrawals;
bytes32[] memory withdrawalRoots;
(withdrawals, withdrawalRoots) = staker.queueWithdrawals(strategies, shares);
assertQueuedWithdrawalState(staker, operator2, strategies, shares, withdrawals, withdrawalRoots);

// 7. Complete withdrawal
// Fast forward to when we can complete the withdrawal
cheats.roll(block.number + delegationManager.withdrawalDelayBlocks());

// Complete withdrawals
for (uint i = 0; i < withdrawals.length; i++) {
IERC20[] memory tokens = staker.completeQueuedWithdrawal(withdrawals[i], true);
uint[] memory expectedTokens = _calculateExpectedTokens(withdrawals[i].strategies, withdrawals[i].shares);
assertWithdrawalAsTokensState(staker, withdrawals[i], strategies, shares, tokens, expectedTokens);
}
}

function testFuzz_deposit_delegate_reDelegate_withAdditionalDepositAfter(uint24 _random) public {
// When new Users are created, they will choose a random configuration from these params:
_configRand({
_randomSeed: _random,
Expand Down

0 comments on commit c017fa2

Please sign in to comment.