22// SPDX-License-Identifier: Apache-2.0
33
44import type { Observable } from 'rxjs' ;
5- import type { Option , u32 } from '@polkadot/types' ;
5+ import type { Option , u32 , Vec } from '@polkadot/types' ;
66import type { AccountId , EraIndex } from '@polkadot/types/interfaces' ;
77import type { PalletStakingNominations , PalletStakingRewardDestination , PalletStakingStakingLedger , PalletStakingValidatorPrefs , SpStakingExposurePage , SpStakingPagedExposureMetadata } from '@polkadot/types/lookup' ;
88import type { AnyNumber } from '@polkadot/types-codec/types' ;
@@ -20,9 +20,14 @@ function rewardDestinationCompat (rewardDestination: PalletStakingRewardDestinat
2020 : ( rewardDestination as PalletStakingRewardDestination ) ;
2121}
2222
23- function parseDetails ( stashId : AccountId , controllerIdOpt : Option < AccountId > | null , nominatorsOpt : Option < PalletStakingNominations > , rewardDestinationOpts : Option < PalletStakingRewardDestination > | PalletStakingRewardDestination , validatorPrefs : PalletStakingValidatorPrefs , exposure : Option < SpStakingExposurePage > , stakingLedgerOpt : Option < PalletStakingStakingLedger > , exposureMeta : Option < SpStakingPagedExposureMetadata > ) : DeriveStakingQuery {
23+ function filterClaimedRewards ( api : DeriveApi , cl : number [ ] ) : Vec < u32 > {
24+ return api . registry . createType ( 'Vec<u32>' , cl . filter ( ( c ) => c !== - 1 ) ) ;
25+ }
26+
27+ function parseDetails ( api : DeriveApi , stashId : AccountId , controllerIdOpt : Option < AccountId > | null , nominatorsOpt : Option < PalletStakingNominations > , rewardDestinationOpts : Option < PalletStakingRewardDestination > | PalletStakingRewardDestination , validatorPrefs : PalletStakingValidatorPrefs , exposure : Option < SpStakingExposurePage > , stakingLedgerOpt : Option < PalletStakingStakingLedger > , exposureMeta : Option < SpStakingPagedExposureMetadata > , claimedRewards : number [ ] ) : DeriveStakingQuery {
2428 return {
2529 accountId : stashId ,
30+ claimedRewardsEras : filterClaimedRewards ( api , claimedRewards ) ,
2631 controllerId : controllerIdOpt ?. unwrapOr ( null ) || null ,
2732 exposureMeta,
2833 exposurePaged : exposure ,
@@ -59,12 +64,22 @@ function getLedgers (api: DeriveApi, optIds: (Option<AccountId> | null)[], { wit
5964 ) ;
6065}
6166
62- function getStashInfo ( api : DeriveApi , stashIds : AccountId [ ] , activeEra : EraIndex , { withController, withDestination, withExposure, withExposureMeta, withLedger, withNominations, withPrefs } : StakingQueryFlags , page : u32 | AnyNumber ) : Observable < [ ( Option < AccountId > | null ) [ ] , Option < PalletStakingNominations > [ ] , Option < PalletStakingRewardDestination > [ ] , PalletStakingValidatorPrefs [ ] , Option < SpStakingExposurePage > [ ] , Option < SpStakingPagedExposureMetadata > [ ] ] > {
67+ function getStashInfo ( api : DeriveApi , stashIds : AccountId [ ] , activeEra : EraIndex , { withClaimedRewardsEras , withController, withDestination, withExposure, withExposureMeta, withLedger, withNominations, withPrefs } : StakingQueryFlags , page : u32 | AnyNumber ) : Observable < [ ( Option < AccountId > | null ) [ ] , Option < PalletStakingNominations > [ ] , Option < PalletStakingRewardDestination > [ ] , PalletStakingValidatorPrefs [ ] , Option < SpStakingExposurePage > [ ] , Option < SpStakingPagedExposureMetadata > [ ] , number [ ] [ ] ] > {
6368 const emptyNoms = api . registry . createType < Option < PalletStakingNominations > > ( 'Option<Nominations>' ) ;
6469 const emptyRewa = api . registry . createType < Option < PalletStakingRewardDestination > > ( 'RewardDestination' ) ;
6570 const emptyExpo = api . registry . createType < Option < SpStakingExposurePage > > ( 'Option<SpStakingExposurePage>' ) ;
6671 const emptyPrefs = api . registry . createType < PalletStakingValidatorPrefs > ( 'ValidatorPrefs' ) ;
6772 const emptyExpoMeta = api . registry . createType < Option < SpStakingPagedExposureMetadata > > ( 'Option<SpStakingPagedExposureMetadata>' ) ;
73+ const emptyClaimedRewards = [ - 1 ] ;
74+
75+ const depth = Number ( api . consts . staking . historyDepth . toNumber ( ) ) ;
76+ const eras = new Array ( depth ) . fill ( 0 ) . map ( ( _ , idx ) => {
77+ if ( idx === 0 ) {
78+ return activeEra . toNumber ( ) - 1 ;
79+ }
80+
81+ return activeEra . toNumber ( ) - idx - 1 ;
82+ } ) ;
6883
6984 return combineLatest ( [
7085 withController || withLedger
@@ -84,17 +99,40 @@ function getStashInfo (api: DeriveApi, stashIds: AccountId[], activeEra: EraInde
8499 : of ( stashIds . map ( ( ) => emptyExpo ) ) ,
85100 withExposureMeta
86101 ? combineLatest ( stashIds . map ( ( s ) => api . query . staking . erasStakersOverview ( activeEra , s ) ) )
87- : of ( stashIds . map ( ( ) => emptyExpoMeta ) )
102+ : of ( stashIds . map ( ( ) => emptyExpoMeta ) ) ,
103+ withClaimedRewardsEras
104+ ? combineLatest ( stashIds . map ( ( s ) =>
105+ combineLatest ( [
106+ combineLatest ( eras . map ( ( e ) => api . query . staking . claimedRewards ( e , s ) ) ) ,
107+ combineLatest ( eras . map ( ( e ) => api . query . staking . erasStakersOverview ( e , s ) ) )
108+ ] ) )
109+ ) . pipe (
110+ map ( ( r ) => {
111+ return r . map ( ( [ stashClaimedEras , overview ] ) => {
112+ // stashClaimedEras length will match the length of eras
113+ return stashClaimedEras . map ( ( claimedReward , idx ) => {
114+ const o = overview [ idx ] . isSome && overview [ idx ] . unwrap ( ) ;
115+
116+ if ( claimedReward . length === ( o && o . pageCount . toNumber ( ) ) ) {
117+ return eras [ idx ] ;
118+ }
119+
120+ return - 1 ;
121+ } ) ;
122+ } ) ;
123+ } )
124+ )
125+ : of ( stashIds . map ( ( ) => emptyClaimedRewards ) )
88126 ] ) ;
89127}
90128
91129function getBatch ( api : DeriveApi , activeEra : EraIndex , stashIds : AccountId [ ] , flags : StakingQueryFlags , page : u32 | AnyNumber ) : Observable < DeriveStakingQuery [ ] > {
92130 return getStashInfo ( api , stashIds , activeEra , flags , page ) . pipe (
93- switchMap ( ( [ controllerIdOpt , nominatorsOpt , rewardDestination , validatorPrefs , exposure , exposureMeta ] ) : Observable < DeriveStakingQuery [ ] > =>
131+ switchMap ( ( [ controllerIdOpt , nominatorsOpt , rewardDestination , validatorPrefs , exposure , exposureMeta , claimedRewardsEras ] ) : Observable < DeriveStakingQuery [ ] > =>
94132 getLedgers ( api , controllerIdOpt , flags ) . pipe (
95133 map ( ( stakingLedgerOpts ) =>
96134 stashIds . map ( ( stashId , index ) =>
97- parseDetails ( stashId , controllerIdOpt [ index ] , nominatorsOpt [ index ] , rewardDestination [ index ] , validatorPrefs [ index ] , exposure [ index ] , stakingLedgerOpts [ index ] , exposureMeta [ index ] )
135+ parseDetails ( api , stashId , controllerIdOpt [ index ] , nominatorsOpt [ index ] , rewardDestination [ index ] , validatorPrefs [ index ] , exposure [ index ] , stakingLedgerOpts [ index ] , exposureMeta [ index ] , claimedRewardsEras [ index ] )
98136 )
99137 )
100138 )
0 commit comments