Skip to content

Revert when not enough precision to assign a delegation share #491

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

Merged
merged 2 commits into from
Sep 22, 2021
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion contracts/staking/Staking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1313,12 +1313,13 @@ contract Staking is StakingV2Storage, GraphUpgradeable, IStaking {
uint256 shares = (pool.tokens == 0)
? delegatedTokens
: delegatedTokens.mul(pool.shares).div(pool.tokens);
require(shares > 0, "!shares");

// Update the delegation pool
pool.tokens = pool.tokens.add(delegatedTokens);
pool.shares = pool.shares.add(shares);

// Update the delegation
// Update the individual delegation
delegation.shares = delegation.shares.add(shares);

emit StakeDelegated(_indexer, _delegator, delegatedTokens, shares);
Expand Down
27 changes: 24 additions & 3 deletions test/staking/delegation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ describe('Staking::Delegation', () => {
await shouldDelegate(delegator2, toGRT('5000'))
})

it('should delegate a high number of tokens', async function () {
it('should delegate a high amount of tokens', async function () {
await shouldDelegate(delegator, toGRT('100'))
await shouldDelegate(delegator, toGRT('1000000000000000000'))
})
Expand All @@ -416,9 +416,10 @@ describe('Staking::Delegation', () => {
await shouldDelegate(delegator, toGRT('10000000'))
})

it('should delegate and burn delegation deposit tax (100%)', async function () {
it('reject delegate with delegation deposit tax (100%)', async function () {
await staking.setDelegationTaxPercentage(1000000)
await shouldDelegate(delegator, toGRT('10000000'))
const tx = staking.connect(delegator.signer).delegate(indexer.address, toGRT('10000000'))
await expect(tx).revertedWith('!shares')
})
})
})
Expand Down Expand Up @@ -649,5 +650,25 @@ describe('Staking::Delegation', () => {
const afterDelegationPool = await staking.delegationPools(indexer.address)
expect(afterDelegationPool.tokens).eq(beforeDelegationPool.tokens.add(delegationFees))
})

it('revert if it cannot assign the smallest amount of shares', async function () {
// Init the delegation pool
await shouldDelegate(delegator, tokensToDelegate)

// Collect funds thru full allocation cycle
await staking.connect(governor.signer).setDelegationRatio(10)
await staking.connect(indexer.signer).setDelegationParameters(0, 0, 0)
await setupAllocation(tokensToAllocate)
await staking.connect(assetHolder.signer).collect(tokensToCollect, allocationID)
await advanceToNextEpoch(epochManager)
await staking.connect(indexer.signer).closeAllocation(allocationID, poi)
await advanceToNextEpoch(epochManager)
await staking.connect(indexer.signer).claim(allocationID, true)

// Delegate with such small amount of tokens (1 wei) that we do not have enough precision
// to even assign 1 wei of shares
const tx = staking.connect(delegator.signer).delegate(indexer.address, toBN(1))
await expect(tx).revertedWith('!shares')
})
})
})