Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: clean up the check for remaining delegation (OZ N-01) #880

Merged
merged 1 commit into from
Dec 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions contracts/staking/StakingExtension.sol
Original file line number Diff line number Diff line change
Expand Up @@ -590,22 +590,26 @@ contract StakingExtension is StakingV4Storage, GraphUpgradeable, IStakingExtensi
_withdrawDelegated(_delegator, _indexer, address(0));
}

uint256 poolTokens = pool.tokens;
uint256 poolShares = pool.shares;

// Calculate tokens to get in exchange for the shares
uint256 tokens = _shares.mul(pool.tokens).div(pool.shares);
uint256 tokens = _shares.mul(poolTokens).div(poolShares);

// Update the delegation pool
pool.tokens = pool.tokens.sub(tokens);
pool.shares = pool.shares.sub(_shares);
poolTokens = poolTokens.sub(tokens);
poolShares = poolShares.sub(_shares);
pool.tokens = poolTokens;
pool.shares = poolShares;

// Update the delegation
delegation.shares = delegation.shares.sub(_shares);
// Enforce more than the minimum delegation is left,
// to prevent rounding attacks
require(
delegation.shares == 0 ||
delegation.shares.mul(pool.tokens).div(pool.shares) >= MINIMUM_DELEGATION,
"!minimum-delegation"
);
if (delegation.shares > 0) {
uint256 remainingDelegation = delegation.shares.mul(poolTokens).div(poolShares);
require(remainingDelegation >= MINIMUM_DELEGATION, "!minimum-delegation");
}
delegation.tokensLocked = delegation.tokensLocked.add(tokens);
delegation.tokensLockedUntil = epochManager().currentEpoch().add(
__delegationUnbondingPeriod
Expand Down