// Copyright 2017-2021 @polkadot/api-derive authors & contributors // SPDX-License-Identifier: Apache-2.0 import type { ApiInterfaceRx } from '@polkadot/api/types'; import type { Observable } from '@polkadot/x-rxjs'; import type { DeriveStakingWaiting, StakingQueryFlags } from '../types'; import { combineLatest } from '@polkadot/x-rxjs'; import { map, switchMap } from '@polkadot/x-rxjs/operators'; import { memo } from '../util'; const DEFAULT_FLAGS = { withController: true, withPrefs: true }; export function waitingInfo (instanceId: string, api: ApiInterfaceRx): (flags?: StakingQueryFlags) => Observable { return memo(instanceId, (flags: StakingQueryFlags = DEFAULT_FLAGS): Observable => combineLatest([ api.derive.staking.validators(), api.derive.staking.stashes() ]).pipe( switchMap(([{ nextElected }, stashes]): Observable => { const elected = nextElected.map((a) => a.toString()); const waiting = stashes.filter((v) => !elected.includes(v.toString())); return api.derive.staking.queryMulti(waiting, flags).pipe( map((info): DeriveStakingWaiting => ({ info, waiting })) ); }) ) ); }