Add derive for single council proposal (#2808)

This commit is contained in:
Jaco Greeff
2020-10-30 12:09:12 +01:00
committed by GitHub
parent 3ebe9f5243
commit ce1e6615e3
6 changed files with 47 additions and 23 deletions
+1
View File
@@ -15,6 +15,7 @@ Changes:
- Expand Rococo known types to cater for v1 (specVersion 10) - Expand Rococo known types to cater for v1 (specVersion 10)
- `registry.hash` now returns an `H256` object (as opposed to raw Uint8Array) - `registry.hash` now returns an `H256` object (as opposed to raw Uint8Array)
- Use utility logger in all relevant error messages (consistency in logging) - Use utility logger in all relevant error messages (consistency in logging)
- Add `derive.council.proposal(<hash>)` for retrieval of a single (expanded) proposal
- Correctly and transparently format type generation types (allowing for nested variants) - Correctly and transparently format type generation types (allowing for nested variants)
+33 -13
View File
@@ -12,14 +12,14 @@ import { isFunction } from '@polkadot/util';
import { memo } from '../util'; import { memo } from '../util';
type Result = [Hash[], Option<Proposal>[], Option<Votes>[]]; type Result = [(Hash | Uint8Array | string)[], Option<Proposal>[], Option<Votes>[]];
function parse ([hashes, proposals, votes]: Result): DeriveCollectiveProposal[] { function parse (api: ApiInterfaceRx, [hashes, proposals, votes]: Result): DeriveCollectiveProposal[] {
return proposals return proposals
.map((proposalOpt, index): DeriveCollectiveProposal | null => .map((proposalOpt, index): DeriveCollectiveProposal | null =>
proposalOpt.isSome proposalOpt.isSome
? { ? {
hash: hashes[index], hash: api.registry.createType('Hash', hashes[index]),
proposal: proposalOpt.unwrap(), proposal: proposalOpt.unwrap(),
votes: votes[index].unwrapOr(null) votes: votes[index].unwrapOr(null)
} }
@@ -28,21 +28,41 @@ function parse ([hashes, proposals, votes]: Result): DeriveCollectiveProposal[]
.filter((proposal): proposal is DeriveCollectiveProposal => !!proposal); .filter((proposal): proposal is DeriveCollectiveProposal => !!proposal);
} }
function _proposalsFrom (instanceId: string, api: ApiInterfaceRx, section: 'council' | 'technicalCommittee'): (hashes: (Hash | Uint8Array | string)[]) => Observable<DeriveCollectiveProposal[]> {
return memo(instanceId, (hashes: (Hash | Uint8Array | string)[]): Observable<DeriveCollectiveProposal[]> =>
(isFunction(api.query[section]?.proposals) && hashes.length
? combineLatest<Result>([
of(hashes),
api.query[section].proposalOf.multi<Option<Proposal>>(hashes),
api.query[section].voting.multi<Option<Votes>>(hashes)
])
: of([[], [], []] as Result)
).pipe(
map((result) => parse(api, result))
)
);
}
export function proposals (instanceId: string, api: ApiInterfaceRx, section: 'council' | 'technicalCommittee'): () => Observable<DeriveCollectiveProposal[]> { export function proposals (instanceId: string, api: ApiInterfaceRx, section: 'council' | 'technicalCommittee'): () => Observable<DeriveCollectiveProposal[]> {
const proposalsFrom = _proposalsFrom(instanceId, api, section);
return memo(instanceId, (): Observable<DeriveCollectiveProposal[]> => return memo(instanceId, (): Observable<DeriveCollectiveProposal[]> =>
isFunction(api.query[section]?.proposals) isFunction(api.query[section]?.proposals)
? api.query[section].proposals().pipe( ? api.query[section].proposals().pipe(
switchMap((hashes: Hash[]): Observable<Result> => switchMap(proposalsFrom)
hashes.length
? combineLatest([
of(hashes),
api.query[section].proposalOf.multi<Option<Proposal>>(hashes),
api.query[section].voting.multi<Option<Votes>>(hashes)
])
: of([[], [], []])
),
map(parse)
) )
: of([] as DeriveCollectiveProposal[]) : of([] as DeriveCollectiveProposal[])
); );
} }
export function proposal (instanceId: string, api: ApiInterfaceRx, section: 'council' | 'technicalCommittee'): (hash: Hash | Uint8Array | string) => Observable<DeriveCollectiveProposal | null> {
const proposalsFrom = _proposalsFrom(instanceId, api, section);
return memo(instanceId, (hash: Hash | Uint8Array | string): Observable<DeriveCollectiveProposal | null> =>
isFunction(api.query[section]?.proposals)
? proposalsFrom([hash]).pipe(
map(([proposal]) => proposal)
)
: of(null)
);
}
+8 -3
View File
@@ -2,12 +2,17 @@
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
import { ApiInterfaceRx } from '@polkadot/api/types'; import { ApiInterfaceRx } from '@polkadot/api/types';
import { DeriveCollectiveProposals } from '../types'; import { Hash } from '@polkadot/types/interfaces';
import { DeriveCollectiveProposal } from '../types';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
import { proposals as collectiveProposals } from '../collective'; import { proposal as collectiveProposal, proposals as collectiveProposals } from '../collective';
import { memo } from '../util'; import { memo } from '../util';
export function proposals (instanceId: string, api: ApiInterfaceRx): () => Observable<DeriveCollectiveProposals> { export function proposal (instanceId: string, api: ApiInterfaceRx): (hash: Hash | Uint8Array | string) => Observable<DeriveCollectiveProposal | null> {
return memo(instanceId, collectiveProposal(instanceId, api, 'council'));
}
export function proposals (instanceId: string, api: ApiInterfaceRx): () => Observable<DeriveCollectiveProposal[]> {
return memo(instanceId, collectiveProposals(instanceId, api, 'council')); return memo(instanceId, collectiveProposals(instanceId, api, 'council'));
} }
@@ -2,12 +2,12 @@
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
import { ApiInterfaceRx } from '@polkadot/api/types'; import { ApiInterfaceRx } from '@polkadot/api/types';
import { DeriveCollectiveProposals } from '../types'; import { DeriveCollectiveProposal } from '../types';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
import { proposals as collectiveProposals } from '../collective'; import { proposals as collectiveProposals } from '../collective';
import { memo } from '../util'; import { memo } from '../util';
export function proposals (instanceId: string, api: ApiInterfaceRx): () => Observable<DeriveCollectiveProposals> { export function proposals (instanceId: string, api: ApiInterfaceRx): () => Observable<DeriveCollectiveProposal[]> {
return memo(instanceId, collectiveProposals(instanceId, api, 'technicalCommittee')); return memo(instanceId, collectiveProposals(instanceId, api, 'technicalCommittee'));
} }
@@ -3,7 +3,7 @@
import { ApiInterfaceRx } from '@polkadot/api/types'; import { ApiInterfaceRx } from '@polkadot/api/types';
import { ProposalIndex, TreasuryProposal } from '@polkadot/types/interfaces'; import { ProposalIndex, TreasuryProposal } from '@polkadot/types/interfaces';
import { DeriveCollectiveProposals, DeriveTreasuryProposal, DeriveTreasuryProposals } from '../types'; import { DeriveCollectiveProposal, DeriveTreasuryProposal, DeriveTreasuryProposals } from '../types';
import { Observable, combineLatest, of } from 'rxjs'; import { Observable, combineLatest, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators'; import { map, switchMap } from 'rxjs/operators';
@@ -15,7 +15,7 @@ interface Result {
allIds: ProposalIndex[]; allIds: ProposalIndex[];
allProposals: Option<TreasuryProposal>[]; allProposals: Option<TreasuryProposal>[];
approvalIds: ProposalIndex[]; approvalIds: ProposalIndex[];
councilProposals: DeriveCollectiveProposals; councilProposals: DeriveCollectiveProposal[];
proposalCount: ProposalIndex; proposalCount: ProposalIndex;
} }
@@ -64,7 +64,7 @@ function retrieveProposals (api: ApiInterfaceRx, proposalCount: ProposalIndex, a
api.query.treasury.proposals.multi<Option<TreasuryProposal>>(allIds), api.query.treasury.proposals.multi<Option<TreasuryProposal>>(allIds),
api.derive.council.proposals() api.derive.council.proposals()
]).pipe( ]).pipe(
map(([allProposals, councilProposals]: [Option<TreasuryProposal>[], DeriveCollectiveProposals]): DeriveTreasuryProposals => map(([allProposals, councilProposals]: [Option<TreasuryProposal>[], DeriveCollectiveProposal[]]): DeriveTreasuryProposals =>
parseResult(api, { allIds, allProposals, approvalIds, councilProposals, proposalCount }) parseResult(api, { allIds, allProposals, approvalIds, councilProposals, proposalCount })
) )
); );
-2
View File
@@ -60,8 +60,6 @@ export interface DeriveCollectiveProposal {
votes: Votes | null; votes: Votes | null;
} }
export type DeriveCollectiveProposals = DeriveCollectiveProposal[];
export interface DeriveFees { export interface DeriveFees {
creationFee: Balance; creationFee: Balance;
existentialDeposit: Balance; existentialDeposit: Balance;