// Copyright 2017-2020 @polkadot/api-derive authors & contributors // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. import { ApiInterfaceRx } from '@polkadot/api/types'; import { AccountId, EraIndex, Exposure, Keys, Nominations, RewardDestination, StakingLedger, ValidatorPrefs } from '@polkadot/types/interfaces'; import { ITuple } from '@polkadot/types/types'; import { DeriveStakingQuery } from '../types'; import { Observable, combineLatest, of } from 'rxjs'; import { map, switchMap } from 'rxjs/operators'; import { Option, Vec } from '@polkadot/types'; import { isFunction } from '@polkadot/util'; import { memo } from '../util'; type MultiResult = [Option, Option | Nominations>, RewardDestination, ITuple<[ValidatorPrefs]> | ValidatorPrefs, Option, Exposure]; function parseSessionIds (stashId: AccountId, queuedKeys: [AccountId, Keys][], nextKeys: Option): { nextSessionIds: AccountId[]; sessionIds: AccountId[] } { const sessionIds = (queuedKeys.find(([currentId]): boolean => currentId.eq(stashId) ) || [undefined, [] as AccountId[]])[1]; const nextSessionIds = nextKeys.unwrapOr([] as AccountId[]); return { nextSessionIds, sessionIds }; } function parseController (stashId: AccountId, queuedKeys: Vec>, [controllerIdOpt, nominatorsOpt, rewardDestination, validatorPrefs, nextKeys, exposure]: MultiResult, stakingLedgerOpt: Option): DeriveStakingQuery { const controllerId = controllerIdOpt.unwrapOr(null); const nominators = nominatorsOpt.unwrapOr(null); return controllerId && stakingLedgerOpt.isSome ? { accountId: stashId, controllerId, exposure, nominators: nominators ? Array.isArray(nominators) ? nominators[0].targets : nominators.targets : [], rewardDestination, stakingLedger: stakingLedgerOpt.unwrapOr(undefined), stashId, validatorPrefs: Array.isArray(validatorPrefs) ? validatorPrefs[0] : validatorPrefs, ...parseSessionIds(stashId, queuedKeys, nextKeys) } : { accountId: stashId, nextSessionIds: [], sessionIds: [] }; } function retrievePrev (api: ApiInterfaceRx, stashId: AccountId): Observable { return api.queryMulti([ [api.query.staking.bonded, stashId], [api.query.staking.nominators, stashId], [api.query.staking.payee, stashId], [api.query.staking.validators, stashId], [api.query.session.nextKeys, [api.consts.session.dedupKeyPrefix, stashId]], [api.query.staking.stakers, stashId] ]); } function retrieveCurr (api: ApiInterfaceRx, stashIds: AccountId[], activeEra: EraIndex): Observable { return combineLatest([ api.query.staking.bonded.multi>(stashIds), api.query.staking.nominators.multi>(stashIds), api.query.staking.payee.multi(stashIds), api.query.staking.validators.multi(stashIds), api.consts.session?.dedupKeyPrefix ? api.query.session.nextKeys.multi>(stashIds.map((stashId) => [api.consts.session.dedupKeyPrefix, stashId])) : api.query.session.nextKeys.multi>(stashIds), api.query.staking.erasStakers.multi(stashIds.map((stashId) => [activeEra, stashId])) ]).pipe( map(([controllerIdOpt, nominatorsOpt, rewardDestination, validatorPrefs, nextKeys, exposure]): MultiResult[] => controllerIdOpt.map((controllerIdOpt, index): MultiResult => [controllerIdOpt, nominatorsOpt[index], rewardDestination[index], validatorPrefs[index], nextKeys[index], exposure[index]] ) ) ); } function retrieveControllers (api: ApiInterfaceRx, optControllerIds: Option[]): Observable[]> { const ids = optControllerIds.filter((opt) => opt.isSome).map((opt) => opt.unwrap()); if (!ids.length) { return of(optControllerIds.map(() => api.registry.createType('Option'))); } return api.query.staking.ledger.multi>(ids).pipe( map((optLedgers): Option[] => { let offset = -1; return optControllerIds.map((opt): Option => opt.isSome ? optLedgers[++offset] : api.registry.createType('Option') ); }) ); } /** * @description From a stash, retrieve the controllerId and all relevant details */ export function query (api: ApiInterfaceRx): (accountId: Uint8Array | string) => Observable { return memo((accountId: Uint8Array | string): Observable => api.derive.staking.queryMulti([accountId]).pipe( map(([first]) => first) ) ); } export function queryMulti (api: ApiInterfaceRx): (accountIds: (Uint8Array | string)[]) => Observable { return memo((accountIds: (Uint8Array | string)[]): Observable => accountIds.length ? combineLatest([ api.query.session.queuedKeys>>(), api.derive.session.indexes() ]).pipe( switchMap(([queuedKeys, { activeEra }]): Observable => { const stashIds = accountIds.map((accountId) => api.registry.createType('AccountId', accountId)); return ( isFunction(api.query.staking.erasStakers) ? retrieveCurr(api, stashIds, activeEra) : combineLatest(stashIds.map((stashId) => retrievePrev(api, stashId))) ).pipe( switchMap((results): Observable => retrieveControllers(api, results.map(([optController]) => optController)).pipe( map((stakingLedgerOpts) => stashIds.map((stashId, index) => parseController(stashId, queuedKeys, results[index], stakingLedgerOpts[index])) ) ) ) ); }) ) : of([]) ); }