// Copyright 2017-2021 @polkadot/api-derive authors & contributors // SPDX-License-Identifier: Apache-2.0 import type { ApiInterfaceRx } from '@polkadot/api/types'; import type { AccountId } from '@polkadot/types/interfaces'; import type { Observable } from '@polkadot/x-rxjs'; import type { DeriveStakingElected, StakingQueryFlags } from '../types'; import { arrayFlatten } from '@polkadot/util'; import { map, switchMap } from '@polkadot/x-rxjs/operators'; import { memo } from '../util'; const DEFAULT_FLAGS = { withController: true, withExposure: true, withPrefs: true }; function combineAccounts (nextElected: AccountId[], validators: AccountId[]): AccountId[] { return arrayFlatten([nextElected, validators.filter((v) => !nextElected.find((n) => n.eq(v)))]); } export function electedInfo (instanceId: string, api: ApiInterfaceRx): (flags?: StakingQueryFlags) => Observable { return memo(instanceId, (flags: StakingQueryFlags = DEFAULT_FLAGS): Observable => api.derive.staking.validators().pipe( switchMap(({ nextElected, validators }): Observable => api.derive.staking.queryMulti(combineAccounts(nextElected, validators), flags).pipe( map((info): DeriveStakingElected => ({ info, nextElected, validators })) ) ) ) ); }