From accd6d895facc8beb9b3d366601553d3663f8fdb Mon Sep 17 00:00:00 2001 From: Kaczanowski Mateusz Date: Thu, 23 Feb 2023 13:50:27 +0100 Subject: [PATCH] fix: don't claim already claimed rewards --- src/claimer.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/claimer.ts b/src/claimer.ts index 8427230..25048ce 100644 --- a/src/claimer.ts +++ b/src/claimer.ts @@ -110,18 +110,28 @@ export class Claimer { private async gatherUnclaimedInfo(validatorAddress: string, validatorInfo: ValidatorInfo): Promise { + const ledger = (await this.api.derive.staking.account(validatorAddress)).stakingLedger + if (!ledger) { + throw new Error(`Could not get ledger for ${validatorAddress}`); + } + const lastReward = validatorInfo.lastReward const numOfPotentialUnclaimedPayouts = this.currentEraIndex - lastReward - 1; const unclaimedPayouts: number[] = [] + const claimedRewards: number[] = ledger.claimedRewards.map(x => x.toNumber()); + for ( let i = 1; i <= numOfPotentialUnclaimedPayouts; i++) { const idx = lastReward + i; + if (claimedRewards.includes(idx)) { + continue; + } const exposure = await this.api.query.staking.erasStakers(idx, validatorAddress); if (exposure.total.toBn().gt(new BN(0))) { unclaimedPayouts.push(idx) } } - validatorInfo.unclaimedPayouts=unclaimedPayouts + validatorInfo.unclaimedPayouts = unclaimedPayouts return unclaimedPayouts }