derive.staking.electedInfo also contains current (#2874)

This commit is contained in:
Jaco Greeff
2020-11-25 13:08:17 +01:00
committed by GitHub
parent 790e0ebdcf
commit f6af257051
3 changed files with 11 additions and 5 deletions
@@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
import { ApiInterfaceRx } from '@polkadot/api/types';
import { AccountId } from '@polkadot/types/interfaces';
import { DeriveStakingElected } from '../types';
import { Observable } from 'rxjs';
@@ -9,14 +10,19 @@ import { map, switchMap } from 'rxjs/operators';
import { memo } from '../util';
function combineAccounts (nextElected: AccountId[], validators: AccountId[]): AccountId[] {
return [...nextElected].concat(...validators.filter((v) => !nextElected.find((n) => n.eq(v))));
}
export function electedInfo (instanceId: string, api: ApiInterfaceRx): () => Observable<DeriveStakingElected> {
return memo(instanceId, (): Observable<DeriveStakingElected> =>
api.derive.staking.validators().pipe(
switchMap(({ nextElected }): Observable<DeriveStakingElected> =>
api.derive.staking.queryMulti(nextElected).pipe(
switchMap(({ nextElected, validators }): Observable<DeriveStakingElected> =>
api.derive.staking.queryMulti(combineAccounts(nextElected, validators)).pipe(
map((info): DeriveStakingElected => ({
info,
nextElected
nextElected,
validators
}))
)
)
+2 -1
View File
@@ -120,8 +120,9 @@ export interface DeriveStakingQuery extends DeriveStakingStash {
}
export interface DeriveStakingElected {
nextElected: AccountId[];
info: DeriveStakingQuery[];
nextElected: AccountId[];
validators: AccountId[];
}
export interface DeriveStakingWaiting {
@@ -30,7 +30,6 @@ export function validators (instanceId: string, api: ApiInterfaceRx): () => Obse
return memo(instanceId, (): Observable<DeriveStakingValidators> =>
// Sadly the node-template is (for some obscure reason) not comprehensive, so while the derive works
// in all actual real-world deployed chains, it does create some confusion for limited template chains
// NOTE: Not doing multi queries here, since we have validators as a single in the derived newHead
combineLatest([
api.query.session
? api.query.session.validators()