From b62cb787b70b25b0a0d0dabeb53eb96dc602b5f9 Mon Sep 17 00:00:00 2001 From: bee344 Date: Fri, 3 May 2024 20:36:26 -0300 Subject: [PATCH 1/9] fix for legacyClaimedRewards after staking v14 --- .../accounts/AccountsStakingPayoutsService.ts | 37 ++++++++++++++++--- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/src/services/accounts/AccountsStakingPayoutsService.ts b/src/services/accounts/AccountsStakingPayoutsService.ts index c05260ac0..a82fc68a8 100644 --- a/src/services/accounts/AccountsStakingPayoutsService.ts +++ b/src/services/accounts/AccountsStakingPayoutsService.ts @@ -21,7 +21,7 @@ import type { DeriveEraNominatorExposure, DeriveEraValidatorExposure, } from '@polkadot/api-derive/staking/types'; -import { Compact, Option, StorageKey, u32, u128 } from '@polkadot/types'; +import { Compact, Option, StorageKey, u16, u32, u128 } from '@polkadot/types'; import { Vec } from '@polkadot/types'; import type { AccountId, @@ -152,6 +152,7 @@ export class AccountsStakingPayoutsService extends AbstractService { const startEra = Math.max(0, sanitizedEra - (depth - 1)); const runtimeInfo = await this.api.rpc.state.getRuntimeVersion(at.hash); const isKusama = runtimeInfo.specName.toString().toLowerCase() === 'kusama'; + const stakingVersion = await historicApi.query.staking.palletVersion(); /** * Given https://github.com/polkadot-js/api/issues/5232, @@ -192,6 +193,7 @@ export class AccountsStakingPayoutsService extends AbstractService { // Create an array of `DeriveEraExposure` allErasGeneral.map((eraGeneral) => eraGeneral[0]), isKusama, + stakingVersion.toNumber(), ).catch((err: Error) => { throw this.createHttpErrorForAddr(address, err); }); @@ -224,7 +226,9 @@ export class AccountsStakingPayoutsService extends AbstractService { return { at, - erasPayouts: allEraData.map((eraData) => this.deriveEraPayouts(address, unclaimedOnly, eraData, isKusama)), + erasPayouts: allEraData.map((eraData) => + this.deriveEraPayouts(address, unclaimedOnly, eraData, isKusama, Number(stakingVersion)), + ), }; } @@ -327,6 +331,7 @@ export class AccountsStakingPayoutsService extends AbstractService { startEra: number, deriveErasExposures: IAdjustedDeriveEraExposure[], isKusama: boolean, + stakingVersion: number, ): Promise { // Cache StakingLedger to reduce redundant queries to node const validatorLedgerCache: { [id: string]: PalletStakingStakingLedger } = {}; @@ -341,7 +346,14 @@ export class AccountsStakingPayoutsService extends AbstractService { } const singleEraCommissions = nominatedExposures.map(({ validatorId }) => - this.fetchCommissionAndLedger(historicApi, validatorId, currEra, validatorLedgerCache, isKusama), + this.fetchCommissionAndLedger( + historicApi, + validatorId, + currEra, + validatorLedgerCache, + isKusama, + stakingVersion, + ), ); return Promise.all(singleEraCommissions); @@ -362,6 +374,7 @@ export class AccountsStakingPayoutsService extends AbstractService { unclaimedOnly: boolean, { deriveEraExposure, eraRewardPoints, erasValidatorRewardOption, exposuresWithCommission, eraIndex }: IEraData, isKusama: boolean, + stakingVersion: number, ): IEraPayouts | { message: string } { if (!exposuresWithCommission) { return { @@ -409,8 +422,10 @@ export class AccountsStakingPayoutsService extends AbstractService { * any reward data. */ let indexOfEra: number; - if (validatorLedger.legacyClaimedRewards) { + if (validatorLedger.legacyClaimedRewards && stakingVersion < 14) { indexOfEra = validatorLedger.legacyClaimedRewards.indexOf(eraIndex); + } else if (validatorLedger.legacyClaimedRewards && stakingVersion >= 14) { + indexOfEra = 0; } else if ((validatorLedger as unknown as StakingLedger).claimedRewards) { indexOfEra = (validatorLedger as unknown as StakingLedger).claimedRewards.indexOf(eraIndex); } else if ((validatorLedger as unknown as StakingLedgerTo240).lastReward) { @@ -471,6 +486,7 @@ export class AccountsStakingPayoutsService extends AbstractService { era: number, validatorLedgerCache: { [id: string]: PalletStakingStakingLedger }, isKusama: boolean, + stakingVersion: number, ): Promise { let commission: Perbill; let validatorLedger; @@ -513,9 +529,20 @@ export class AccountsStakingPayoutsService extends AbstractService { return { commission, }; + } else if (stakingVersion < 14) { + validatorLedger = validatorLedgerOption.unwrap(); + } else { + const validatorLedgerOpts = validatorLedgerOption.unwrap(); + const claimed = await historicApi.query.staking.claimedRewards(era, validatorControllerOption.unwrap()); + validatorLedger = { + stash: validatorLedgerOpts.stash, + total: validatorLedgerOpts.total, + active: validatorLedgerOpts.active, + unlocking: validatorLedgerOpts.unlocking, + legacyClaimedRewards: claimed, + } as PalletStakingStakingLedger; } - validatorLedger = validatorLedgerOption.unwrap(); validatorLedgerCache[validatorId] = validatorLedger; } From 16b55375d5ab6f4ea458e0ffcc673d072ca88971 Mon Sep 17 00:00:00 2001 From: bee344 Date: Fri, 3 May 2024 20:51:25 -0300 Subject: [PATCH 2/9] missed bug --- .../accounts/AccountsStakingPayoutsService.ts | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/services/accounts/AccountsStakingPayoutsService.ts b/src/services/accounts/AccountsStakingPayoutsService.ts index a82fc68a8..ec5255d59 100644 --- a/src/services/accounts/AccountsStakingPayoutsService.ts +++ b/src/services/accounts/AccountsStakingPayoutsService.ts @@ -532,15 +532,21 @@ export class AccountsStakingPayoutsService extends AbstractService { } else if (stakingVersion < 14) { validatorLedger = validatorLedgerOption.unwrap(); } else { - const validatorLedgerOpts = validatorLedgerOption.unwrap(); const claimed = await historicApi.query.staking.claimedRewards(era, validatorControllerOption.unwrap()); - validatorLedger = { - stash: validatorLedgerOpts.stash, - total: validatorLedgerOpts.total, - active: validatorLedgerOpts.active, - unlocking: validatorLedgerOpts.unlocking, - legacyClaimedRewards: claimed, - } as PalletStakingStakingLedger; + if (claimed.length > 0) { + const validatorLedgerOpts = validatorLedgerOption.unwrap(); + validatorLedger = { + stash: validatorLedgerOpts.stash, + total: validatorLedgerOpts.total, + active: validatorLedgerOpts.active, + unlocking: validatorLedgerOpts.unlocking, + legacyClaimedRewards: claimed, + } as PalletStakingStakingLedger; + } else { + return { + commission, + }; + } } validatorLedgerCache[validatorId] = validatorLedger; From c3426fd7ab4cdbc985bc84b5ac7bd16744789962 Mon Sep 17 00:00:00 2001 From: bee344 Date: Fri, 3 May 2024 21:25:48 -0300 Subject: [PATCH 3/9] fixed test --- .../accounts/AccountsStakingPayoutsService.spec.ts | 2 ++ src/services/test-helpers/mock/accounts/stakingPayouts.ts | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/src/services/accounts/AccountsStakingPayoutsService.spec.ts b/src/services/accounts/AccountsStakingPayoutsService.spec.ts index 6aaab6caa..a994d093a 100644 --- a/src/services/accounts/AccountsStakingPayoutsService.spec.ts +++ b/src/services/accounts/AccountsStakingPayoutsService.spec.ts @@ -30,6 +30,7 @@ import { erasValidatorPrefsAt, erasValidatorRewardAt, ledgerAt, + palletVersionAt } from '../test-helpers/mock/accounts'; import { AccountsStakingPayoutsService } from './AccountsStakingPayoutsService'; @@ -59,6 +60,7 @@ const mockHistoricApi = { erasStakersClipped: { entries: erasStakersClippedAt, }, + palletVersion: palletVersionAt, }, }, } as unknown as ApiDecoration<'promise'>; diff --git a/src/services/test-helpers/mock/accounts/stakingPayouts.ts b/src/services/test-helpers/mock/accounts/stakingPayouts.ts index 85373c8b5..e71bbf3a8 100644 --- a/src/services/test-helpers/mock/accounts/stakingPayouts.ts +++ b/src/services/test-helpers/mock/accounts/stakingPayouts.ts @@ -83,6 +83,13 @@ export const erasRewardPointsAt = (_: EraIndex) => ), ); +export const palletVersionAt = () => Promise.resolve().then(() => + api.registry.createType( + 'u16', + '13', + ), +); + export const deriveEraExposureParam = { era: api.registry.createType('EraIndex', ERA), nominators: { From c04d28144b0234f344312b244dff5bbb7a0fe52f Mon Sep 17 00:00:00 2001 From: bee344 Date: Fri, 3 May 2024 21:34:37 -0300 Subject: [PATCH 4/9] linting --- .../accounts/AccountsStakingPayoutsService.spec.ts | 2 +- src/services/test-helpers/mock/accounts/stakingPayouts.ts | 7 +------ 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/src/services/accounts/AccountsStakingPayoutsService.spec.ts b/src/services/accounts/AccountsStakingPayoutsService.spec.ts index a994d093a..40cb47235 100644 --- a/src/services/accounts/AccountsStakingPayoutsService.spec.ts +++ b/src/services/accounts/AccountsStakingPayoutsService.spec.ts @@ -30,7 +30,7 @@ import { erasValidatorPrefsAt, erasValidatorRewardAt, ledgerAt, - palletVersionAt + palletVersionAt, } from '../test-helpers/mock/accounts'; import { AccountsStakingPayoutsService } from './AccountsStakingPayoutsService'; diff --git a/src/services/test-helpers/mock/accounts/stakingPayouts.ts b/src/services/test-helpers/mock/accounts/stakingPayouts.ts index e71bbf3a8..714a1fae0 100644 --- a/src/services/test-helpers/mock/accounts/stakingPayouts.ts +++ b/src/services/test-helpers/mock/accounts/stakingPayouts.ts @@ -83,12 +83,7 @@ export const erasRewardPointsAt = (_: EraIndex) => ), ); -export const palletVersionAt = () => Promise.resolve().then(() => - api.registry.createType( - 'u16', - '13', - ), -); +export const palletVersionAt = () => Promise.resolve().then(() => api.registry.createType('u16', '13')); export const deriveEraExposureParam = { era: api.registry.createType('EraIndex', ERA), From 0dd2f5440576be8f2692ec8e20e4bd2b38d62d0e Mon Sep 17 00:00:00 2001 From: bee344 Date: Sat, 4 May 2024 22:05:52 -0300 Subject: [PATCH 5/9] remove redundant check --- src/services/accounts/AccountsStakingPayoutsService.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/services/accounts/AccountsStakingPayoutsService.ts b/src/services/accounts/AccountsStakingPayoutsService.ts index ec5255d59..64fc78f16 100644 --- a/src/services/accounts/AccountsStakingPayoutsService.ts +++ b/src/services/accounts/AccountsStakingPayoutsService.ts @@ -424,7 +424,7 @@ export class AccountsStakingPayoutsService extends AbstractService { let indexOfEra: number; if (validatorLedger.legacyClaimedRewards && stakingVersion < 14) { indexOfEra = validatorLedger.legacyClaimedRewards.indexOf(eraIndex); - } else if (validatorLedger.legacyClaimedRewards && stakingVersion >= 14) { + } else if (stakingVersion >= 14) { indexOfEra = 0; } else if ((validatorLedger as unknown as StakingLedger).claimedRewards) { indexOfEra = (validatorLedger as unknown as StakingLedger).claimedRewards.indexOf(eraIndex); From 41799cc5d196b363744c460bdd75d6402336d66a Mon Sep 17 00:00:00 2001 From: bee344 Date: Mon, 6 May 2024 08:50:09 -0300 Subject: [PATCH 6/9] bug fix --- .../accounts/AccountsStakingPayoutsService.ts | 23 ++++--------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/src/services/accounts/AccountsStakingPayoutsService.ts b/src/services/accounts/AccountsStakingPayoutsService.ts index 64fc78f16..4acf28cbb 100644 --- a/src/services/accounts/AccountsStakingPayoutsService.ts +++ b/src/services/accounts/AccountsStakingPayoutsService.ts @@ -226,9 +226,7 @@ export class AccountsStakingPayoutsService extends AbstractService { return { at, - erasPayouts: allEraData.map((eraData) => - this.deriveEraPayouts(address, unclaimedOnly, eraData, isKusama, Number(stakingVersion)), - ), + erasPayouts: allEraData.map((eraData) => this.deriveEraPayouts(address, unclaimedOnly, eraData, isKusama)), }; } @@ -374,7 +372,6 @@ export class AccountsStakingPayoutsService extends AbstractService { unclaimedOnly: boolean, { deriveEraExposure, eraRewardPoints, erasValidatorRewardOption, exposuresWithCommission, eraIndex }: IEraData, isKusama: boolean, - stakingVersion: number, ): IEraPayouts | { message: string } { if (!exposuresWithCommission) { return { @@ -422,10 +419,8 @@ export class AccountsStakingPayoutsService extends AbstractService { * any reward data. */ let indexOfEra: number; - if (validatorLedger.legacyClaimedRewards && stakingVersion < 14) { + if (validatorLedger.legacyClaimedRewards) { indexOfEra = validatorLedger.legacyClaimedRewards.indexOf(eraIndex); - } else if (stakingVersion >= 14) { - indexOfEra = 0; } else if ((validatorLedger as unknown as StakingLedger).claimedRewards) { indexOfEra = (validatorLedger as unknown as StakingLedger).claimedRewards.indexOf(eraIndex); } else if ((validatorLedger as unknown as StakingLedgerTo240).lastReward) { @@ -532,20 +527,10 @@ export class AccountsStakingPayoutsService extends AbstractService { } else if (stakingVersion < 14) { validatorLedger = validatorLedgerOption.unwrap(); } else { + validatorLedger = validatorLedgerOption.unwrap(); const claimed = await historicApi.query.staking.claimedRewards(era, validatorControllerOption.unwrap()); if (claimed.length > 0) { - const validatorLedgerOpts = validatorLedgerOption.unwrap(); - validatorLedger = { - stash: validatorLedgerOpts.stash, - total: validatorLedgerOpts.total, - active: validatorLedgerOpts.active, - unlocking: validatorLedgerOpts.unlocking, - legacyClaimedRewards: claimed, - } as PalletStakingStakingLedger; - } else { - return { - commission, - }; + validatorLedger.legacyClaimedRewards.push(era as unknown as u32); } } From f1c4da89e514dc98523c2d09223b0720a4093ff2 Mon Sep 17 00:00:00 2001 From: bee344 Date: Tue, 14 May 2024 12:18:56 -0300 Subject: [PATCH 7/9] updated eras checks --- .../accounts/AccountsStakingPayoutsService.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/services/accounts/AccountsStakingPayoutsService.ts b/src/services/accounts/AccountsStakingPayoutsService.ts index 4acf28cbb..7504a5e49 100644 --- a/src/services/accounts/AccountsStakingPayoutsService.ts +++ b/src/services/accounts/AccountsStakingPayoutsService.ts @@ -524,13 +524,17 @@ export class AccountsStakingPayoutsService extends AbstractService { return { commission, }; - } else if (stakingVersion < 14) { - validatorLedger = validatorLedgerOption.unwrap(); } else { validatorLedger = validatorLedgerOption.unwrap(); - const claimed = await historicApi.query.staking.claimedRewards(era, validatorControllerOption.unwrap()); - if (claimed.length > 0) { - validatorLedger.legacyClaimedRewards.push(era as unknown as u32); + if ( + 14 >= stakingVersion && + (await historicApi.query.staking.claimedRewards(era, validatorControllerOption.unwrap())).length === + (await historicApi.query.staking.erasStakersOverview(era, validatorControllerOption.unwrap())) + .unwrap() + .pageCount.toNumber() + ) { + const eraVal: u32 = historicApi.registry.createType('u32', era); + validatorLedger.legacyClaimedRewards.push(eraVal); } } From 3844db3eb38ea38c7327da3c7b0df36919a047dd Mon Sep 17 00:00:00 2001 From: bee344 Date: Tue, 14 May 2024 12:26:06 -0300 Subject: [PATCH 8/9] removed stakingVersion --- src/services/accounts/AccountsStakingPayoutsService.ts | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/services/accounts/AccountsStakingPayoutsService.ts b/src/services/accounts/AccountsStakingPayoutsService.ts index 7504a5e49..5883a6eb6 100644 --- a/src/services/accounts/AccountsStakingPayoutsService.ts +++ b/src/services/accounts/AccountsStakingPayoutsService.ts @@ -21,7 +21,7 @@ import type { DeriveEraNominatorExposure, DeriveEraValidatorExposure, } from '@polkadot/api-derive/staking/types'; -import { Compact, Option, StorageKey, u16, u32, u128 } from '@polkadot/types'; +import { Compact, Option, StorageKey, u32, u128 } from '@polkadot/types'; import { Vec } from '@polkadot/types'; import type { AccountId, @@ -152,7 +152,6 @@ export class AccountsStakingPayoutsService extends AbstractService { const startEra = Math.max(0, sanitizedEra - (depth - 1)); const runtimeInfo = await this.api.rpc.state.getRuntimeVersion(at.hash); const isKusama = runtimeInfo.specName.toString().toLowerCase() === 'kusama'; - const stakingVersion = await historicApi.query.staking.palletVersion(); /** * Given https://github.com/polkadot-js/api/issues/5232, @@ -193,7 +192,6 @@ export class AccountsStakingPayoutsService extends AbstractService { // Create an array of `DeriveEraExposure` allErasGeneral.map((eraGeneral) => eraGeneral[0]), isKusama, - stakingVersion.toNumber(), ).catch((err: Error) => { throw this.createHttpErrorForAddr(address, err); }); @@ -329,7 +327,6 @@ export class AccountsStakingPayoutsService extends AbstractService { startEra: number, deriveErasExposures: IAdjustedDeriveEraExposure[], isKusama: boolean, - stakingVersion: number, ): Promise { // Cache StakingLedger to reduce redundant queries to node const validatorLedgerCache: { [id: string]: PalletStakingStakingLedger } = {}; @@ -350,7 +347,6 @@ export class AccountsStakingPayoutsService extends AbstractService { currEra, validatorLedgerCache, isKusama, - stakingVersion, ), ); @@ -481,7 +477,6 @@ export class AccountsStakingPayoutsService extends AbstractService { era: number, validatorLedgerCache: { [id: string]: PalletStakingStakingLedger }, isKusama: boolean, - stakingVersion: number, ): Promise { let commission: Perbill; let validatorLedger; @@ -527,7 +522,7 @@ export class AccountsStakingPayoutsService extends AbstractService { } else { validatorLedger = validatorLedgerOption.unwrap(); if ( - 14 >= stakingVersion && + historicApi.query.staking.claimedRewards && (await historicApi.query.staking.claimedRewards(era, validatorControllerOption.unwrap())).length === (await historicApi.query.staking.erasStakersOverview(era, validatorControllerOption.unwrap())) .unwrap() From c04adb16d16c7d05c4bbfefee571e3ae8925c1e3 Mon Sep 17 00:00:00 2001 From: bee344 Date: Tue, 14 May 2024 12:26:49 -0300 Subject: [PATCH 9/9] recommit for removing stakingVersion --- src/services/accounts/AccountsStakingPayoutsService.ts | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/services/accounts/AccountsStakingPayoutsService.ts b/src/services/accounts/AccountsStakingPayoutsService.ts index 5883a6eb6..ff9498e05 100644 --- a/src/services/accounts/AccountsStakingPayoutsService.ts +++ b/src/services/accounts/AccountsStakingPayoutsService.ts @@ -341,13 +341,7 @@ export class AccountsStakingPayoutsService extends AbstractService { } const singleEraCommissions = nominatedExposures.map(({ validatorId }) => - this.fetchCommissionAndLedger( - historicApi, - validatorId, - currEra, - validatorLedgerCache, - isKusama, - ), + this.fetchCommissionAndLedger(historicApi, validatorId, currEra, validatorLedgerCache, isKusama), ); return Promise.all(singleEraCommissions);