Expose derive staking.nextElected (#2042)

This commit is contained in:
Jaco Greeff
2020-03-13 08:45:28 +01:00
committed by GitHub
parent 1659e672ae
commit affa5d390c
2 changed files with 16 additions and 13 deletions
+15 -13
View File
@@ -12,16 +12,20 @@ import { StorageKey } from '@polkadot/types';
import { memo } from '../util';
function queryNextStakers (api: ApiInterfaceRx): Observable<AccountId[]> {
// only populate for next era in the last session, so track both here - entries are not
// subscriptions, so we need a trigger - currentIndex acts as that trigger to refresh
return api.derive.session.indexes().pipe(
switchMap(({ activeEra }): Observable<StorageKey[]> =>
api.query.staking.erasStakers.keys(activeEra.addn(1)).pipe(take(1))
),
map((keys): AccountId[] =>
keys.map((key): AccountId => key.args[1] as AccountId)
)
export function nextElected (api: ApiInterfaceRx): () => Observable<AccountId[]> {
return memo((): Observable<AccountId[]> =>
api.query.staking.erasStakers
? api.derive.session.indexes().pipe(
// only populate for next era in the last session, so track both here - entries are not
// subscriptions, so we need a trigger - currentIndex acts as that trigger to refresh
switchMap(({ currentEra }): Observable<StorageKey[]> =>
api.query.staking.erasStakers.keys(currentEra).pipe(take(1))
),
map((keys): AccountId[] =>
keys.map((key): AccountId => key.args[1] as AccountId)
)
)
: api.query.staking.currentElected<AccountId[]>()
);
}
@@ -38,9 +42,7 @@ export function validators (api: ApiInterfaceRx): () => Observable<DeriveStaking
? api.query.session.validators()
: of([]),
api.query.staking
? api.query.staking.erasStakers
? queryNextStakers(api)
: api.query.staking.currentElected<AccountId[]>()
? api.derive.staking.nextElected()
: of([])
]).pipe(
map(([validators, nextElected]): DeriveStakingValidators => ({