Skip to content

Commit 821c93a

Browse files
committed
fix: clean up the check for remaining delegation (OZ N-01)
1 parent f9af7eb commit 821c93a

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

contracts/staking/StakingExtension.sol

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -590,22 +590,26 @@ contract StakingExtension is StakingV4Storage, GraphUpgradeable, IStakingExtensi
590590
_withdrawDelegated(_delegator, _indexer, address(0));
591591
}
592592

593+
uint256 poolTokens = pool.tokens;
594+
uint256 poolShares = pool.shares;
595+
593596
// 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);
595598

596599
// 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;
599604

600605
// Update the delegation
601606
delegation.shares = delegation.shares.sub(_shares);
602607
// Enforce more than the minimum delegation is left,
603608
// 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+
}
609613
delegation.tokensLocked = delegation.tokensLocked.add(tokens);
610614
delegation.tokensLockedUntil = epochManager().currentEpoch().add(
611615
__delegationUnbondingPeriod

0 commit comments

Comments
 (0)