Skip to content

Commit f07b62a

Browse files
authored
staking: init delegation params on first stake to the most benefitial state for the indexer (#255)
1 parent 44be3f0 commit f07b62a

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

contracts/staking/Staking.sol

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ contract Staking is StakingV1Storage, IStaking, Governed {
2323
using Rebates for Rebates.Pool;
2424

2525
// 100% in parts per million
26-
uint256 private constant MAX_PPM = 1000000;
26+
uint32 private constant MAX_PPM = 1000000;
2727

2828
// -- Events --
2929

@@ -908,8 +908,18 @@ contract Staking is StakingV1Storage, IStaking, Governed {
908908
* @param _tokens Amount of tokens to stake
909909
*/
910910
function _stake(address _indexer, uint256 _tokens) private {
911+
// Deposit tokens into the indexer stake
911912
Stakes.Indexer storage indexerStake = stakes[_indexer];
912913
indexerStake.deposit(_tokens);
914+
915+
// Initialize the delegation pool the first time
916+
DelegationPool storage pool = delegationPools[_indexer];
917+
if (pool.updatedAtBlock == 0) {
918+
pool.indexingRewardCut = MAX_PPM;
919+
pool.queryFeeCut = MAX_PPM;
920+
pool.updatedAtBlock = block.number;
921+
}
922+
913923
emit StakeDeposited(_indexer, _tokens);
914924
}
915925

test/staking/delegation.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,25 @@ describe('Staking::Delegation', () => {
263263
expect(params.cooldownBlocks).eq(cooldownBlocks)
264264
expect(params.updatedAtBlock).eq(await latestBlock())
265265
})
266+
267+
it('should init delegation parameters on first stake', async function () {
268+
// Before
269+
const beforeParams = await staking.delegationPools(indexer.address)
270+
expect(beforeParams.indexingRewardCut).eq(0)
271+
expect(beforeParams.queryFeeCut).eq(0)
272+
expect(beforeParams.cooldownBlocks).eq(0)
273+
expect(beforeParams.updatedAtBlock).eq(0)
274+
275+
// Indexer stake tokens
276+
await staking.connect(indexer.signer).stake(toGRT('200'))
277+
278+
// State updated
279+
const afterParams = await staking.delegationPools(indexer.address)
280+
expect(afterParams.indexingRewardCut).eq(MAX_PPM)
281+
expect(afterParams.queryFeeCut).eq(MAX_PPM)
282+
expect(afterParams.cooldownBlocks).eq(0)
283+
expect(afterParams.updatedAtBlock).eq(await latestBlock())
284+
})
266285
})
267286
})
268287

0 commit comments

Comments
 (0)