From a5e76d154545a1781e59b3f29172abf839b83617 Mon Sep 17 00:00:00 2001 From: tarikgul Date: Mon, 22 Apr 2024 09:24:15 -0400 Subject: [PATCH 01/11] Fix staking derives --- packages/api-derive/src/staking/query.ts | 10 +++++----- packages/api-derive/src/staking/types.ts | 5 +++-- packages/api-derive/src/staking/validators.ts | 4 ++-- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/packages/api-derive/src/staking/query.ts b/packages/api-derive/src/staking/query.ts index f850e8a89114..8c3b2add5e69 100644 --- a/packages/api-derive/src/staking/query.ts +++ b/packages/api-derive/src/staking/query.ts @@ -4,7 +4,7 @@ import type { Observable } from 'rxjs'; import type { Option } from '@polkadot/types'; import type { AccountId, EraIndex } from '@polkadot/types/interfaces'; -import type { PalletStakingNominations, PalletStakingRewardDestination, PalletStakingStakingLedger, PalletStakingValidatorPrefs, SpStakingExposure } from '@polkadot/types/lookup'; +import type { PalletStakingNominations, PalletStakingRewardDestination, PalletStakingStakingLedger, PalletStakingValidatorPrefs, SpStakingPagedExposureMetadata } from '@polkadot/types/lookup'; import type { DeriveApi, DeriveStakingQuery, StakingQueryFlags } from '../types.js'; import { combineLatest, map, of, switchMap } from 'rxjs'; @@ -19,7 +19,7 @@ function rewardDestinationCompat (rewardDestination: PalletStakingRewardDestinat : (rewardDestination as PalletStakingRewardDestination); } -function parseDetails (stashId: AccountId, controllerIdOpt: Option | null, nominatorsOpt: Option, rewardDestinationOpts: Option | PalletStakingRewardDestination, validatorPrefs: PalletStakingValidatorPrefs, exposure: SpStakingExposure, stakingLedgerOpt: Option): DeriveStakingQuery { +function parseDetails (stashId: AccountId, controllerIdOpt: Option | null, nominatorsOpt: Option, rewardDestinationOpts: Option | PalletStakingRewardDestination, validatorPrefs: PalletStakingValidatorPrefs, exposure: Option, stakingLedgerOpt: Option): DeriveStakingQuery { return { accountId: stashId, controllerId: controllerIdOpt?.unwrapOr(null) || null, @@ -57,10 +57,10 @@ function getLedgers (api: DeriveApi, optIds: (Option | null)[], { wit ); } -function getStashInfo (api: DeriveApi, stashIds: AccountId[], activeEra: EraIndex, { withController, withDestination, withExposure, withLedger, withNominations, withPrefs }: StakingQueryFlags): Observable<[(Option | null)[], Option[], Option[], PalletStakingValidatorPrefs[], SpStakingExposure[]]> { +function getStashInfo (api: DeriveApi, stashIds: AccountId[], activeEra: EraIndex, { withController, withDestination, withExposure, withLedger, withNominations, withPrefs }: StakingQueryFlags): Observable<[(Option | null)[], Option[], Option[], PalletStakingValidatorPrefs[], Option[]]> { const emptyNoms = api.registry.createType>('Option'); const emptyRewa = api.registry.createType>('RewardDestination'); - const emptyExpo = api.registry.createType('Exposure'); + const emptyExpo = api.registry.createType>('Option'); const emptyPrefs = api.registry.createType('ValidatorPrefs'); return combineLatest([ @@ -77,7 +77,7 @@ function getStashInfo (api: DeriveApi, stashIds: AccountId[], activeEra: EraInde ? combineLatest(stashIds.map((s) => api.query.staking.validators(s))) : of(stashIds.map(() => emptyPrefs)), withExposure - ? combineLatest(stashIds.map((s) => api.query.staking.erasStakers(activeEra, s))) + ? combineLatest(stashIds.map((s) => api.query.staking.erasStakersOverview>(activeEra, s))) : of(stashIds.map(() => emptyExpo)) ]); } diff --git a/packages/api-derive/src/staking/types.ts b/packages/api-derive/src/staking/types.ts index 8438bc150031..0b4e3c2667fc 100644 --- a/packages/api-derive/src/staking/types.ts +++ b/packages/api-derive/src/staking/types.ts @@ -1,8 +1,9 @@ // Copyright 2017-2024 @polkadot/api-derive authors & contributors // SPDX-License-Identifier: Apache-2.0 +import type { Option } from '@polkadot/types'; import type { AccountId, Balance, EraIndex, RewardPoint } from '@polkadot/types/interfaces'; -import type { PalletStakingRewardDestination, PalletStakingStakingLedger, PalletStakingValidatorPrefs, SpStakingExposure, SpStakingExposurePage } from '@polkadot/types/lookup'; +import type { PalletStakingRewardDestination, PalletStakingStakingLedger, PalletStakingValidatorPrefs, SpStakingExposure, SpStakingExposurePage, SpStakingPagedExposureMetadata } from '@polkadot/types/lookup'; import type { BN } from '@polkadot/util'; import type { DeriveSessionIndexes } from '../session/types.js'; @@ -115,7 +116,7 @@ export interface DeriveStakingValidators { export interface DeriveStakingStash { controllerId: AccountId | null; - exposure: SpStakingExposure; + exposure: Option; nominators: AccountId[]; rewardDestination: PalletStakingRewardDestination | null; stashId: AccountId; diff --git a/packages/api-derive/src/staking/validators.ts b/packages/api-derive/src/staking/validators.ts index 88e7c4c89414..90362f0129eb 100644 --- a/packages/api-derive/src/staking/validators.ts +++ b/packages/api-derive/src/staking/validators.ts @@ -11,11 +11,11 @@ import { memo } from '../util/index.js'; export function nextElected (instanceId: string, api: DeriveApi): () => Observable { return memo(instanceId, (): Observable => - api.query.staking.erasStakers + api.query.staking.erasStakersPaged ? api.derive.session.indexes().pipe( // only populate for next era in the last session, so track both here - entries are not // subscriptions, so we need a trigger - currentIndex acts as that trigger to refresh - switchMap(({ currentEra }) => api.query.staking.erasStakers.keys(currentEra)), + switchMap(({ currentEra }) => api.query.staking.erasStakersPaged.keys(currentEra)), map((keys) => keys.map(({ args: [, accountId] }) => accountId)) ) : api.query.staking['currentElected']() From cc46d9c570dc6e179f7912a9887331b850c292a1 Mon Sep 17 00:00:00 2001 From: tarikgul Date: Mon, 22 Apr 2024 09:43:14 -0400 Subject: [PATCH 02/11] ownexposure --- packages/api-derive/src/staking/ownExposure.ts | 6 ++++-- packages/api-derive/src/staking/types.ts | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/api-derive/src/staking/ownExposure.ts b/packages/api-derive/src/staking/ownExposure.ts index 1423af15fbf7..ee3677d25dec 100644 --- a/packages/api-derive/src/staking/ownExposure.ts +++ b/packages/api-derive/src/staking/ownExposure.ts @@ -2,7 +2,9 @@ // SPDX-License-Identifier: Apache-2.0 import type { Observable } from 'rxjs'; +import type { Option } from '@polkadot/types'; import type { EraIndex } from '@polkadot/types/interfaces'; +import type { SpStakingExposurePage } from '@polkadot/types/lookup'; import type { DeriveApi, DeriveOwnExposure } from '../types.js'; import { combineLatest, map, of } from 'rxjs'; @@ -14,8 +16,8 @@ export function _ownExposures (instanceId: string, api: DeriveApi): (accountId: return memo(instanceId, (accountId: Uint8Array | string, eras: EraIndex[], _withActive: boolean): Observable => eras.length ? combineLatest([ - combineLatest(eras.map((e) => api.query.staking.erasStakersClipped(e, accountId))), - combineLatest(eras.map((e) => api.query.staking.erasStakers(e, accountId))) + combineLatest(eras.map((e) => api.query.staking.erasStakersPaged>(e, accountId))), + combineLatest(eras.map((e) => api.query.staking.erasStakersOverview(e, accountId))) ]).pipe( map(([clp, exp]): DeriveOwnExposure[] => eras.map((era, index) => ({ clipped: clp[index], era, exposure: exp[index] })) diff --git a/packages/api-derive/src/staking/types.ts b/packages/api-derive/src/staking/types.ts index 0b4e3c2667fc..cb9f2698c10e 100644 --- a/packages/api-derive/src/staking/types.ts +++ b/packages/api-derive/src/staking/types.ts @@ -42,9 +42,9 @@ export interface DeriveStakerPoints { } export interface DeriveOwnExposure { - clipped: SpStakingExposure; + clipped: Option; era: EraIndex; - exposure: SpStakingExposure; + exposure: Option; } export interface DeriveEraExposureNominating { From 9f06c8d12403bb366de0c5e6fb427aaab5d3c4b3 Mon Sep 17 00:00:00 2001 From: tarikgul Date: Mon, 22 Apr 2024 10:11:54 -0400 Subject: [PATCH 03/11] fix query --- packages/api-derive/src/staking/query.ts | 10 +++++----- packages/api-derive/src/staking/types.ts | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/api-derive/src/staking/query.ts b/packages/api-derive/src/staking/query.ts index 8c3b2add5e69..cb1313f27d49 100644 --- a/packages/api-derive/src/staking/query.ts +++ b/packages/api-derive/src/staking/query.ts @@ -4,7 +4,7 @@ import type { Observable } from 'rxjs'; import type { Option } from '@polkadot/types'; import type { AccountId, EraIndex } from '@polkadot/types/interfaces'; -import type { PalletStakingNominations, PalletStakingRewardDestination, PalletStakingStakingLedger, PalletStakingValidatorPrefs, SpStakingPagedExposureMetadata } from '@polkadot/types/lookup'; +import type { PalletStakingNominations, PalletStakingRewardDestination, PalletStakingStakingLedger, PalletStakingValidatorPrefs, SpStakingExposurePage } from '@polkadot/types/lookup'; import type { DeriveApi, DeriveStakingQuery, StakingQueryFlags } from '../types.js'; import { combineLatest, map, of, switchMap } from 'rxjs'; @@ -19,7 +19,7 @@ function rewardDestinationCompat (rewardDestination: PalletStakingRewardDestinat : (rewardDestination as PalletStakingRewardDestination); } -function parseDetails (stashId: AccountId, controllerIdOpt: Option | null, nominatorsOpt: Option, rewardDestinationOpts: Option | PalletStakingRewardDestination, validatorPrefs: PalletStakingValidatorPrefs, exposure: Option, stakingLedgerOpt: Option): DeriveStakingQuery { +function parseDetails (stashId: AccountId, controllerIdOpt: Option | null, nominatorsOpt: Option, rewardDestinationOpts: Option | PalletStakingRewardDestination, validatorPrefs: PalletStakingValidatorPrefs, exposure: Option, stakingLedgerOpt: Option): DeriveStakingQuery { return { accountId: stashId, controllerId: controllerIdOpt?.unwrapOr(null) || null, @@ -57,10 +57,10 @@ function getLedgers (api: DeriveApi, optIds: (Option | null)[], { wit ); } -function getStashInfo (api: DeriveApi, stashIds: AccountId[], activeEra: EraIndex, { withController, withDestination, withExposure, withLedger, withNominations, withPrefs }: StakingQueryFlags): Observable<[(Option | null)[], Option[], Option[], PalletStakingValidatorPrefs[], Option[]]> { +function getStashInfo (api: DeriveApi, stashIds: AccountId[], activeEra: EraIndex, { withController, withDestination, withExposure, withLedger, withNominations, withPrefs }: StakingQueryFlags): Observable<[(Option | null)[], Option[], Option[], PalletStakingValidatorPrefs[], Option[]]> { const emptyNoms = api.registry.createType>('Option'); const emptyRewa = api.registry.createType>('RewardDestination'); - const emptyExpo = api.registry.createType>('Option'); + const emptyExpo = api.registry.createType>('Option'); const emptyPrefs = api.registry.createType('ValidatorPrefs'); return combineLatest([ @@ -77,7 +77,7 @@ function getStashInfo (api: DeriveApi, stashIds: AccountId[], activeEra: EraInde ? combineLatest(stashIds.map((s) => api.query.staking.validators(s))) : of(stashIds.map(() => emptyPrefs)), withExposure - ? combineLatest(stashIds.map((s) => api.query.staking.erasStakersOverview>(activeEra, s))) + ? combineLatest(stashIds.map((s) => api.query.staking.erasStakersPaged>(activeEra, s))) : of(stashIds.map(() => emptyExpo)) ]); } diff --git a/packages/api-derive/src/staking/types.ts b/packages/api-derive/src/staking/types.ts index cb9f2698c10e..661fa1d58609 100644 --- a/packages/api-derive/src/staking/types.ts +++ b/packages/api-derive/src/staking/types.ts @@ -116,7 +116,7 @@ export interface DeriveStakingValidators { export interface DeriveStakingStash { controllerId: AccountId | null; - exposure: Option; + exposure: Option; nominators: AccountId[]; rewardDestination: PalletStakingRewardDestination | null; stashId: AccountId; From 122c76474bff59c783e24f7b1251fdd8d8e87721 Mon Sep 17 00:00:00 2001 From: tarikgul Date: Mon, 22 Apr 2024 10:45:37 -0400 Subject: [PATCH 04/11] Add WithExposureMEta --- packages/api-derive/src/staking/query.ts | 12 ++++++++---- packages/api-derive/src/staking/types.ts | 1 + 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/packages/api-derive/src/staking/query.ts b/packages/api-derive/src/staking/query.ts index cb1313f27d49..92af3e6e625d 100644 --- a/packages/api-derive/src/staking/query.ts +++ b/packages/api-derive/src/staking/query.ts @@ -4,7 +4,7 @@ import type { Observable } from 'rxjs'; import type { Option } from '@polkadot/types'; import type { AccountId, EraIndex } from '@polkadot/types/interfaces'; -import type { PalletStakingNominations, PalletStakingRewardDestination, PalletStakingStakingLedger, PalletStakingValidatorPrefs, SpStakingExposurePage } from '@polkadot/types/lookup'; +import type { PalletStakingNominations, PalletStakingRewardDestination, PalletStakingStakingLedger, PalletStakingValidatorPrefs, SpStakingExposurePage, SpStakingPagedExposureMetadata } from '@polkadot/types/lookup'; import type { DeriveApi, DeriveStakingQuery, StakingQueryFlags } from '../types.js'; import { combineLatest, map, of, switchMap } from 'rxjs'; @@ -57,11 +57,12 @@ function getLedgers (api: DeriveApi, optIds: (Option | null)[], { wit ); } -function getStashInfo (api: DeriveApi, stashIds: AccountId[], activeEra: EraIndex, { withController, withDestination, withExposure, withLedger, withNominations, withPrefs }: StakingQueryFlags): Observable<[(Option | null)[], Option[], Option[], PalletStakingValidatorPrefs[], Option[]]> { +function getStashInfo (api: DeriveApi, stashIds: AccountId[], activeEra: EraIndex, { withController, withDestination, withExposure, withExposureMeta, withLedger, withNominations, withPrefs }: StakingQueryFlags): Observable<[(Option | null)[], Option[], Option[], PalletStakingValidatorPrefs[], Option[], Option[]]> { const emptyNoms = api.registry.createType>('Option'); const emptyRewa = api.registry.createType>('RewardDestination'); const emptyExpo = api.registry.createType>('Option'); const emptyPrefs = api.registry.createType('ValidatorPrefs'); + const emptyExpoMeta = api.registry.createType>('Option'); return combineLatest([ withController || withLedger @@ -77,8 +78,11 @@ function getStashInfo (api: DeriveApi, stashIds: AccountId[], activeEra: EraInde ? combineLatest(stashIds.map((s) => api.query.staking.validators(s))) : of(stashIds.map(() => emptyPrefs)), withExposure - ? combineLatest(stashIds.map((s) => api.query.staking.erasStakersPaged>(activeEra, s))) - : of(stashIds.map(() => emptyExpo)) + ? combineLatest(stashIds.map((s) => api.query.staking.erasStakersPaged>(activeEra, s, 0))) + : of(stashIds.map(() => emptyExpo)), + withExposureMeta + ? combineLatest(stashIds.map((s) => api.query.staking.erasStakersOverview(activeEra, s))) + : of(stashIds.map(() => emptyExpoMeta)) ]); } diff --git a/packages/api-derive/src/staking/types.ts b/packages/api-derive/src/staking/types.ts index 661fa1d58609..6b3d1a16cae6 100644 --- a/packages/api-derive/src/staking/types.ts +++ b/packages/api-derive/src/staking/types.ts @@ -161,4 +161,5 @@ export interface StakingQueryFlags { withLedger?: boolean; withNominations?: boolean; withPrefs?: boolean; + withExposureMeta?: boolean; } From 61cb48ab0d6bd14c455c49e286cb2c9fc924531f Mon Sep 17 00:00:00 2001 From: tarikgul Date: Mon, 22 Apr 2024 10:54:39 -0400 Subject: [PATCH 05/11] exposureMEta --- packages/api-derive/src/staking/query.ts | 7 ++++--- packages/api-derive/src/staking/types.ts | 1 + 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/api-derive/src/staking/query.ts b/packages/api-derive/src/staking/query.ts index 92af3e6e625d..7fdf942b8dca 100644 --- a/packages/api-derive/src/staking/query.ts +++ b/packages/api-derive/src/staking/query.ts @@ -19,11 +19,12 @@ function rewardDestinationCompat (rewardDestination: PalletStakingRewardDestinat : (rewardDestination as PalletStakingRewardDestination); } -function parseDetails (stashId: AccountId, controllerIdOpt: Option | null, nominatorsOpt: Option, rewardDestinationOpts: Option | PalletStakingRewardDestination, validatorPrefs: PalletStakingValidatorPrefs, exposure: Option, stakingLedgerOpt: Option): DeriveStakingQuery { +function parseDetails (stashId: AccountId, controllerIdOpt: Option | null, nominatorsOpt: Option, rewardDestinationOpts: Option | PalletStakingRewardDestination, validatorPrefs: PalletStakingValidatorPrefs, exposure: Option, stakingLedgerOpt: Option, exposureMeta: Option): DeriveStakingQuery { return { accountId: stashId, controllerId: controllerIdOpt?.unwrapOr(null) || null, exposure, + exposureMeta, nominators: nominatorsOpt.isSome ? nominatorsOpt.unwrap().targets : [], @@ -88,11 +89,11 @@ function getStashInfo (api: DeriveApi, stashIds: AccountId[], activeEra: EraInde function getBatch (api: DeriveApi, activeEra: EraIndex, stashIds: AccountId[], flags: StakingQueryFlags): Observable { return getStashInfo(api, stashIds, activeEra, flags).pipe( - switchMap(([controllerIdOpt, nominatorsOpt, rewardDestination, validatorPrefs, exposure]): Observable => + switchMap(([controllerIdOpt, nominatorsOpt, rewardDestination, validatorPrefs, exposure, exposureMeta]): Observable => getLedgers(api, controllerIdOpt, flags).pipe( map((stakingLedgerOpts) => stashIds.map((stashId, index) => - parseDetails(stashId, controllerIdOpt[index], nominatorsOpt[index], rewardDestination[index], validatorPrefs[index], exposure[index], stakingLedgerOpts[index]) + parseDetails(stashId, controllerIdOpt[index], nominatorsOpt[index], rewardDestination[index], validatorPrefs[index], exposure[index], stakingLedgerOpts[index], exposureMeta[index]) ) ) ) diff --git a/packages/api-derive/src/staking/types.ts b/packages/api-derive/src/staking/types.ts index 6b3d1a16cae6..78929a8ebed6 100644 --- a/packages/api-derive/src/staking/types.ts +++ b/packages/api-derive/src/staking/types.ts @@ -117,6 +117,7 @@ export interface DeriveStakingValidators { export interface DeriveStakingStash { controllerId: AccountId | null; exposure: Option; + exposureMeta: Option; nominators: AccountId[]; rewardDestination: PalletStakingRewardDestination | null; stashId: AccountId; From 2720d757e49cbd3296a8c95c7fbfcec36f917d41 Mon Sep 17 00:00:00 2001 From: tarikgul Date: Mon, 22 Apr 2024 13:41:03 -0400 Subject: [PATCH 06/11] fix ownExposures --- packages/api-derive/src/staking/ownExposure.ts | 16 ++++++++++------ packages/api-derive/src/staking/types.ts | 6 ++++-- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/packages/api-derive/src/staking/ownExposure.ts b/packages/api-derive/src/staking/ownExposure.ts index ee3677d25dec..9ee25a2e46b6 100644 --- a/packages/api-derive/src/staking/ownExposure.ts +++ b/packages/api-derive/src/staking/ownExposure.ts @@ -13,17 +13,21 @@ import { firstMemo, memo } from '../util/index.js'; import { erasHistoricApplyAccount } from './util.js'; export function _ownExposures (instanceId: string, api: DeriveApi): (accountId: Uint8Array | string, eras: EraIndex[], withActive: boolean) => Observable { - return memo(instanceId, (accountId: Uint8Array | string, eras: EraIndex[], _withActive: boolean): Observable => - eras.length + return memo(instanceId, (accountId: Uint8Array | string, eras: EraIndex[], _withActive: boolean): Observable => { + return eras.length ? combineLatest([ - combineLatest(eras.map((e) => api.query.staking.erasStakersPaged>(e, accountId))), + // Backwards and forward compat for historical integrity when using `erasHistoricApplyAccount` + combineLatest(eras.map((e) => api.query.staking.erasStakersClipped(e, accountId))), + combineLatest(eras.map((e) => api.query.staking.erasStakers(e, accountId))), + combineLatest(eras.map((e) => api.query.staking.erasStakersPaged>(e, accountId, 0))), combineLatest(eras.map((e) => api.query.staking.erasStakersOverview(e, accountId))) ]).pipe( - map(([clp, exp]): DeriveOwnExposure[] => - eras.map((era, index) => ({ clipped: clp[index], era, exposure: exp[index] })) + map(([clp, exp, paged, expMeta]): DeriveOwnExposure[] => + eras.map((era, index) => ({ clipped: clp[index], era, exposure: exp[index], exposureMeta: expMeta[index], paged: paged[index] })) ) ) - : of([]) + : of([]); + } ); } diff --git a/packages/api-derive/src/staking/types.ts b/packages/api-derive/src/staking/types.ts index 78929a8ebed6..b8fed4a2d493 100644 --- a/packages/api-derive/src/staking/types.ts +++ b/packages/api-derive/src/staking/types.ts @@ -42,9 +42,11 @@ export interface DeriveStakerPoints { } export interface DeriveOwnExposure { - clipped: Option; + clipped: SpStakingExposure; + paged: Option; era: EraIndex; - exposure: Option; + exposure: SpStakingExposure; + exposureMeta: Option; } export interface DeriveEraExposureNominating { From 85e9da0efcbaf84285b198fefa68968eef6312ba Mon Sep 17 00:00:00 2001 From: tarikgul Date: Mon, 22 Apr 2024 14:05:03 -0400 Subject: [PATCH 07/11] Add page input for staking.query --- packages/api-derive/src/staking/query.ts | 23 ++++++++++++----------- packages/api-derive/src/staking/types.ts | 2 +- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/packages/api-derive/src/staking/query.ts b/packages/api-derive/src/staking/query.ts index 7fdf942b8dca..2e451782191d 100644 --- a/packages/api-derive/src/staking/query.ts +++ b/packages/api-derive/src/staking/query.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { Observable } from 'rxjs'; -import type { Option } from '@polkadot/types'; +import type { Option, u32 } from '@polkadot/types'; import type { AccountId, EraIndex } from '@polkadot/types/interfaces'; import type { PalletStakingNominations, PalletStakingRewardDestination, PalletStakingStakingLedger, PalletStakingValidatorPrefs, SpStakingExposurePage, SpStakingPagedExposureMetadata } from '@polkadot/types/lookup'; import type { DeriveApi, DeriveStakingQuery, StakingQueryFlags } from '../types.js'; @@ -23,8 +23,8 @@ function parseDetails (stashId: AccountId, controllerIdOpt: Option | return { accountId: stashId, controllerId: controllerIdOpt?.unwrapOr(null) || null, - exposure, exposureMeta, + exposurePaged: exposure, nominators: nominatorsOpt.isSome ? nominatorsOpt.unwrap().targets : [], @@ -58,7 +58,7 @@ function getLedgers (api: DeriveApi, optIds: (Option | null)[], { wit ); } -function getStashInfo (api: DeriveApi, stashIds: AccountId[], activeEra: EraIndex, { withController, withDestination, withExposure, withExposureMeta, withLedger, withNominations, withPrefs }: StakingQueryFlags): Observable<[(Option | null)[], Option[], Option[], PalletStakingValidatorPrefs[], Option[], Option[]]> { +function getStashInfo (api: DeriveApi, stashIds: AccountId[], activeEra: EraIndex, { withController, withDestination, withExposure, withExposureMeta, withLedger, withNominations, withPrefs }: StakingQueryFlags, page: u32 | number): Observable<[(Option | null)[], Option[], Option[], PalletStakingValidatorPrefs[], Option[], Option[]]> { const emptyNoms = api.registry.createType>('Option'); const emptyRewa = api.registry.createType>('RewardDestination'); const emptyExpo = api.registry.createType>('Option'); @@ -79,7 +79,7 @@ function getStashInfo (api: DeriveApi, stashIds: AccountId[], activeEra: EraInde ? combineLatest(stashIds.map((s) => api.query.staking.validators(s))) : of(stashIds.map(() => emptyPrefs)), withExposure - ? combineLatest(stashIds.map((s) => api.query.staking.erasStakersPaged>(activeEra, s, 0))) + ? combineLatest(stashIds.map((s) => api.query.staking.erasStakersPaged>(activeEra, s, page))) : of(stashIds.map(() => emptyExpo)), withExposureMeta ? combineLatest(stashIds.map((s) => api.query.staking.erasStakersOverview(activeEra, s))) @@ -87,8 +87,8 @@ function getStashInfo (api: DeriveApi, stashIds: AccountId[], activeEra: EraInde ]); } -function getBatch (api: DeriveApi, activeEra: EraIndex, stashIds: AccountId[], flags: StakingQueryFlags): Observable { - return getStashInfo(api, stashIds, activeEra, flags).pipe( +function getBatch (api: DeriveApi, activeEra: EraIndex, stashIds: AccountId[], flags: StakingQueryFlags, page: u32 | number): Observable { + return getStashInfo(api, stashIds, activeEra, flags, page).pipe( switchMap(([controllerIdOpt, nominatorsOpt, rewardDestination, validatorPrefs, exposure, exposureMeta]): Observable => getLedgers(api, controllerIdOpt, flags).pipe( map((stakingLedgerOpts) => @@ -106,18 +106,19 @@ function getBatch (api: DeriveApi, activeEra: EraIndex, stashIds: AccountId[], f * @description From a stash, retrieve the controllerId and all relevant details */ export const query = /*#__PURE__*/ firstMemo( - (api: DeriveApi, accountId: Uint8Array | string, flags: StakingQueryFlags) => - api.derive.staking.queryMulti([accountId], flags) + (api: DeriveApi, accountId: Uint8Array | string, flags: StakingQueryFlags, page?: u32) => + api.derive.staking.queryMulti([accountId], flags, page) ); -export function queryMulti (instanceId: string, api: DeriveApi): (accountIds: (Uint8Array | string)[], flags: StakingQueryFlags) => Observable { - return memo(instanceId, (accountIds: (Uint8Array | string)[], flags: StakingQueryFlags): Observable => +export function queryMulti (instanceId: string, api: DeriveApi): (accountIds: (Uint8Array | string)[], flags: StakingQueryFlags, page?: u32 | number) => Observable { + return memo(instanceId, (accountIds: (Uint8Array | string)[], flags: StakingQueryFlags, page?: u32 | number): Observable => api.derive.session.indexes().pipe( switchMap(({ activeEra }): Observable => { const stashIds = accountIds.map((a) => api.registry.createType('AccountId', a)); + const p = page || api.registry.createType('u32', 0); return stashIds.length - ? getBatch(api, activeEra, stashIds, flags) + ? getBatch(api, activeEra, stashIds, flags, p) : of([]); }) ) diff --git a/packages/api-derive/src/staking/types.ts b/packages/api-derive/src/staking/types.ts index b8fed4a2d493..76312761236d 100644 --- a/packages/api-derive/src/staking/types.ts +++ b/packages/api-derive/src/staking/types.ts @@ -118,7 +118,7 @@ export interface DeriveStakingValidators { export interface DeriveStakingStash { controllerId: AccountId | null; - exposure: Option; + exposurePaged: Option; exposureMeta: Option; nominators: AccountId[]; rewardDestination: PalletStakingRewardDestination | null; From 96fe0d97a24e553180aa1bfe70a236fb23273431 Mon Sep 17 00:00:00 2001 From: tarikgul Date: Mon, 22 Apr 2024 14:10:49 -0400 Subject: [PATCH 08/11] Update types for query --- packages/api-derive/src/staking/query.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/api-derive/src/staking/query.ts b/packages/api-derive/src/staking/query.ts index 2e451782191d..796ddd61ea99 100644 --- a/packages/api-derive/src/staking/query.ts +++ b/packages/api-derive/src/staking/query.ts @@ -5,6 +5,7 @@ import type { Observable } from 'rxjs'; import type { Option, u32 } from '@polkadot/types'; import type { AccountId, EraIndex } from '@polkadot/types/interfaces'; import type { PalletStakingNominations, PalletStakingRewardDestination, PalletStakingStakingLedger, PalletStakingValidatorPrefs, SpStakingExposurePage, SpStakingPagedExposureMetadata } from '@polkadot/types/lookup'; +import type { AnyNumber } from '@polkadot/types-codec/types'; import type { DeriveApi, DeriveStakingQuery, StakingQueryFlags } from '../types.js'; import { combineLatest, map, of, switchMap } from 'rxjs'; @@ -58,7 +59,7 @@ function getLedgers (api: DeriveApi, optIds: (Option | null)[], { wit ); } -function getStashInfo (api: DeriveApi, stashIds: AccountId[], activeEra: EraIndex, { withController, withDestination, withExposure, withExposureMeta, withLedger, withNominations, withPrefs }: StakingQueryFlags, page: u32 | number): Observable<[(Option | null)[], Option[], Option[], PalletStakingValidatorPrefs[], Option[], Option[]]> { +function getStashInfo (api: DeriveApi, stashIds: AccountId[], activeEra: EraIndex, { withController, withDestination, withExposure, withExposureMeta, withLedger, withNominations, withPrefs }: StakingQueryFlags, page: u32 | AnyNumber): Observable<[(Option | null)[], Option[], Option[], PalletStakingValidatorPrefs[], Option[], Option[]]> { const emptyNoms = api.registry.createType>('Option'); const emptyRewa = api.registry.createType>('RewardDestination'); const emptyExpo = api.registry.createType>('Option'); @@ -87,7 +88,7 @@ function getStashInfo (api: DeriveApi, stashIds: AccountId[], activeEra: EraInde ]); } -function getBatch (api: DeriveApi, activeEra: EraIndex, stashIds: AccountId[], flags: StakingQueryFlags, page: u32 | number): Observable { +function getBatch (api: DeriveApi, activeEra: EraIndex, stashIds: AccountId[], flags: StakingQueryFlags, page: u32 | AnyNumber): Observable { return getStashInfo(api, stashIds, activeEra, flags, page).pipe( switchMap(([controllerIdOpt, nominatorsOpt, rewardDestination, validatorPrefs, exposure, exposureMeta]): Observable => getLedgers(api, controllerIdOpt, flags).pipe( @@ -110,12 +111,12 @@ export const query = /*#__PURE__*/ firstMemo( api.derive.staking.queryMulti([accountId], flags, page) ); -export function queryMulti (instanceId: string, api: DeriveApi): (accountIds: (Uint8Array | string)[], flags: StakingQueryFlags, page?: u32 | number) => Observable { - return memo(instanceId, (accountIds: (Uint8Array | string)[], flags: StakingQueryFlags, page?: u32 | number): Observable => +export function queryMulti (instanceId: string, api: DeriveApi): (accountIds: (Uint8Array | string)[], flags: StakingQueryFlags, page?: u32 | AnyNumber) => Observable { + return memo(instanceId, (accountIds: (Uint8Array | string)[], flags: StakingQueryFlags, page?: u32 | AnyNumber): Observable => api.derive.session.indexes().pipe( switchMap(({ activeEra }): Observable => { const stashIds = accountIds.map((a) => api.registry.createType('AccountId', a)); - const p = page || api.registry.createType('u32', 0); + const p = page || 0; return stashIds.length ? getBatch(api, activeEra, stashIds, flags, p) From 644192990f1ea3eac335a0879d0c36792925a488 Mon Sep 17 00:00:00 2001 From: tarikgul Date: Mon, 22 Apr 2024 14:26:59 -0400 Subject: [PATCH 09/11] add page param to ownExposure --- packages/api-derive/src/staking/ownExposure.ts | 13 +++++++------ packages/api-derive/src/staking/util.ts | 6 ++++-- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/packages/api-derive/src/staking/ownExposure.ts b/packages/api-derive/src/staking/ownExposure.ts index 9ee25a2e46b6..7ab85c93a3ac 100644 --- a/packages/api-derive/src/staking/ownExposure.ts +++ b/packages/api-derive/src/staking/ownExposure.ts @@ -2,9 +2,10 @@ // SPDX-License-Identifier: Apache-2.0 import type { Observable } from 'rxjs'; -import type { Option } from '@polkadot/types'; +import type { Option, u32 } from '@polkadot/types'; import type { EraIndex } from '@polkadot/types/interfaces'; import type { SpStakingExposurePage } from '@polkadot/types/lookup'; +import type { AnyNumber } from '@polkadot/types-codec/types'; import type { DeriveApi, DeriveOwnExposure } from '../types.js'; import { combineLatest, map, of } from 'rxjs'; @@ -12,14 +13,14 @@ import { combineLatest, map, of } from 'rxjs'; import { firstMemo, memo } from '../util/index.js'; import { erasHistoricApplyAccount } from './util.js'; -export function _ownExposures (instanceId: string, api: DeriveApi): (accountId: Uint8Array | string, eras: EraIndex[], withActive: boolean) => Observable { - return memo(instanceId, (accountId: Uint8Array | string, eras: EraIndex[], _withActive: boolean): Observable => { +export function _ownExposures (instanceId: string, api: DeriveApi): (accountId: Uint8Array | string, eras: EraIndex[], withActive: boolean, page: u32 | AnyNumber) => Observable { + return memo(instanceId, (accountId: Uint8Array | string, eras: EraIndex[], _withActive: boolean, page?: u32 | AnyNumber): Observable => { return eras.length ? combineLatest([ // Backwards and forward compat for historical integrity when using `erasHistoricApplyAccount` combineLatest(eras.map((e) => api.query.staking.erasStakersClipped(e, accountId))), combineLatest(eras.map((e) => api.query.staking.erasStakers(e, accountId))), - combineLatest(eras.map((e) => api.query.staking.erasStakersPaged>(e, accountId, 0))), + combineLatest(eras.map((e) => api.query.staking.erasStakersPaged>(e, accountId, page || 0))), combineLatest(eras.map((e) => api.query.staking.erasStakersOverview(e, accountId))) ]).pipe( map(([clp, exp, paged, expMeta]): DeriveOwnExposure[] => @@ -32,8 +33,8 @@ export function _ownExposures (instanceId: string, api: DeriveApi): (accountId: } export const ownExposure = /*#__PURE__*/ firstMemo( - (api: DeriveApi, accountId: Uint8Array | string, era: EraIndex) => - api.derive.staking._ownExposures(accountId, [era], true) + (api: DeriveApi, accountId: Uint8Array | string, era: EraIndex, page?: u32 | AnyNumber) => + api.derive.staking._ownExposures(accountId, [era], true, page || 0) ); export const ownExposures = /*#__PURE__*/ erasHistoricApplyAccount('_ownExposures'); diff --git a/packages/api-derive/src/staking/util.ts b/packages/api-derive/src/staking/util.ts index 2a9a5c20470c..343cc7b75126 100644 --- a/packages/api-derive/src/staking/util.ts +++ b/packages/api-derive/src/staking/util.ts @@ -3,7 +3,9 @@ import type { Observable } from 'rxjs'; import type { ObsInnerType } from '@polkadot/api-base/types'; +import type { u32 } from '@polkadot/types'; import type { EraIndex } from '@polkadot/types/interfaces'; +import type { AnyNumber } from '@polkadot/types-codec/types'; import type { ExactDerive } from '../derive.js'; import type { DeriveApi } from '../types.js'; @@ -60,9 +62,9 @@ export function erasHistoricApplyAccount // Cannot quite get the typing right, but it is right in the code // eslint-disable-next-line @typescript-eslint/no-unsafe-return - memo(instanceId, (accountId: string | Uint8Array, withActive = false) => + memo(instanceId, (accountId: string | Uint8Array, withActive = false, page?: u32 | AnyNumber) => api.derive.staking.erasHistoric(withActive).pipe( - switchMap((e) => api.derive.staking[fn](accountId, e, withActive)) + switchMap((e) => api.derive.staking[fn](accountId, e, withActive, page || 0)) ) ) as any; } From 0f188a8189e9caeb404976f221fafa5517b551fd Mon Sep 17 00:00:00 2001 From: tarikgul Date: Mon, 22 Apr 2024 14:57:57 -0400 Subject: [PATCH 10/11] change page to exposurePaged --- packages/api-derive/src/staking/ownExposure.ts | 2 +- packages/api-derive/src/staking/types.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/api-derive/src/staking/ownExposure.ts b/packages/api-derive/src/staking/ownExposure.ts index 7ab85c93a3ac..22305137e823 100644 --- a/packages/api-derive/src/staking/ownExposure.ts +++ b/packages/api-derive/src/staking/ownExposure.ts @@ -24,7 +24,7 @@ export function _ownExposures (instanceId: string, api: DeriveApi): (accountId: combineLatest(eras.map((e) => api.query.staking.erasStakersOverview(e, accountId))) ]).pipe( map(([clp, exp, paged, expMeta]): DeriveOwnExposure[] => - eras.map((era, index) => ({ clipped: clp[index], era, exposure: exp[index], exposureMeta: expMeta[index], paged: paged[index] })) + eras.map((era, index) => ({ clipped: clp[index], era, exposure: exp[index], exposureMeta: expMeta[index], exposurePaged: paged[index] })) ) ) : of([]); diff --git a/packages/api-derive/src/staking/types.ts b/packages/api-derive/src/staking/types.ts index 76312761236d..6f844987ab04 100644 --- a/packages/api-derive/src/staking/types.ts +++ b/packages/api-derive/src/staking/types.ts @@ -43,7 +43,7 @@ export interface DeriveStakerPoints { export interface DeriveOwnExposure { clipped: SpStakingExposure; - paged: Option; + exposurePaged: Option; era: EraIndex; exposure: SpStakingExposure; exposureMeta: Option; From 92f522a253f301522cf153a58bbefe264b4d4c58 Mon Sep 17 00:00:00 2001 From: tarikgul Date: Mon, 22 Apr 2024 15:19:59 -0400 Subject: [PATCH 11/11] fix small nit --- packages/api-derive/src/staking/ownExposure.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/api-derive/src/staking/ownExposure.ts b/packages/api-derive/src/staking/ownExposure.ts index 22305137e823..b20c057a9387 100644 --- a/packages/api-derive/src/staking/ownExposure.ts +++ b/packages/api-derive/src/staking/ownExposure.ts @@ -14,13 +14,13 @@ import { firstMemo, memo } from '../util/index.js'; import { erasHistoricApplyAccount } from './util.js'; export function _ownExposures (instanceId: string, api: DeriveApi): (accountId: Uint8Array | string, eras: EraIndex[], withActive: boolean, page: u32 | AnyNumber) => Observable { - return memo(instanceId, (accountId: Uint8Array | string, eras: EraIndex[], _withActive: boolean, page?: u32 | AnyNumber): Observable => { + return memo(instanceId, (accountId: Uint8Array | string, eras: EraIndex[], _withActive: boolean, page: u32 | AnyNumber): Observable => { return eras.length ? combineLatest([ // Backwards and forward compat for historical integrity when using `erasHistoricApplyAccount` combineLatest(eras.map((e) => api.query.staking.erasStakersClipped(e, accountId))), combineLatest(eras.map((e) => api.query.staking.erasStakers(e, accountId))), - combineLatest(eras.map((e) => api.query.staking.erasStakersPaged>(e, accountId, page || 0))), + combineLatest(eras.map((e) => api.query.staking.erasStakersPaged>(e, accountId, page))), combineLatest(eras.map((e) => api.query.staking.erasStakersOverview(e, accountId))) ]).pipe( map(([clp, exp, paged, expMeta]): DeriveOwnExposure[] =>