Skip to content

Commit

Permalink
Merge pull request #491 from graphprotocol/ariel/delegation-no-shares
Browse files Browse the repository at this point in the history
Revert when not enough precision to assign a delegation share
  • Loading branch information
abarmat authored Sep 22, 2021
2 parents d7a7e49 + 86798d6 commit 834854a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
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')
})
})
})

0 comments on commit 834854a

Please sign in to comment.