Skip to content

Commit 523f162

Browse files
authored
Collective aliases (don't apply to derive naming) (#4374)
* Collective aliases (don't apply to derive naming) * Fix build (aka new form apply to prime)
1 parent a2c027b commit 523f162

File tree

4 files changed

+33
-31
lines changed

4 files changed

+33
-31
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
Changes:
66

7+
- Fix alias module mapping in collective derives
78
- Update `dispathchQueue` derive to align with latest Substrate
89
- Update to latest Substrate metadata
910

packages/api-derive/src/collective/helpers.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,26 @@ import { isFunction } from '@polkadot/util';
1111

1212
import { memo } from '../util';
1313

14-
export function getInstance (api: DeriveApi, section: string): string {
14+
export function getInstance (api: DeriveApi, section: string): DeriveApi['query']['council'] {
1515
const instances = api.registry.getModuleInstances(api.runtimeVersion.specName.toString(), section);
16-
17-
return instances && instances.length
16+
const name = instances && instances.length
1817
? instances[0]
1918
: section;
19+
20+
return api.query[name as 'council'];
2021
}
2122

22-
export function withSection <T, F extends (...args: any[]) => Observable<T>> (_section: Collective, fn: (section: string, api: DeriveApi, instanceId: string) => F): (instanceId: string, api: DeriveApi) => F {
23+
export function withSection <T, F extends (...args: any[]) => Observable<T>> (section: Collective, fn: (query: DeriveApi['query']['council'], api: DeriveApi, instanceId: string) => F): (instanceId: string, api: DeriveApi) => F {
2324
return (instanceId: string, api: DeriveApi) =>
24-
memo(instanceId, fn(getInstance(api, _section), api, instanceId)) as unknown as F;
25+
memo(instanceId, fn(getInstance(api, section), api, instanceId)) as unknown as F;
2526
}
2627

27-
export function callMethod <T> (method: 'members' | 'proposals' | 'proposalCount', empty: T): (_section: Collective) => (instanceId: string, api: DeriveApi) => () => Observable<T> {
28-
return (_section: Collective) =>
29-
withSection(_section, (section, api) =>
28+
export function callMethod <T> (method: 'members' | 'proposals' | 'proposalCount', empty: T): (section: Collective) => (instanceId: string, api: DeriveApi) => () => Observable<T> {
29+
return (section: Collective) =>
30+
withSection(section, (query) =>
3031
(): Observable<T> =>
31-
isFunction(api.query[section]?.[method])
32-
? api.query[section as 'council'][method]() as unknown as Observable<T>
32+
isFunction(query?.[method])
33+
? query[method]() as unknown as Observable<T>
3334
: of(empty)
3435
);
3536
}

packages/api-derive/src/collective/prime.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ import { withSection } from './helpers';
1515
// We are re-exporting these from here to ensure that *.d.ts generation is correct
1616
export type { AccountId } from '@polkadot/types/interfaces';
1717

18-
export function prime (_section: Collective): (instanceId: string, api: DeriveApi) => () => Observable<AccountId | null> {
19-
return withSection(_section, (section, api) =>
18+
export function prime (section: Collective): (instanceId: string, api: DeriveApi) => () => Observable<AccountId | null> {
19+
return withSection(section, (query) =>
2020
(): Observable<AccountId | null> =>
21-
isFunction(api.query[section as 'council']?.prime)
22-
? api.query[section as 'council'].prime().pipe(
23-
map((optPrime): AccountId | null =>
24-
optPrime.unwrapOr(null)
21+
isFunction(query?.prime)
22+
? query.prime().pipe(
23+
map((o): AccountId | null =>
24+
o.unwrapOr(null)
2525
)
2626
)
2727
: of(null)

packages/api-derive/src/collective/proposals.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,47 +34,47 @@ function parse (api: DeriveApi, [hashes, proposals, votes]: Result): DeriveColle
3434
.filter((proposal): proposal is DeriveCollectiveProposal => !!proposal);
3535
}
3636

37-
function _proposalsFrom (section: string, api: DeriveApi, hashes: (Hash | Uint8Array | string)[]): Observable<DeriveCollectiveProposal[]> {
38-
return (isFunction(api.query[section]?.proposals) && hashes.length
37+
function _proposalsFrom (api: DeriveApi, query: DeriveApi['query']['council'], hashes: (Hash | Uint8Array | string)[]): Observable<DeriveCollectiveProposal[]> {
38+
return (isFunction(query?.proposals) && hashes.length
3939
? combineLatest([
4040
of(hashes),
4141
// this should simply be api.query[section].proposalOf.multi<Option<Proposal>>(hashes),
4242
// however we have had cases on Edgeware where the indices have moved around after an
4343
// upgrade, which results in invalid on-chain data
4444
combineLatest(hashes.map((h) =>
45-
api.query[section].proposalOf<Option<Proposal>>(h).pipe(
45+
query.proposalOf<Option<Proposal>>(h).pipe(
4646
catchError(() => of(null))
4747
)
4848
)),
49-
api.query[section].voting.multi<Option<Votes>>(hashes)
49+
query.voting.multi<Option<Votes>>(hashes)
5050
])
5151
: of<Result>([[], [], []])
5252
).pipe(
5353
map((r) => parse(api, r))
5454
);
5555
}
5656

57-
export function hasProposals (_section: Collective): (instanceId: string, api: DeriveApi) => () => Observable<boolean> {
58-
return withSection(_section, (section, api) =>
57+
export function hasProposals (section: Collective): (instanceId: string, api: DeriveApi) => () => Observable<boolean> {
58+
return withSection(section, (query) =>
5959
(): Observable<boolean> =>
60-
of(isFunction(api.query[section]?.proposals))
60+
of(isFunction(query?.proposals))
6161
);
6262
}
6363

64-
export function proposals (_section: Collective): (instanceId: string, api: DeriveApi) => () => Observable<DeriveCollectiveProposal[]> {
65-
return withSection(_section, (section, api) =>
64+
export function proposals (section: Collective): (instanceId: string, api: DeriveApi) => () => Observable<DeriveCollectiveProposal[]> {
65+
return withSection(section, (query, api) =>
6666
(): Observable<DeriveCollectiveProposal[]> =>
67-
api.derive[section as 'council'].proposalHashes().pipe(
68-
switchMap((all) => _proposalsFrom(section, api, all))
67+
api.derive[section].proposalHashes().pipe(
68+
switchMap((all) => _proposalsFrom(api, query, all))
6969
)
7070
);
7171
}
7272

73-
export function proposal (_section: Collective): (instanceId: string, api: DeriveApi) => (hash: Hash | Uint8Array | string) => Observable<DeriveCollectiveProposal | null> {
74-
return withSection(_section, (section, api) =>
73+
export function proposal (section: Collective): (instanceId: string, api: DeriveApi) => (hash: Hash | Uint8Array | string) => Observable<DeriveCollectiveProposal | null> {
74+
return withSection(section, (query, api) =>
7575
(hash: Hash | Uint8Array | string): Observable<DeriveCollectiveProposal | null> =>
76-
isFunction(api.query[section]?.proposals)
77-
? firstObservable(_proposalsFrom(section, api, [hash]))
76+
isFunction(query?.proposals)
77+
? firstObservable(_proposalsFrom(api, query, [hash]))
7878
: of(null)
7979
);
8080
}

0 commit comments

Comments
 (0)