Dedupe progress extraction (#4322)
* Dedupe progress extraction * Adjust
This commit is contained in:
@@ -54,7 +54,8 @@ function getConstants (api: ApiInterfaceRx, elections: string | null): Partial<D
|
||||
: {};
|
||||
}
|
||||
|
||||
function queryElections (api: ApiInterfaceRx): Observable<DeriveElectionsInfo> {
|
||||
function getModules (api: ApiInterfaceRx): [string, string | null] {
|
||||
const [council] = api.registry.getModuleInstances(api.runtimeVersion.specName.toString(), 'council') || ['council'];
|
||||
const elections = api.query.phragmenElection
|
||||
? 'phragmenElection'
|
||||
: api.query.electionsPhragmen
|
||||
@@ -62,33 +63,8 @@ function queryElections (api: ApiInterfaceRx): Observable<DeriveElectionsInfo> {
|
||||
: api.query.elections
|
||||
? 'elections'
|
||||
: null;
|
||||
const [council] = api.registry.getModuleInstances(api.runtimeVersion.specName.toString(), 'council') || ['council'];
|
||||
|
||||
return (
|
||||
elections
|
||||
? api.queryMulti<[Vec<AccountId>, Vec<Candidate>, Vec<Member>, Vec<Member>]>([
|
||||
api.query[council].members,
|
||||
api.query[elections].candidates,
|
||||
api.query[elections].members,
|
||||
api.query[elections].runnersUp
|
||||
])
|
||||
: combineLatest([
|
||||
api.query[council].members<Vec<AccountId>>(),
|
||||
of<Candidate[]>([]),
|
||||
of<Member[]>([]),
|
||||
of<Member[]>([])
|
||||
])
|
||||
).pipe(
|
||||
map(([councilMembers, candidates, members, runnersUp]): DeriveElectionsInfo => ({
|
||||
...getConstants(api, elections),
|
||||
candidateCount: api.registry.createType('u32', candidates.length),
|
||||
candidates: candidates.map(getCandidate),
|
||||
members: members.length
|
||||
? members.map(getAccountTuple).sort(sortAccounts)
|
||||
: councilMembers.map((accountId): [AccountId, Balance] => [accountId, api.registry.createType('Balance')]),
|
||||
runnersUp: runnersUp.map(getAccountTuple).sort(sortAccounts)
|
||||
}))
|
||||
);
|
||||
return [council, elections];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -105,5 +81,33 @@ function queryElections (api: ApiInterfaceRx): Observable<DeriveElectionsInfo> {
|
||||
* ```
|
||||
*/
|
||||
export function info (instanceId: string, api: ApiInterfaceRx): () => Observable<DeriveElectionsInfo> {
|
||||
return memo(instanceId, (): Observable<DeriveElectionsInfo> => queryElections(api));
|
||||
return memo(instanceId, (): Observable<DeriveElectionsInfo> => {
|
||||
const [council, elections] = getModules(api);
|
||||
|
||||
return (
|
||||
elections
|
||||
? api.queryMulti<[Vec<AccountId>, Vec<Candidate>, Vec<Member>, Vec<Member>]>([
|
||||
api.query[council].members,
|
||||
api.query[elections].candidates,
|
||||
api.query[elections].members,
|
||||
api.query[elections].runnersUp
|
||||
])
|
||||
: combineLatest([
|
||||
api.query[council].members<Vec<AccountId>>(),
|
||||
of<Candidate[]>([]),
|
||||
of<Member[]>([]),
|
||||
of<Member[]>([])
|
||||
])
|
||||
).pipe(
|
||||
map(([councilMembers, candidates, members, runnersUp]): DeriveElectionsInfo => ({
|
||||
...getConstants(api, elections),
|
||||
candidateCount: api.registry.createType('u32', candidates.length),
|
||||
candidates: candidates.map(getCandidate),
|
||||
members: members.length
|
||||
? members.map(getAccountTuple).sort(sortAccounts)
|
||||
: councilMembers.map((a): [AccountId, Balance] => [a, api.registry.createType('Balance')]),
|
||||
runnersUp: runnersUp.map(getAccountTuple).sort(sortAccounts)
|
||||
}))
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
// Copyright 2017-2021 @polkadot/api-derive authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Observable } from 'rxjs';
|
||||
import type { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
import type { BlockNumber } from '@polkadot/types/interfaces';
|
||||
|
||||
import { map } from 'rxjs';
|
||||
|
||||
import { memo } from '../util';
|
||||
|
||||
export function eraLength (instanceId: string, api: ApiInterfaceRx): () => Observable<BlockNumber> {
|
||||
return memo(instanceId, (): Observable<BlockNumber> =>
|
||||
api.derive.session.info().pipe(
|
||||
map((info) => info.eraLength)
|
||||
));
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
// Copyright 2017-2021 @polkadot/api-derive authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Observable } from 'rxjs';
|
||||
import type { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
import type { BlockNumber } from '@polkadot/types/interfaces';
|
||||
|
||||
import { map } from 'rxjs';
|
||||
|
||||
import { memo } from '../util';
|
||||
|
||||
export function eraProgress (instanceId: string, api: ApiInterfaceRx): () => Observable<BlockNumber> {
|
||||
return memo(instanceId, (): Observable<BlockNumber> =>
|
||||
api.derive.session.progress().pipe(
|
||||
map((info) => info.eraProgress)
|
||||
));
|
||||
}
|
||||
@@ -1,9 +1,6 @@
|
||||
// Copyright 2017-2021 @polkadot/api-derive authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export * from './eraLength';
|
||||
export * from './eraProgress';
|
||||
export * from './indexes';
|
||||
export * from './info';
|
||||
export * from './progress';
|
||||
export * from './sessionProgress';
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
import type { Observable } from 'rxjs';
|
||||
import type { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
import type { Option, u64 } from '@polkadot/types';
|
||||
import type { SessionIndex } from '@polkadot/types/interfaces';
|
||||
import type { BlockNumber, SessionIndex } from '@polkadot/types/interfaces';
|
||||
import type { DeriveSessionInfo, DeriveSessionProgress } from '../types';
|
||||
|
||||
import { combineLatest, map, of, switchMap } from 'rxjs';
|
||||
@@ -15,6 +15,15 @@ type ResultSlotsNoSession = [u64, u64, u64];
|
||||
type ResultSlots = [u64, u64, u64, Option<SessionIndex>];
|
||||
type ResultSlotsFlat = [u64, u64, u64, SessionIndex];
|
||||
|
||||
function withProgressField (field: 'eraLength' | 'eraProgress' | 'sessionProgress'): (instanceId: string, api: ApiInterfaceRx) => () => Observable<BlockNumber> {
|
||||
return (instanceId: string, api: ApiInterfaceRx) =>
|
||||
memo(instanceId, (): Observable<BlockNumber> =>
|
||||
api.derive.session.progress().pipe(
|
||||
map((info) => info[field])
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
function createDerive (api: ApiInterfaceRx, info: DeriveSessionInfo, [currentSlot, epochIndex, epochOrGenesisStartSlot, activeEraStartSessionIndex]: ResultSlotsFlat): DeriveSessionProgress {
|
||||
const epochStartSlot = epochIndex.mul(info.sessionLength).iadd(epochOrGenesisStartSlot);
|
||||
const sessionProgress = currentSlot.sub(epochStartSlot);
|
||||
@@ -77,3 +86,7 @@ export function progress (instanceId: string, api: ApiInterfaceRx): () => Observ
|
||||
: queryAura(api)
|
||||
);
|
||||
}
|
||||
|
||||
export const eraLength = withProgressField('eraLength');
|
||||
export const eraProgress = withProgressField('eraProgress');
|
||||
export const sessionProgress = withProgressField('sessionProgress');
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
// Copyright 2017-2021 @polkadot/api-derive authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Observable } from 'rxjs';
|
||||
import type { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
import type { BlockNumber } from '@polkadot/types/interfaces';
|
||||
|
||||
import { map } from 'rxjs';
|
||||
|
||||
import { memo } from '../util';
|
||||
|
||||
export function sessionProgress (instanceId: string, api: ApiInterfaceRx): () => Observable<BlockNumber> {
|
||||
return memo(instanceId, (): Observable<BlockNumber> =>
|
||||
api.derive.session.progress().pipe(
|
||||
map((info) => info.sessionProgress)
|
||||
));
|
||||
}
|
||||
Reference in New Issue
Block a user