Skip to content

Commit 9254c57

Browse files
authored
Dedupe progress extraction (#4322)
* Dedupe progress extraction * Adjust
1 parent e13f3be commit 9254c57

File tree

6 files changed

+46
-83
lines changed

6 files changed

+46
-83
lines changed

packages/api-derive/src/elections/info.ts

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -54,41 +54,17 @@ function getConstants (api: ApiInterfaceRx, elections: string | null): Partial<D
5454
: {};
5555
}
5656

57-
function queryElections (api: ApiInterfaceRx): Observable<DeriveElectionsInfo> {
57+
function getModules (api: ApiInterfaceRx): [string, string | null] {
58+
const [council] = api.registry.getModuleInstances(api.runtimeVersion.specName.toString(), 'council') || ['council'];
5859
const elections = api.query.phragmenElection
5960
? 'phragmenElection'
6061
: api.query.electionsPhragmen
6162
? 'electionsPhragmen'
6263
: api.query.elections
6364
? 'elections'
6465
: null;
65-
const [council] = api.registry.getModuleInstances(api.runtimeVersion.specName.toString(), 'council') || ['council'];
6666

67-
return (
68-
elections
69-
? api.queryMulti<[Vec<AccountId>, Vec<Candidate>, Vec<Member>, Vec<Member>]>([
70-
api.query[council].members,
71-
api.query[elections].candidates,
72-
api.query[elections].members,
73-
api.query[elections].runnersUp
74-
])
75-
: combineLatest([
76-
api.query[council].members<Vec<AccountId>>(),
77-
of<Candidate[]>([]),
78-
of<Member[]>([]),
79-
of<Member[]>([])
80-
])
81-
).pipe(
82-
map(([councilMembers, candidates, members, runnersUp]): DeriveElectionsInfo => ({
83-
...getConstants(api, elections),
84-
candidateCount: api.registry.createType('u32', candidates.length),
85-
candidates: candidates.map(getCandidate),
86-
members: members.length
87-
? members.map(getAccountTuple).sort(sortAccounts)
88-
: councilMembers.map((accountId): [AccountId, Balance] => [accountId, api.registry.createType('Balance')]),
89-
runnersUp: runnersUp.map(getAccountTuple).sort(sortAccounts)
90-
}))
91-
);
67+
return [council, elections];
9268
}
9369

9470
/**
@@ -105,5 +81,33 @@ function queryElections (api: ApiInterfaceRx): Observable<DeriveElectionsInfo> {
10581
* ```
10682
*/
10783
export function info (instanceId: string, api: ApiInterfaceRx): () => Observable<DeriveElectionsInfo> {
108-
return memo(instanceId, (): Observable<DeriveElectionsInfo> => queryElections(api));
84+
return memo(instanceId, (): Observable<DeriveElectionsInfo> => {
85+
const [council, elections] = getModules(api);
86+
87+
return (
88+
elections
89+
? api.queryMulti<[Vec<AccountId>, Vec<Candidate>, Vec<Member>, Vec<Member>]>([
90+
api.query[council].members,
91+
api.query[elections].candidates,
92+
api.query[elections].members,
93+
api.query[elections].runnersUp
94+
])
95+
: combineLatest([
96+
api.query[council].members<Vec<AccountId>>(),
97+
of<Candidate[]>([]),
98+
of<Member[]>([]),
99+
of<Member[]>([])
100+
])
101+
).pipe(
102+
map(([councilMembers, candidates, members, runnersUp]): DeriveElectionsInfo => ({
103+
...getConstants(api, elections),
104+
candidateCount: api.registry.createType('u32', candidates.length),
105+
candidates: candidates.map(getCandidate),
106+
members: members.length
107+
? members.map(getAccountTuple).sort(sortAccounts)
108+
: councilMembers.map((a): [AccountId, Balance] => [a, api.registry.createType('Balance')]),
109+
runnersUp: runnersUp.map(getAccountTuple).sort(sortAccounts)
110+
}))
111+
);
112+
});
109113
}

packages/api-derive/src/session/eraLength.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

packages/api-derive/src/session/eraProgress.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// Copyright 2017-2021 @polkadot/api-derive authors & contributors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
export * from './eraLength';
5-
export * from './eraProgress';
64
export * from './indexes';
75
export * from './info';
86
export * from './progress';
9-
export * from './sessionProgress';

packages/api-derive/src/session/progress.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import type { Observable } from 'rxjs';
55
import type { ApiInterfaceRx } from '@polkadot/api/types';
66
import type { Option, u64 } from '@polkadot/types';
7-
import type { SessionIndex } from '@polkadot/types/interfaces';
7+
import type { BlockNumber, SessionIndex } from '@polkadot/types/interfaces';
88
import type { DeriveSessionInfo, DeriveSessionProgress } from '../types';
99

1010
import { combineLatest, map, of, switchMap } from 'rxjs';
@@ -15,6 +15,15 @@ type ResultSlotsNoSession = [u64, u64, u64];
1515
type ResultSlots = [u64, u64, u64, Option<SessionIndex>];
1616
type ResultSlotsFlat = [u64, u64, u64, SessionIndex];
1717

18+
function withProgressField (field: 'eraLength' | 'eraProgress' | 'sessionProgress'): (instanceId: string, api: ApiInterfaceRx) => () => Observable<BlockNumber> {
19+
return (instanceId: string, api: ApiInterfaceRx) =>
20+
memo(instanceId, (): Observable<BlockNumber> =>
21+
api.derive.session.progress().pipe(
22+
map((info) => info[field])
23+
)
24+
);
25+
}
26+
1827
function createDerive (api: ApiInterfaceRx, info: DeriveSessionInfo, [currentSlot, epochIndex, epochOrGenesisStartSlot, activeEraStartSessionIndex]: ResultSlotsFlat): DeriveSessionProgress {
1928
const epochStartSlot = epochIndex.mul(info.sessionLength).iadd(epochOrGenesisStartSlot);
2029
const sessionProgress = currentSlot.sub(epochStartSlot);
@@ -77,3 +86,7 @@ export function progress (instanceId: string, api: ApiInterfaceRx): () => Observ
7786
: queryAura(api)
7887
);
7988
}
89+
90+
export const eraLength = withProgressField('eraLength');
91+
export const eraProgress = withProgressField('eraProgress');
92+
export const sessionProgress = withProgressField('sessionProgress');

packages/api-derive/src/session/sessionProgress.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)