From 646e64c286a6e13fc04061db5c4d911177985f26 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Sat, 20 Jun 2020 15:30:48 +0200 Subject: [PATCH] Add derive.staking.waitingInfo (#2367) * Add derive.staking.waitingInfo * Small optimizations * Updated --- .../api-derive/src/staking/electedInfo.ts | 27 +++++++-------- packages/api-derive/src/staking/index.ts | 1 + packages/api-derive/src/staking/types.ts | 5 +++ .../api-derive/src/staking/waitingInfo.ts | 34 +++++++++++++++++++ 4 files changed, 52 insertions(+), 15 deletions(-) create mode 100644 packages/api-derive/src/staking/waitingInfo.ts diff --git a/packages/api-derive/src/staking/electedInfo.ts b/packages/api-derive/src/staking/electedInfo.ts index ee9e2cd310..22d4676667 100644 --- a/packages/api-derive/src/staking/electedInfo.ts +++ b/packages/api-derive/src/staking/electedInfo.ts @@ -3,10 +3,9 @@ // 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 { DeriveStakingQuery, DeriveStakingElected } from '../types'; +import { DeriveStakingElected } from '../types'; -import { Observable, combineLatest, of } from 'rxjs'; +import { Observable, combineLatest } from 'rxjs'; import { map, switchMap } from 'rxjs/operators'; import { memo } from '../util'; @@ -14,18 +13,16 @@ import { memo } from '../util'; export function electedInfo (api: ApiInterfaceRx): () => Observable { return memo((): Observable => api.derive.staking.validators().pipe( - switchMap(({ nextElected }): Observable<[AccountId[], DeriveStakingQuery[]]> => - combineLatest([ - of(nextElected), - combineLatest( - nextElected.map((accountId) => api.derive.staking.query(accountId)) - ) - ]) - ), - map(([nextElected, info]): DeriveStakingElected => ({ - info, - nextElected - })) + switchMap(({ nextElected }): Observable => + combineLatest( + nextElected.map((accountId) => api.derive.staking.query(accountId)) + ).pipe( + map((info): DeriveStakingElected => ({ + info, + nextElected + })) + ) + ) ) ); } diff --git a/packages/api-derive/src/staking/index.ts b/packages/api-derive/src/staking/index.ts index 0b6eff83d3..a828db7657 100644 --- a/packages/api-derive/src/staking/index.ts +++ b/packages/api-derive/src/staking/index.ts @@ -21,3 +21,4 @@ export * from './stakerRewards'; export * from './stakerSlashes'; export * from './stashes'; export * from './validators'; +export * from './waitingInfo'; diff --git a/packages/api-derive/src/staking/types.ts b/packages/api-derive/src/staking/types.ts index 689d59cb62..f775542a9d 100644 --- a/packages/api-derive/src/staking/types.ts +++ b/packages/api-derive/src/staking/types.ts @@ -96,6 +96,11 @@ export interface DeriveStakingElected { info: DeriveStakingQuery[]; } +export interface DeriveStakingWaiting { + info: DeriveStakingQuery[]; + waiting: AccountId[]; +} + export interface DeriveStakingValidators { nextElected: AccountId[]; validators: AccountId[]; diff --git a/packages/api-derive/src/staking/waitingInfo.ts b/packages/api-derive/src/staking/waitingInfo.ts new file mode 100644 index 0000000000..09759c1c5a --- /dev/null +++ b/packages/api-derive/src/staking/waitingInfo.ts @@ -0,0 +1,34 @@ +// 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 { DeriveStakingWaiting } from '../types'; + +import { Observable, combineLatest } from 'rxjs'; +import { map, switchMap } from 'rxjs/operators'; + +import { memo } from '../util'; + +export function waitingInfo (api: ApiInterfaceRx): () => Observable { + return memo((): 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 combineLatest( + waiting.map((accountId) => api.derive.staking.query(accountId)) + ).pipe( + map((info): DeriveStakingWaiting => ({ + info, + waiting + })) + ); + }) + ) + ); +}