@@ -590,22 +590,26 @@ contract StakingExtension is StakingV4Storage, GraphUpgradeable, IStakingExtensi
590
590
_withdrawDelegated (_delegator, _indexer, address (0 ));
591
591
}
592
592
593
+ uint256 poolTokens = pool.tokens;
594
+ uint256 poolShares = pool.shares;
595
+
593
596
// Calculate tokens to get in exchange for the shares
594
- uint256 tokens = _shares.mul (pool.tokens ).div (pool.shares );
597
+ uint256 tokens = _shares.mul (poolTokens ).div (poolShares );
595
598
596
599
// Update the delegation pool
597
- pool.tokens = pool.tokens.sub (tokens);
598
- pool.shares = pool.shares.sub (_shares);
600
+ poolTokens = poolTokens.sub (tokens);
601
+ poolShares = poolShares.sub (_shares);
602
+ pool.tokens = poolTokens;
603
+ pool.shares = poolShares;
599
604
600
605
// Update the delegation
601
606
delegation.shares = delegation.shares.sub (_shares);
602
607
// Enforce more than the minimum delegation is left,
603
608
// to prevent rounding attacks
604
- require (
605
- delegation.shares == 0 ||
606
- delegation.shares.mul (pool.tokens).div (pool.shares) >= MINIMUM_DELEGATION,
607
- "!minimum-delegation "
608
- );
609
+ if (delegation.shares > 0 ) {
610
+ uint256 remainingDelegation = delegation.shares.mul (poolTokens).div (poolShares);
611
+ require (remainingDelegation >= MINIMUM_DELEGATION, "!minimum-delegation " );
612
+ }
609
613
delegation.tokensLocked = delegation.tokensLocked.add (tokens);
610
614
delegation.tokensLockedUntil = epochManager ().currentEpoch ().add (
611
615
__delegationUnbondingPeriod
0 commit comments