diff --git a/packages/api-derive/src/staking/electedInfo.ts b/packages/api-derive/src/staking/electedInfo.ts new file mode 100644 index 000000000000..29b002a59a42 --- /dev/null +++ b/packages/api-derive/src/staking/electedInfo.ts @@ -0,0 +1,30 @@ +// Copyright 2017-2019 @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 } from '@polkadot/types/interfaces'; +import { DerivedStaking, DerivedStakingElected } from '../types'; + +import { Observable, combineLatest, of } from 'rxjs'; +import { map, switchMap } from 'rxjs/operators'; + +import { memo } from '../util'; + +export function electedInfo (api: ApiInterfaceRx): () => Observable { + return memo((): Observable => + api.derive.staking.validators().pipe( + switchMap(({ currentElected }): Observable<[AccountId[], DerivedStaking[]]> => + combineLatest([ + of(currentElected), + combineLatest(currentElected.map((accountId): Observable => + api.derive.staking.info(accountId) + )) + ]) + ), + map(([currentElected, info]): DerivedStakingElected => ({ + currentElected, info + })) + ) + ); +} diff --git a/packages/api-derive/src/staking/index.ts b/packages/api-derive/src/staking/index.ts index ea8c019bb1fa..168a8c482c27 100644 --- a/packages/api-derive/src/staking/index.ts +++ b/packages/api-derive/src/staking/index.ts @@ -3,6 +3,7 @@ // of the Apache-2.0 license. See the LICENSE file for details. export * from './controllers'; +export * from './electedInfo'; export * from './info'; export * from './overview'; export * from './recentlyOffline'; diff --git a/packages/api-derive/src/types.ts b/packages/api-derive/src/types.ts index 47f8e2bbe9bd..9c14abcdf3e4 100644 --- a/packages/api-derive/src/types.ts +++ b/packages/api-derive/src/types.ts @@ -106,6 +106,11 @@ export type DerivedStakingAccount = [AccountId, DerivedStakingOnlineStatus]; export type DerivedStakingAccounts = DerivedStakingAccount[]; +export interface DerivedStakingElected { + currentElected: AccountId[]; + info: DerivedStaking[]; +} + export interface DerivedStakingOnlineStatus { online?: { isOnline: boolean;