diff --git a/CHANGELOG.md b/CHANGELOG.md index a3acfdd2d9..29bc62f57a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/packages/api-derive/src/collective/helpers.ts b/packages/api-derive/src/collective/helpers.ts index b852173b78..fa9add1636 100644 --- a/packages/api-derive/src/collective/helpers.ts +++ b/packages/api-derive/src/collective/helpers.ts @@ -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 Observable> (_section: Collective, fn: (section: string, api: DeriveApi, instanceId: string) => F): (instanceId: string, api: DeriveApi) => F { +export function withSection Observable> (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 (method: 'members' | 'proposals' | 'proposalCount', empty: T): (_section: Collective) => (instanceId: string, api: DeriveApi) => () => Observable { - return (_section: Collective) => - withSection(_section, (section, api) => +export function callMethod (method: 'members' | 'proposals' | 'proposalCount', empty: T): (section: Collective) => (instanceId: string, api: DeriveApi) => () => Observable { + return (section: Collective) => + withSection(section, (query) => (): Observable => - isFunction(api.query[section]?.[method]) - ? api.query[section as 'council'][method]() as unknown as Observable + isFunction(query?.[method]) + ? query[method]() as unknown as Observable : of(empty) ); } diff --git a/packages/api-derive/src/collective/prime.ts b/packages/api-derive/src/collective/prime.ts index ad5187c248..f4ec48547f 100644 --- a/packages/api-derive/src/collective/prime.ts +++ b/packages/api-derive/src/collective/prime.ts @@ -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 { - return withSection(_section, (section, api) => +export function prime (section: Collective): (instanceId: string, api: DeriveApi) => () => Observable { + return withSection(section, (query) => (): Observable => - 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) diff --git a/packages/api-derive/src/collective/proposals.ts b/packages/api-derive/src/collective/proposals.ts index 18b5881437..2e3052e839 100644 --- a/packages/api-derive/src/collective/proposals.ts +++ b/packages/api-derive/src/collective/proposals.ts @@ -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 { - return (isFunction(api.query[section]?.proposals) && hashes.length +function _proposalsFrom (api: DeriveApi, query: DeriveApi['query']['council'], hashes: (Hash | Uint8Array | string)[]): Observable { + return (isFunction(query?.proposals) && hashes.length ? combineLatest([ of(hashes), // this should simply be api.query[section].proposalOf.multi>(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>(h).pipe( + query.proposalOf>(h).pipe( catchError(() => of(null)) ) )), - api.query[section].voting.multi>(hashes) + query.voting.multi>(hashes) ]) : of([[], [], []]) ).pipe( @@ -54,27 +54,27 @@ function _proposalsFrom (section: string, api: DeriveApi, hashes: (Hash | Uint8A ); } -export function hasProposals (_section: Collective): (instanceId: string, api: DeriveApi) => () => Observable { - return withSection(_section, (section, api) => +export function hasProposals (section: Collective): (instanceId: string, api: DeriveApi) => () => Observable { + return withSection(section, (query) => (): Observable => - of(isFunction(api.query[section]?.proposals)) + of(isFunction(query?.proposals)) ); } -export function proposals (_section: Collective): (instanceId: string, api: DeriveApi) => () => Observable { - return withSection(_section, (section, api) => +export function proposals (section: Collective): (instanceId: string, api: DeriveApi) => () => Observable { + return withSection(section, (query, api) => (): Observable => - 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 { - return withSection(_section, (section, api) => +export function proposal (section: Collective): (instanceId: string, api: DeriveApi) => (hash: Hash | Uint8Array | string) => Observable { + return withSection(section, (query, api) => (hash: Hash | Uint8Array | string): Observable => - isFunction(api.query[section]?.proposals) - ? firstObservable(_proposalsFrom(section, api, [hash])) + isFunction(query?.proposals) + ? firstObservable(_proposalsFrom(api, query, [hash])) : of(null) ); }