-
Notifications
You must be signed in to change notification settings - Fork 152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: added staking.claimed()
in replacement of staking.ledger().legacyClaimedRewards
#1436
base: master
Are you sure you want to change the base?
Changes from 5 commits
b62cb78
16b5537
c3426fd
c04d281
0dd2f54
41799cc
ead106a
f1c4da8
3844db3
c04adb1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<u16>(); | ||
|
||
/** | ||
* 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<ICommissionAndLedger[][]> { | ||
// 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 (stakingVersion >= 14) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we add this check here, whenever the staking pallet version is >= 14, variable There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Awesome catch! That's right, I changed it and should not happen now |
||
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<ICommissionAndLedger> { | ||
let commission: Perbill; | ||
let validatorLedger; | ||
|
@@ -513,9 +529,26 @@ export class AccountsStakingPayoutsService extends AbstractService { | |
return { | ||
commission, | ||
}; | ||
} else if (stakingVersion < 14) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have serious doubts about using By adding this check here for older eras even if Example
Payouts shown in Subscan There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I made some changes and reduced the use of the |
||
validatorLedger = validatorLedgerOption.unwrap(); | ||
} else { | ||
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 = validatorLedgerOption.unwrap(); | ||
validatorLedgerCache[validatorId] = validatorLedger; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an awesome call. Has this been around for a bit? Is it safe to use historically?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, it has been around since v13. Before that it was
api.query.staking.storageVersion()
and it returned a typeReleases
first and thenPalletStakingReleases
, that went fromV0
toV12_0_0