Additional query opts (flags) (#2899)
This commit is contained in:
@@ -12,6 +12,13 @@ import { BN_ZERO } from '@polkadot/util';
|
||||
|
||||
import { memo } from '../util';
|
||||
|
||||
const QUERY_OPTS = {
|
||||
withDestination: true,
|
||||
withLedger: true,
|
||||
withNominations: true,
|
||||
withPrefs: true
|
||||
};
|
||||
|
||||
function groupByEra (list: UnlockChunk[]): Record<string, BN> {
|
||||
return list.reduce((map: Record<string, BN>, { era, value }): Record<string, BN> => {
|
||||
const key = era.toString();
|
||||
@@ -63,7 +70,7 @@ export function accounts (instanceId: string, api: ApiInterfaceRx): (accountIds:
|
||||
switchMap((sessionInfo) =>
|
||||
combineLatest([
|
||||
api.derive.staking.keysMulti(accountIds),
|
||||
api.derive.staking.queryMulti(accountIds, true)
|
||||
api.derive.staking.queryMulti(accountIds, QUERY_OPTS)
|
||||
]).pipe(
|
||||
map(([keys, queries]) => queries.map((query, index) => parseResult(api, sessionInfo, keys[index], query)))
|
||||
)
|
||||
|
||||
@@ -18,7 +18,7 @@ export function electedInfo (instanceId: string, api: ApiInterfaceRx): () => Obs
|
||||
return memo(instanceId, (): Observable<DeriveStakingElected> =>
|
||||
api.derive.staking.validators().pipe(
|
||||
switchMap(({ nextElected, validators }): Observable<DeriveStakingElected> =>
|
||||
api.derive.staking.queryMulti(combineAccounts(nextElected, validators), false).pipe(
|
||||
api.derive.staking.queryMulti(combineAccounts(nextElected, validators), { withExposure: true, withPrefs: true }).pipe(
|
||||
map((info): DeriveStakingElected => ({
|
||||
info,
|
||||
nextElected,
|
||||
|
||||
@@ -22,5 +22,4 @@ export * from './stakerRewards';
|
||||
export * from './stakerSlashes';
|
||||
export * from './stashes';
|
||||
export * from './validators';
|
||||
export * from './validatorsFrom';
|
||||
export * from './waitingInfo';
|
||||
|
||||
@@ -14,6 +14,14 @@ import { isFunction } from '@polkadot/util';
|
||||
|
||||
import { memo } from '../util';
|
||||
|
||||
interface QueryFlags {
|
||||
withDestination?: boolean;
|
||||
withExposure?: boolean;
|
||||
withLedger?: boolean;
|
||||
withNominations?: boolean;
|
||||
withPrefs?: boolean;
|
||||
}
|
||||
|
||||
type MultiResultPrev = [Option<AccountId>, Option<ITuple<[Nominations]>>, RewardDestination, ITuple<[ValidatorPrefs]> | ValidatorPrefs, Exposure];
|
||||
|
||||
type MultiResultCombo = [Option<AccountId>, Option<ITuple<[Nominations]> | Nominations>, RewardDestination, ITuple<[ValidatorPrefs]> | ValidatorPrefs, Exposure];
|
||||
@@ -49,23 +57,29 @@ function retrievePrev (api: ApiInterfaceRx, stashId: AccountId): Observable<Mult
|
||||
]);
|
||||
}
|
||||
|
||||
function retrieveCurr (api: ApiInterfaceRx, stashIds: AccountId[], activeEra: EraIndex, withFull: boolean): Observable<MultiResultCombo[]> {
|
||||
function retrieveCurr (api: ApiInterfaceRx, stashIds: AccountId[], activeEra: EraIndex, { withDestination, withExposure, withLedger, withNominations, withPrefs }: QueryFlags): Observable<MultiResultCombo[]> {
|
||||
const emptyCont = api.registry.createType('Option<AccountId>');
|
||||
const emptyNoms = api.registry.createType('Option<Nominations>');
|
||||
const emptyRewa = api.registry.createType('RewardDestination');
|
||||
const emptyExpo = api.registry.createType('Exposure');
|
||||
const emptyPrefs = api.registry.createType('ValidatorPrefs');
|
||||
|
||||
return combineLatest([
|
||||
withFull
|
||||
withLedger
|
||||
? api.query.staking.bonded.multi<Option<AccountId>>(stashIds)
|
||||
: of(stashIds.map(() => emptyCont)),
|
||||
withFull && api.query.staking.nominators
|
||||
withNominations && api.query.staking.nominators
|
||||
? api.query.staking.nominators.multi<Option<Nominations>>(stashIds)
|
||||
: of(stashIds.map(() => emptyNoms)),
|
||||
withFull
|
||||
withDestination
|
||||
? api.query.staking.payee.multi<RewardDestination>(stashIds)
|
||||
: of(stashIds.map(() => emptyRewa)),
|
||||
api.query.staking.validators.multi<ValidatorPrefs>(stashIds),
|
||||
api.query.staking.erasStakers.multi<Exposure>(stashIds.map((stashId) => [activeEra, stashId]))
|
||||
withPrefs
|
||||
? api.query.staking.validators.multi<ValidatorPrefs>(stashIds)
|
||||
: of(stashIds.map(() => emptyPrefs)),
|
||||
withExposure
|
||||
? api.query.staking.erasStakers.multi<Exposure>(stashIds.map((stashId) => [activeEra, stashId]))
|
||||
: of(stashIds.map(() => emptyExpo))
|
||||
]).pipe(
|
||||
map(([controllerIdOpt, nominatorsOpt, rewardDestination, validatorPrefs, exposure]): MultiResultCombo[] =>
|
||||
controllerIdOpt.map((controllerIdOpt, index): MultiResultCombo =>
|
||||
@@ -99,38 +113,33 @@ function retrieveControllers (api: ApiInterfaceRx, optControllerIds: Option<Acco
|
||||
/**
|
||||
* @description From a stash, retrieve the controllerId and all relevant details
|
||||
*/
|
||||
export function query (instanceId: string, api: ApiInterfaceRx): (accountId: Uint8Array | string, withFull?: boolean) => Observable<DeriveStakingQuery> {
|
||||
return memo(instanceId, (accountId: Uint8Array | string, withFull?: boolean): Observable<DeriveStakingQuery> =>
|
||||
api.derive.staking.queryMulti([accountId], withFull).pipe(
|
||||
export function query (instanceId: string, api: ApiInterfaceRx): (accountId: Uint8Array | string, flags: QueryFlags) => Observable<DeriveStakingQuery> {
|
||||
return memo(instanceId, (accountId: Uint8Array | string, flags: QueryFlags): Observable<DeriveStakingQuery> =>
|
||||
api.derive.staking.queryMulti([accountId], flags).pipe(
|
||||
map(([first]) => first)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
export function queryMulti (instanceId: string, api: ApiInterfaceRx): (accountIds: (Uint8Array | string)[], withFull?: boolean) => Observable<DeriveStakingQuery[]> {
|
||||
return memo(instanceId, (accountIds: (Uint8Array | string)[], withFull = true): Observable<DeriveStakingQuery[]> =>
|
||||
export function queryMulti (instanceId: string, api: ApiInterfaceRx): (accountIds: (Uint8Array | string)[], flags: QueryFlags) => Observable<DeriveStakingQuery[]> {
|
||||
return memo(instanceId, (accountIds: (Uint8Array | string)[], flags: QueryFlags): Observable<DeriveStakingQuery[]> =>
|
||||
accountIds.length
|
||||
? api.derive.session.indexes().pipe(
|
||||
switchMap(({ activeEra }): Observable<DeriveStakingQuery[]> => {
|
||||
const stashIds = accountIds.map((accountId) => api.registry.createType('AccountId', accountId));
|
||||
const emptyLed = api.registry.createType('Option<StakingLedger>');
|
||||
|
||||
return (
|
||||
isFunction(api.query.staking.erasStakers)
|
||||
? retrieveCurr(api, stashIds, activeEra, withFull)
|
||||
? retrieveCurr(api, stashIds, activeEra, flags)
|
||||
: combineLatest(stashIds.map((stashId) => retrievePrev(api, stashId)))
|
||||
).pipe(
|
||||
withFull
|
||||
? switchMap((results): Observable<DeriveStakingQuery[]> =>
|
||||
retrieveControllers(api, results.map(([optController]) => optController)).pipe(
|
||||
map((stakingLedgerOpts) =>
|
||||
stashIds.map((stashId, index) => parseDetails(stashId, results[index], stakingLedgerOpts[index]))
|
||||
)
|
||||
switchMap((results): Observable<DeriveStakingQuery[]> =>
|
||||
retrieveControllers(api, results.map(([optController]) => optController)).pipe(
|
||||
map((stakingLedgerOpts) =>
|
||||
stashIds.map((stashId, index) => parseDetails(stashId, results[index], stakingLedgerOpts[index]))
|
||||
)
|
||||
)
|
||||
: map((results): DeriveStakingQuery[] =>
|
||||
stashIds.map((stashId, index) => parseDetails(stashId, results[index], emptyLed))
|
||||
)
|
||||
)
|
||||
);
|
||||
})
|
||||
)
|
||||
|
||||
@@ -127,7 +127,7 @@ function filterRewards (api: ApiInterfaceRx, eras: EraIndex[], { migrateEra, rew
|
||||
|
||||
return (
|
||||
isFunction(api.tx.staking.payoutStakers)
|
||||
? api.derive.staking.queryMulti(validators, false)
|
||||
? api.derive.staking.queryMulti(validators, { withLedger: true })
|
||||
: of([])
|
||||
).pipe(
|
||||
map((queryValidators): DeriveStakerReward[] =>
|
||||
@@ -172,7 +172,7 @@ export function _stakerRewardsEras (instanceId: string, api: ApiInterfaceRx): (e
|
||||
export function _stakerRewards (instanceId: string, api: ApiInterfaceRx): (accountId: Uint8Array | string, eras: EraIndex[], withActive: boolean) => Observable<DeriveStakerReward[]> {
|
||||
return memo(instanceId, (accountId: Uint8Array | string, eras: EraIndex[], withActive: boolean): Observable<DeriveStakerReward[]> =>
|
||||
combineLatest([
|
||||
api.derive.staking.query(accountId),
|
||||
api.derive.staking.query(accountId, { withLedger: true }),
|
||||
api.derive.staking._stakerExposure(accountId, eras, withActive),
|
||||
api.derive.staking._stakerRewardsEras(eras, withActive)
|
||||
]).pipe(
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Observable } from 'rxjs';
|
||||
import type { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
import type { Option } from '@polkadot/types';
|
||||
import type { AccountId, Nominations } from '@polkadot/types/interfaces';
|
||||
import type { DeriveStakingQuery } from '../types';
|
||||
|
||||
import { switchMap } from 'rxjs/operators';
|
||||
|
||||
import { memo } from '../util';
|
||||
|
||||
/**
|
||||
* @description From a list of stashIds, staking ledgers for those acting as validators and those being nominated
|
||||
*/
|
||||
export function validatorsFrom (instanceId: string, api: ApiInterfaceRx): (stashIds: (Uint8Array | string)[]) => Observable<DeriveStakingQuery[]> {
|
||||
return memo(instanceId, (stashIds: (Uint8Array | string)[]): Observable<DeriveStakingQuery[]> =>
|
||||
api.query.staking.nominators.multi<Option<Nominations>>(stashIds).pipe(
|
||||
switchMap((optNoms) =>
|
||||
api.derive.staking.queryMulti(
|
||||
optNoms.reduce((validatorIds: AccountId[], optNom): AccountId[] => {
|
||||
return optNom.unwrapOrDefault().targets.reduce((validatorIds: AccountId[], targetId): AccountId[] => {
|
||||
if (!validatorIds.find((validatorId) => validatorId.eq(targetId))) {
|
||||
validatorIds.push(targetId);
|
||||
}
|
||||
|
||||
return validatorIds;
|
||||
}, validatorIds);
|
||||
}, []),
|
||||
false
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -20,7 +20,7 @@ export function waitingInfo (instanceId: string, api: ApiInterfaceRx): () => Obs
|
||||
const elected = nextElected.map((a) => a.toString());
|
||||
const waiting = stashes.filter((v) => !elected.includes(v.toString()));
|
||||
|
||||
return api.derive.staking.queryMulti(waiting, false).pipe(
|
||||
return api.derive.staking.queryMulti(waiting, { withPrefs: true }).pipe(
|
||||
map((info): DeriveStakingWaiting => ({
|
||||
info,
|
||||
waiting
|
||||
|
||||
Reference in New Issue
Block a user