From 772769c981dbf7d7e051c59fbd91b1bc9bb67312 Mon Sep 17 00:00:00 2001 From: Jaco Date: Mon, 26 Apr 2021 14:57:30 +0300 Subject: [PATCH] Add membership derives (#3442) --- packages/api-derive/src/collective/proposals.ts | 10 ++++++---- packages/api-derive/src/council/proposals.ts | 4 ++-- packages/api-derive/src/index.ts | 4 +++- packages/api-derive/src/membership/index.ts | 4 ++++ packages/api-derive/src/membership/proposals.ts | 13 +++++++++++++ 5 files changed, 28 insertions(+), 7 deletions(-) create mode 100644 packages/api-derive/src/membership/index.ts create mode 100644 packages/api-derive/src/membership/proposals.ts diff --git a/packages/api-derive/src/collective/proposals.ts b/packages/api-derive/src/collective/proposals.ts index 15f5893b4d..b0e826e1ac 100644 --- a/packages/api-derive/src/collective/proposals.ts +++ b/packages/api-derive/src/collective/proposals.ts @@ -13,6 +13,8 @@ import { catchError, map, switchMap } from '@polkadot/x-rxjs/operators'; import { memo } from '../util'; +type Collective = 'council' | 'membership' | 'technicalCommittee'; + type Result = [(Hash | Uint8Array | string)[], (Option | null)[], Option[]]; function parse (api: ApiInterfaceRx, [hashes, proposals, votes]: Result): DeriveCollectiveProposal[] { @@ -29,7 +31,7 @@ function parse (api: ApiInterfaceRx, [hashes, proposals, votes]: Result): Derive .filter((proposal): proposal is DeriveCollectiveProposal => !!proposal); } -function _proposalsFrom (instanceId: string, api: ApiInterfaceRx, section: 'council' | 'technicalCommittee' = 'council'): (hashes: (Hash | Uint8Array | string)[]) => Observable { +function _proposalsFrom (instanceId: string, api: ApiInterfaceRx, section: Collective): (hashes: (Hash | Uint8Array | string)[]) => Observable { return memo(instanceId, (hashes: (Hash | Uint8Array | string)[]): Observable => (isFunction(api.query[section]?.proposals) && hashes.length ? combineLatest([ @@ -49,19 +51,19 @@ function _proposalsFrom (instanceId: string, api: ApiInterfaceRx, section: 'coun ); } -export function proposals (instanceId: string, api: ApiInterfaceRx, section: 'council' | 'technicalCommittee' = 'council'): () => Observable { +export function proposals (instanceId: string, api: ApiInterfaceRx, section: Collective): () => Observable { const proposalsFrom = _proposalsFrom(instanceId, api, section); return memo(instanceId, (): Observable => isFunction(api.query[section]?.proposals) - ? api.query[section].proposals().pipe( + ? api.query[section as 'council'].proposals().pipe( switchMap(proposalsFrom) ) : of([] as DeriveCollectiveProposal[]) ); } -export function proposal (instanceId: string, api: ApiInterfaceRx, section: 'council' | 'technicalCommittee' = 'council'): (hash: Hash | Uint8Array | string) => Observable { +export function proposal (instanceId: string, api: ApiInterfaceRx, section: Collective): (hash: Hash | Uint8Array | string) => Observable { const proposalsFrom = _proposalsFrom(instanceId, api, section); return memo(instanceId, (hash: Hash | Uint8Array | string): Observable => diff --git a/packages/api-derive/src/council/proposals.ts b/packages/api-derive/src/council/proposals.ts index 1a719a508c..d6c5faf5c2 100644 --- a/packages/api-derive/src/council/proposals.ts +++ b/packages/api-derive/src/council/proposals.ts @@ -10,9 +10,9 @@ import { proposal as collectiveProposal, proposals as collectiveProposals } from import { memo } from '../util'; export function proposal (instanceId: string, api: ApiInterfaceRx): (hash: Hash | Uint8Array | string) => Observable { - return memo(instanceId, collectiveProposal(instanceId, api)); + return memo(instanceId, collectiveProposal(instanceId, api, 'council')); } export function proposals (instanceId: string, api: ApiInterfaceRx): () => Observable { - return memo(instanceId, collectiveProposals(instanceId, api)); + return memo(instanceId, collectiveProposals(instanceId, api, 'council')); } diff --git a/packages/api-derive/src/index.ts b/packages/api-derive/src/index.ts index 6cb6aeee70..f4933363c0 100644 --- a/packages/api-derive/src/index.ts +++ b/packages/api-derive/src/index.ts @@ -16,6 +16,7 @@ import * as council from './council'; import * as democracy from './democracy'; import * as elections from './elections'; import * as imOnline from './imOnline'; +import * as membership from './membership'; import * as parachains from './parachains'; import * as session from './session'; import * as society from './society'; @@ -27,7 +28,7 @@ import * as tx from './tx'; export { packageInfo } from './packageInfo'; export * from './type'; -export const derive = { accounts, balances, bounties, chain, contracts, council, democracy, elections, imOnline, parachains, session, society, staking, technicalCommittee, treasury, tx }; +export const derive = { accounts, balances, bounties, chain, contracts, council, democracy, elections, imOnline, membership, parachains, session, society, staking, technicalCommittee, treasury, tx }; type DeriveSection
= { [Method in keyof Section]: Section[Method] extends AnyFunction @@ -49,6 +50,7 @@ const deriveAvail: Record = { democracy: ['democracy'], elections: ['electionsPhragmen', 'elections'], imOnline: ['imOnline'], + membership: ['membership'], parachains: ['parachains', 'registrar'], session: ['session'], society: ['society'], diff --git a/packages/api-derive/src/membership/index.ts b/packages/api-derive/src/membership/index.ts new file mode 100644 index 0000000000..c927b748a9 --- /dev/null +++ b/packages/api-derive/src/membership/index.ts @@ -0,0 +1,4 @@ +// Copyright 2017-2021 @polkadot/api-derive authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +export * from './proposals'; diff --git a/packages/api-derive/src/membership/proposals.ts b/packages/api-derive/src/membership/proposals.ts new file mode 100644 index 0000000000..e362b0f9aa --- /dev/null +++ b/packages/api-derive/src/membership/proposals.ts @@ -0,0 +1,13 @@ +// Copyright 2017-2021 @polkadot/api-derive authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import type { ApiInterfaceRx } from '@polkadot/api/types'; +import type { Observable } from '@polkadot/x-rxjs'; +import type { DeriveCollectiveProposal } from '../types'; + +import { proposals as collectiveProposals } from '../collective'; +import { memo } from '../util'; + +export function proposals (instanceId: string, api: ApiInterfaceRx): () => Observable { + return memo(instanceId, collectiveProposals(instanceId, api, 'membership')); +}