Skip to content

Commit 834854a

Browse files
authored
Merge pull request #491 from graphprotocol/ariel/delegation-no-shares
Revert when not enough precision to assign a delegation share
2 parents d7a7e49 + 86798d6 commit 834854a

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

contracts/staking/Staking.sol

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1313,12 +1313,13 @@ contract Staking is StakingV2Storage, GraphUpgradeable, IStaking {
13131313
uint256 shares = (pool.tokens == 0)
13141314
? delegatedTokens
13151315
: delegatedTokens.mul(pool.shares).div(pool.tokens);
1316+
require(shares > 0, "!shares");
13161317

13171318
// Update the delegation pool
13181319
pool.tokens = pool.tokens.add(delegatedTokens);
13191320
pool.shares = pool.shares.add(shares);
13201321

1321-
// Update the delegation
1322+
// Update the individual delegation
13221323
delegation.shares = delegation.shares.add(shares);
13231324

13241325
emit StakeDelegated(_indexer, _delegator, delegatedTokens, shares);

test/staking/delegation.test.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ describe('Staking::Delegation', () => {
400400
await shouldDelegate(delegator2, toGRT('5000'))
401401
})
402402

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

419-
it('should delegate and burn delegation deposit tax (100%)', async function () {
419+
it('reject delegate with delegation deposit tax (100%)', async function () {
420420
await staking.setDelegationTaxPercentage(1000000)
421-
await shouldDelegate(delegator, toGRT('10000000'))
421+
const tx = staking.connect(delegator.signer).delegate(indexer.address, toGRT('10000000'))
422+
await expect(tx).revertedWith('!shares')
422423
})
423424
})
424425
})
@@ -649,5 +650,25 @@ describe('Staking::Delegation', () => {
649650
const afterDelegationPool = await staking.delegationPools(indexer.address)
650651
expect(afterDelegationPool.tokens).eq(beforeDelegationPool.tokens.add(delegationFees))
651652
})
653+
654+
it('revert if it cannot assign the smallest amount of shares', async function () {
655+
// Init the delegation pool
656+
await shouldDelegate(delegator, tokensToDelegate)
657+
658+
// Collect funds thru full allocation cycle
659+
await staking.connect(governor.signer).setDelegationRatio(10)
660+
await staking.connect(indexer.signer).setDelegationParameters(0, 0, 0)
661+
await setupAllocation(tokensToAllocate)
662+
await staking.connect(assetHolder.signer).collect(tokensToCollect, allocationID)
663+
await advanceToNextEpoch(epochManager)
664+
await staking.connect(indexer.signer).closeAllocation(allocationID, poi)
665+
await advanceToNextEpoch(epochManager)
666+
await staking.connect(indexer.signer).claim(allocationID, true)
667+
668+
// Delegate with such small amount of tokens (1 wei) that we do not have enough precision
669+
// to even assign 1 wei of shares
670+
const tx = staking.connect(delegator.signer).delegate(indexer.address, toBN(1))
671+
await expect(tx).revertedWith('!shares')
672+
})
652673
})
653674
})

0 commit comments

Comments
 (0)