From ddd5eab7f54144fd252b4a8da8923f0b2aa8d26d Mon Sep 17 00:00:00 2001 From: Keith Ingram Date: Sun, 15 Dec 2019 15:37:27 -0500 Subject: [PATCH] collective proposals (#1671) --- packages/api-derive/src/collective/index.ts | 5 ++ .../api-derive/src/collective/proposals.ts | 48 +++++++++++++++++++ packages/api-derive/src/council/proposals.ts | 45 ++--------------- packages/api-derive/src/index.ts | 3 +- .../src/technicalCommittee/index.ts | 5 ++ .../src/technicalCommittee/proposals.ts | 13 +++++ packages/api-derive/src/treasury/proposals.ts | 6 +-- packages/api-derive/src/types.ts | 6 +-- 8 files changed, 84 insertions(+), 47 deletions(-) create mode 100644 packages/api-derive/src/collective/index.ts create mode 100644 packages/api-derive/src/collective/proposals.ts create mode 100644 packages/api-derive/src/technicalCommittee/index.ts create mode 100644 packages/api-derive/src/technicalCommittee/proposals.ts diff --git a/packages/api-derive/src/collective/index.ts b/packages/api-derive/src/collective/index.ts new file mode 100644 index 0000000000..b8ce486d01 --- /dev/null +++ b/packages/api-derive/src/collective/index.ts @@ -0,0 +1,5 @@ +// Copyright 2017-2019 @polkadot/api-derive authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +export * from './proposals'; diff --git a/packages/api-derive/src/collective/proposals.ts b/packages/api-derive/src/collective/proposals.ts new file mode 100644 index 0000000000..f5d3dfa65f --- /dev/null +++ b/packages/api-derive/src/collective/proposals.ts @@ -0,0 +1,48 @@ +// Copyright 2017-2019 @polkadot/api-derive authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +import { ApiInterfaceRx } from '@polkadot/api/types'; +import { Hash, Proposal, Votes } from '@polkadot/types/interfaces'; +import { DerivedCollectiveProposals } from '../types'; + +import { Observable, combineLatest, of } from 'rxjs'; +import { map, switchMap } from 'rxjs/operators'; +import { Option } from '@polkadot/types'; + +import { memo } from '../util'; + +function retrieveProposals (api: ApiInterfaceRx, section: 'council' | 'technicalCommittee', hashes: Hash[]): Observable { + return combineLatest([ + api.query[section].proposalOf.multi>(hashes), + api.query[section].voting.multi>(hashes) + ]).pipe( + map(([proposals, votes]: [Option[], Option[]]): DerivedCollectiveProposals => { + const result: DerivedCollectiveProposals = []; + + proposals.forEach((proposalOpt, index): void => { + if (proposalOpt.isSome) { + result.push({ + hash: hashes[index], + proposal: proposalOpt.unwrap(), + votes: votes[index].unwrapOr(null) + }); + } + }); + + return result; + }) + ); +} + +export function proposals (api: ApiInterfaceRx, section: 'council' | 'technicalCommittee'): () => Observable { + return memo((): Observable => + api.query[section] + ? api.query[section].proposals().pipe( + switchMap((proposals: Hash[]): Observable => + retrieveProposals(api, section, proposals) + ) + ) + : of([] as DerivedCollectiveProposals) + ); +} diff --git a/packages/api-derive/src/council/proposals.ts b/packages/api-derive/src/council/proposals.ts index d54c088651..3b34907185 100644 --- a/packages/api-derive/src/council/proposals.ts +++ b/packages/api-derive/src/council/proposals.ts @@ -3,46 +3,11 @@ // of the Apache-2.0 license. See the LICENSE file for details. import { ApiInterfaceRx } from '@polkadot/api/types'; -import { Hash, Proposal, Votes } from '@polkadot/types/interfaces'; -import { DerivedCouncilProposals } from '../types'; +import { DerivedCollectiveProposals } from '../types'; -import { Observable, combineLatest, of } from 'rxjs'; -import { map, switchMap } from 'rxjs/operators'; -import { Option } from '@polkadot/types'; +import { Observable } from 'rxjs'; +import { proposals as collectiveProposals } from '../collective'; -import { memo } from '../util'; - -function retrieveProposals (api: ApiInterfaceRx, hashes: Hash[]): Observable { - return combineLatest([ - api.query.council.proposalOf.multi>(hashes), - api.query.council.voting.multi>(hashes) - ]).pipe( - map(([proposals, votes]: [Option[], Option[]]): DerivedCouncilProposals => { - const result: DerivedCouncilProposals = []; - - proposals.forEach((proposalOpt, index): void => { - if (proposalOpt.isSome) { - result.push({ - hash: hashes[index], - proposal: proposalOpt.unwrap(), - votes: votes[index].unwrapOr(null) - }); - } - }); - - return result; - }) - ); -} - -export function proposals (api: ApiInterfaceRx): () => Observable { - return memo((): Observable => - api.query.council - ? api.query.council.proposals().pipe( - switchMap((proposals: Hash[]): Observable => - retrieveProposals(api, proposals) - ) - ) - : of([] as DerivedCouncilProposals) - ); +export function proposals (api: ApiInterfaceRx): () => Observable { + return collectiveProposals(api, 'council'); } diff --git a/packages/api-derive/src/index.ts b/packages/api-derive/src/index.ts index 48ae1cc3d9..8c20cc3c01 100644 --- a/packages/api-derive/src/index.ts +++ b/packages/api-derive/src/index.ts @@ -17,6 +17,7 @@ import * as elections from './elections'; import * as imOnline from './imOnline'; import * as session from './session'; import * as staking from './staking'; +import * as technicalCommittee from './technicalCommittee'; import * as treasury from './treasury'; export * from './type'; @@ -58,7 +59,7 @@ function injectFunctions (api: ApiInterfaceRx, allSections: AllSect }, {} as DeriveAllSections); } -export const derive = { accounts, balances, chain, contracts, council, democracy, elections, imOnline, session, staking, treasury }; +export const derive = { accounts, balances, chain, contracts, council, democracy, elections, imOnline, session, technicalCommittee, staking, treasury }; export type ExactDerive = DeriveAllSections; // FIXME The return type of this function should be {...ExactDerive, ...DeriveCustom} diff --git a/packages/api-derive/src/technicalCommittee/index.ts b/packages/api-derive/src/technicalCommittee/index.ts new file mode 100644 index 0000000000..b8ce486d01 --- /dev/null +++ b/packages/api-derive/src/technicalCommittee/index.ts @@ -0,0 +1,5 @@ +// Copyright 2017-2019 @polkadot/api-derive authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +export * from './proposals'; diff --git a/packages/api-derive/src/technicalCommittee/proposals.ts b/packages/api-derive/src/technicalCommittee/proposals.ts new file mode 100644 index 0000000000..ad52c0e83f --- /dev/null +++ b/packages/api-derive/src/technicalCommittee/proposals.ts @@ -0,0 +1,13 @@ +// Copyright 2017-2019 @polkadot/api-derive authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +import { ApiInterfaceRx } from '@polkadot/api/types'; +import { DerivedCollectiveProposals } from '../types'; + +import { Observable } from 'rxjs'; +import { proposals as collectiveProposals } from '../collective'; + +export function proposals (api: ApiInterfaceRx): () => Observable { + return collectiveProposals(api, 'technicalCommittee'); +} diff --git a/packages/api-derive/src/treasury/proposals.ts b/packages/api-derive/src/treasury/proposals.ts index ef6618fe38..5ac210987c 100644 --- a/packages/api-derive/src/treasury/proposals.ts +++ b/packages/api-derive/src/treasury/proposals.ts @@ -4,7 +4,7 @@ import { ApiInterfaceRx } from '@polkadot/api/types'; import { ProposalIndex, TreasuryProposal } from '@polkadot/types/interfaces'; -import { DerivedCouncilProposals, DerivedTreasuryProposal, DerivedTreasuryProposals } from '../types'; +import { DerivedCollectiveProposals, DerivedTreasuryProposal, DerivedTreasuryProposals } from '../types'; import { Observable, combineLatest, of } from 'rxjs'; import { map, switchMap } from 'rxjs/operators'; @@ -16,7 +16,7 @@ interface Result { allIds: ProposalIndex[]; allProposals: Option[]; approvalIds: ProposalIndex[]; - councilProposals: DerivedCouncilProposals; + councilProposals: DerivedCollectiveProposals; proposalCount: ProposalIndex; } @@ -65,7 +65,7 @@ function retrieveProposals (api: ApiInterfaceRx, proposalCount: ProposalIndex, a api.query.treasury.proposals.multi>(allIds), api.derive.council.proposals() ]).pipe( - map(([allProposals, councilProposals]: [Option[], DerivedCouncilProposals]): DerivedTreasuryProposals => + map(([allProposals, councilProposals]: [Option[], DerivedCollectiveProposals]): DerivedTreasuryProposals => parseResult(api, { allIds, allProposals, approvalIds, councilProposals, proposalCount }) ) ); diff --git a/packages/api-derive/src/types.ts b/packages/api-derive/src/types.ts index df028b8de1..1d5b03576f 100644 --- a/packages/api-derive/src/types.ts +++ b/packages/api-derive/src/types.ts @@ -50,13 +50,13 @@ export interface DerivedContractFees { transferFee: BN; } -export interface DerivedCouncilProposal { +export interface DerivedCollectiveProposal { hash: Hash; proposal: Proposal; votes: Votes | null; } -export type DerivedCouncilProposals = DerivedCouncilProposal[]; +export type DerivedCollectiveProposals = DerivedCollectiveProposal[]; export interface DerivedElectionsInfo { candidates: AccountId[]; @@ -179,7 +179,7 @@ export interface DerivedStakingOverview extends DeriveSessionIndexes { } export interface DerivedTreasuryProposal { - council: DerivedCouncilProposal[]; + council: DerivedCollectiveProposal[]; id: ProposalIndex; proposal: TreasuryProposal; }