// Copyright 2017-2022 @polkadot/api-derive authors & contributors // SPDX-License-Identifier: Apache-2.0 import type { Observable } from 'rxjs'; import type { Option } from '@polkadot/types'; import type { AccountId, EraIndex } from '@polkadot/types/interfaces'; import type { PalletStakingExposure, PalletStakingNominations, PalletStakingRewardDestination, PalletStakingStakingLedger, PalletStakingValidatorPrefs } from '@polkadot/types/lookup'; import type { DeriveApi, DeriveStakingQuery, StakingQueryFlags } from '../types'; import { combineLatest, map, of, switchMap } from 'rxjs'; import { firstMemo, memo } from '../util'; function parseDetails (stashId: AccountId, controllerIdOpt: Option | null, nominatorsOpt: Option, rewardDestination: PalletStakingRewardDestination, validatorPrefs: PalletStakingValidatorPrefs, exposure: PalletStakingExposure, stakingLedgerOpt: Option): DeriveStakingQuery { return { accountId: stashId, controllerId: controllerIdOpt && controllerIdOpt.unwrapOr(null), exposure, nominators: nominatorsOpt.isSome ? nominatorsOpt.unwrap().targets : [], rewardDestination, stakingLedger: stakingLedgerOpt.unwrapOrDefault(), stashId, validatorPrefs }; } function getLedgers (api: DeriveApi, optIds: (Option | null)[], { withLedger = false }: StakingQueryFlags): Observable[]> { const ids = optIds .filter((o): o is Option => withLedger && !!o && o.isSome) .map((o) => o.unwrap()); const emptyLed = api.registry.createType>('Option'); return ( ids.length ? combineLatest(ids.map((s) => api.query.staking.ledger(s))) : of([]) ).pipe( map((optLedgers): Option[] => { let offset = -1; return optIds.map((o): Option => o && o.isSome ? optLedgers[++offset] || emptyLed : emptyLed ); }) ); } function getStashInfo (api: DeriveApi, stashIds: AccountId[], activeEra: EraIndex, { withController, withDestination, withExposure, withLedger, withNominations, withPrefs }: StakingQueryFlags): Observable<[(Option | null)[], Option[], PalletStakingRewardDestination[], PalletStakingValidatorPrefs[], PalletStakingExposure[]]> { const emptyNoms = api.registry.createType>('Option'); const emptyRewa = api.registry.createType('RewardDestination'); const emptyExpo = api.registry.createType('Exposure'); const emptyPrefs = api.registry.createType('ValidatorPrefs'); return combineLatest([ withController || withLedger ? combineLatest(stashIds.map((s) => api.query.staking.bonded(s))) : of(stashIds.map(() => null)), withNominations ? combineLatest(stashIds.map((s) => api.query.staking.nominators(s))) : of(stashIds.map(() => emptyNoms)), withDestination ? combineLatest(stashIds.map((s) => api.query.staking.payee(s))) : of(stashIds.map(() => emptyRewa)), withPrefs ? combineLatest(stashIds.map((s) => api.query.staking.validators(s))) : of(stashIds.map(() => emptyPrefs)), withExposure ? combineLatest(stashIds.map((s) => api.query.staking.erasStakers(activeEra, s))) : of(stashIds.map(() => emptyExpo)) ]); } 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 => 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]) ) ) ) ) ); } // /** * @description From a stash, retrieve the controllerId and all relevant details */ export const query = firstMemo( (api: DeriveApi, accountId: Uint8Array | string, flags: StakingQueryFlags) => api.derive.staking.queryMulti([accountId], flags) ); export function queryMulti (instanceId: string, api: DeriveApi): (accountIds: (Uint8Array | string)[], flags: StakingQueryFlags) => Observable { return memo(instanceId, (accountIds: (Uint8Array | string)[], flags: StakingQueryFlags): Observable => api.derive.session.indexes().pipe( switchMap(({ activeEra }): Observable => { const stashIds = accountIds.map((a) => api.registry.createType('AccountId', a)); return stashIds.length ? getBatch(api, activeEra, stashIds, flags) : of([]); }) ) ); }