Adjust with queryMulti as applicable (#2370)

This commit is contained in:
Jaco Greeff
2020-06-21 09:56:02 +02:00
committed by GitHub
parent 4e02b982d3
commit 34101ec12b
3 changed files with 22 additions and 31 deletions
+15 -12
View File
@@ -7,7 +7,7 @@ import { Balance, StakingLedger, UnlockChunk } from '@polkadot/types/interfaces'
import { DeriveSessionInfo, DeriveStakingAccount, DeriveStakingQuery, DeriveUnlocking } from '../types';
import BN from 'bn.js';
import { Observable, combineLatest } from 'rxjs';
import { Observable } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { isUndefined } from '@polkadot/util';
@@ -70,21 +70,19 @@ function parseResult (api: ApiInterfaceRx, sessionInfo: DeriveSessionInfo, query
};
}
export function _account (api: ApiInterfaceRx): (sessionInfo: DeriveSessionInfo, accountId: Uint8Array | string) => Observable<DeriveStakingAccount> {
return memo((sessionInfo: DeriveSessionInfo, accountId: Uint8Array | string): Observable<DeriveStakingAccount> =>
api.derive.staking.query(accountId).pipe(
map((query) => parseResult(api, sessionInfo, query))
));
}
/**
* @description From a stash, retrieve the controllerId and fill in all the relevant staking details
*/
export function account (api: ApiInterfaceRx): (accountId: Uint8Array | string) => Observable<DeriveStakingAccount> {
return memo((accountId: Uint8Array | string): Observable<DeriveStakingAccount> =>
api.derive.session.info().pipe(
switchMap((sessionInfo) => api.derive.staking._account(sessionInfo, accountId))
));
switchMap((sessionInfo) =>
api.derive.staking.query(accountId).pipe(
map((query) => parseResult(api, sessionInfo, query))
)
)
)
);
}
/**
@@ -94,7 +92,12 @@ export function accounts (api: ApiInterfaceRx): (accountIds: (Uint8Array | strin
return memo((accountIds: (Uint8Array | string)[]): Observable<DeriveStakingAccount[]> =>
api.derive.session.info().pipe(
switchMap((sessionInfo) =>
combineLatest(accountIds.map((accountId) => api.derive.staking._account(sessionInfo, accountId)))
api.derive.staking.queryMulti(accountIds).pipe(
map((queries) =>
queries.map((query) => parseResult(api, sessionInfo, query))
)
)
)
));
)
);
}
+5 -11
View File
@@ -5,7 +5,7 @@
import { ApiInterfaceRx } from '@polkadot/api/types';
import { DeriveStakingElected } from '../types';
import { Observable, combineLatest, of } from 'rxjs';
import { Observable } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { memo } from '../util';
@@ -13,20 +13,14 @@ import { memo } from '../util';
export function electedInfo (api: ApiInterfaceRx): () => Observable<DeriveStakingElected> {
return memo((): Observable<DeriveStakingElected> =>
api.derive.staking.validators().pipe(
switchMap(({ nextElected }): Observable<DeriveStakingElected> => {
if (!nextElected.length) {
return of({ info: [], nextElected: [] });
}
return combineLatest(
nextElected.map((accountId) => api.derive.staking.query(accountId))
).pipe(
switchMap(({ nextElected }): Observable<DeriveStakingElected> =>
api.derive.staking.queryMulti(nextElected).pipe(
map((info): DeriveStakingElected => ({
info,
nextElected
}))
);
})
)
)
)
);
}
@@ -5,7 +5,7 @@
import { ApiInterfaceRx } from '@polkadot/api/types';
import { DeriveStakingWaiting } from '../types';
import { Observable, combineLatest, of } from 'rxjs';
import { Observable, combineLatest } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { memo } from '../util';
@@ -20,13 +20,7 @@ export function waitingInfo (api: ApiInterfaceRx): () => Observable<DeriveStakin
const elected = nextElected.map((a) => a.toString());
const waiting = stashes.filter((v) => !elected.includes(v.toString()));
if (!waiting.length) {
return of({ info: [], waiting: [] });
}
return combineLatest(
waiting.map((accountId) => api.derive.staking.query(accountId))
).pipe(
return api.derive.staking.queryMulti(waiting).pipe(
map((info): DeriveStakingWaiting => ({
info,
waiting