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)
This commit is contained in:
Jaco
2021-12-21 10:17:41 +02:00
committed by GitHub
parent a2c027b7b7
commit 523f1622b5
4 changed files with 33 additions and 31 deletions
+1
View File
@@ -4,6 +4,7 @@
Changes:
- Fix alias module mapping in collective derives
- Update `dispathchQueue` derive to align with latest Substrate
- Update to latest Substrate metadata
+11 -10
View File
@@ -11,25 +11,26 @@ import { isFunction } from '@polkadot/util';
import { memo } from '../util';
export function getInstance (api: DeriveApi, section: string): string {
export function getInstance (api: DeriveApi, section: string): DeriveApi['query']['council'] {
const instances = api.registry.getModuleInstances(api.runtimeVersion.specName.toString(), section);
return instances && instances.length
const name = instances && instances.length
? instances[0]
: section;
return api.query[name as 'council'];
}
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 {
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 {
return (instanceId: string, api: DeriveApi) =>
memo(instanceId, fn(getInstance(api, _section), api, instanceId)) as unknown as F;
memo(instanceId, fn(getInstance(api, section), api, instanceId)) as unknown as F;
}
export function callMethod <T> (method: 'members' | 'proposals' | 'proposalCount', empty: T): (_section: Collective) => (instanceId: string, api: DeriveApi) => () => Observable<T> {
return (_section: Collective) =>
withSection(_section, (section, api) =>
export function callMethod <T> (method: 'members' | 'proposals' | 'proposalCount', empty: T): (section: Collective) => (instanceId: string, api: DeriveApi) => () => Observable<T> {
return (section: Collective) =>
withSection(section, (query) =>
(): Observable<T> =>
isFunction(api.query[section]?.[method])
? api.query[section as 'council'][method]() as unknown as Observable<T>
isFunction(query?.[method])
? query[method]() as unknown as Observable<T>
: of(empty)
);
}
+6 -6
View File
@@ -15,13 +15,13 @@ import { withSection } from './helpers';
// We are re-exporting these from here to ensure that *.d.ts generation is correct
export type { AccountId } from '@polkadot/types/interfaces';
export function prime (_section: Collective): (instanceId: string, api: DeriveApi) => () => Observable<AccountId | null> {
return withSection(_section, (section, api) =>
export function prime (section: Collective): (instanceId: string, api: DeriveApi) => () => Observable<AccountId | null> {
return withSection(section, (query) =>
(): Observable<AccountId | null> =>
isFunction(api.query[section as 'council']?.prime)
? api.query[section as 'council'].prime().pipe(
map((optPrime): AccountId | null =>
optPrime.unwrapOr(null)
isFunction(query?.prime)
? query.prime().pipe(
map((o): AccountId | null =>
o.unwrapOr(null)
)
)
: of(null)
+15 -15
View File
@@ -34,19 +34,19 @@ function parse (api: DeriveApi, [hashes, proposals, votes]: Result): DeriveColle
.filter((proposal): proposal is DeriveCollectiveProposal => !!proposal);
}
function _proposalsFrom (section: string, api: DeriveApi, hashes: (Hash | Uint8Array | string)[]): Observable<DeriveCollectiveProposal[]> {
return (isFunction(api.query[section]?.proposals) && hashes.length
function _proposalsFrom (api: DeriveApi, query: DeriveApi['query']['council'], hashes: (Hash | Uint8Array | string)[]): Observable<DeriveCollectiveProposal[]> {
return (isFunction(query?.proposals) && hashes.length
? combineLatest([
of(hashes),
// this should simply be api.query[section].proposalOf.multi<Option<Proposal>>(hashes),
// however we have had cases on Edgeware where the indices have moved around after an
// upgrade, which results in invalid on-chain data
combineLatest(hashes.map((h) =>
api.query[section].proposalOf<Option<Proposal>>(h).pipe(
query.proposalOf<Option<Proposal>>(h).pipe(
catchError(() => of(null))
)
)),
api.query[section].voting.multi<Option<Votes>>(hashes)
query.voting.multi<Option<Votes>>(hashes)
])
: of<Result>([[], [], []])
).pipe(
@@ -54,27 +54,27 @@ function _proposalsFrom (section: string, api: DeriveApi, hashes: (Hash | Uint8A
);
}
export function hasProposals (_section: Collective): (instanceId: string, api: DeriveApi) => () => Observable<boolean> {
return withSection(_section, (section, api) =>
export function hasProposals (section: Collective): (instanceId: string, api: DeriveApi) => () => Observable<boolean> {
return withSection(section, (query) =>
(): Observable<boolean> =>
of(isFunction(api.query[section]?.proposals))
of(isFunction(query?.proposals))
);
}
export function proposals (_section: Collective): (instanceId: string, api: DeriveApi) => () => Observable<DeriveCollectiveProposal[]> {
return withSection(_section, (section, api) =>
export function proposals (section: Collective): (instanceId: string, api: DeriveApi) => () => Observable<DeriveCollectiveProposal[]> {
return withSection(section, (query, api) =>
(): Observable<DeriveCollectiveProposal[]> =>
api.derive[section as 'council'].proposalHashes().pipe(
switchMap((all) => _proposalsFrom(section, api, all))
api.derive[section].proposalHashes().pipe(
switchMap((all) => _proposalsFrom(api, query, all))
)
);
}
export function proposal (_section: Collective): (instanceId: string, api: DeriveApi) => (hash: Hash | Uint8Array | string) => Observable<DeriveCollectiveProposal | null> {
return withSection(_section, (section, api) =>
export function proposal (section: Collective): (instanceId: string, api: DeriveApi) => (hash: Hash | Uint8Array | string) => Observable<DeriveCollectiveProposal | null> {
return withSection(section, (query, api) =>
(hash: Hash | Uint8Array | string): Observable<DeriveCollectiveProposal | null> =>
isFunction(api.query[section]?.proposals)
? firstObservable(_proposalsFrom(section, api, [hash]))
isFunction(query?.proposals)
? firstObservable(_proposalsFrom(api, query, [hash]))
: of(null)
);
}