collective proposals (#1671)

This commit is contained in:
Keith Ingram
2019-12-15 15:37:27 -05:00
committed by GitHub
parent 1da6193f34
commit ddd5eab7f5
8 changed files with 84 additions and 47 deletions
@@ -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';
@@ -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<DerivedCollectiveProposals> {
return combineLatest([
api.query[section].proposalOf.multi<Option<Proposal>>(hashes),
api.query[section].voting.multi<Option<Votes>>(hashes)
]).pipe(
map(([proposals, votes]: [Option<Proposal>[], Option<Votes>[]]): 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<DerivedCollectiveProposals> {
return memo((): Observable<DerivedCollectiveProposals> =>
api.query[section]
? api.query[section].proposals().pipe(
switchMap((proposals: Hash[]): Observable<DerivedCollectiveProposals> =>
retrieveProposals(api, section, proposals)
)
)
: of([] as DerivedCollectiveProposals)
);
}
+5 -40
View File
@@ -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<DerivedCouncilProposals> {
return combineLatest([
api.query.council.proposalOf.multi<Option<Proposal>>(hashes),
api.query.council.voting.multi<Option<Votes>>(hashes)
]).pipe(
map(([proposals, votes]: [Option<Proposal>[], Option<Votes>[]]): 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<DerivedCouncilProposals> {
return memo((): Observable<DerivedCouncilProposals> =>
api.query.council
? api.query.council.proposals().pipe(
switchMap((proposals: Hash[]): Observable<DerivedCouncilProposals> =>
retrieveProposals(api, proposals)
)
)
: of([] as DerivedCouncilProposals)
);
export function proposals (api: ApiInterfaceRx): () => Observable<DerivedCollectiveProposals> {
return collectiveProposals(api, 'council');
}
+2 -1
View File
@@ -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<AllSections> (api: ApiInterfaceRx, allSections: AllSect
}, {} as DeriveAllSections<AllSections>);
}
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<typeof derive>;
// FIXME The return type of this function should be {...ExactDerive, ...DeriveCustom}
@@ -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';
@@ -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<DerivedCollectiveProposals> {
return collectiveProposals(api, 'technicalCommittee');
}
@@ -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<TreasuryProposal>[];
approvalIds: ProposalIndex[];
councilProposals: DerivedCouncilProposals;
councilProposals: DerivedCollectiveProposals;
proposalCount: ProposalIndex;
}
@@ -65,7 +65,7 @@ function retrieveProposals (api: ApiInterfaceRx, proposalCount: ProposalIndex, a
api.query.treasury.proposals.multi<Option<TreasuryProposal>>(allIds),
api.derive.council.proposals()
]).pipe(
map(([allProposals, councilProposals]: [Option<TreasuryProposal>[], DerivedCouncilProposals]): DerivedTreasuryProposals =>
map(([allProposals, councilProposals]: [Option<TreasuryProposal>[], DerivedCollectiveProposals]): DerivedTreasuryProposals =>
parseResult(api, { allIds, allProposals, approvalIds, councilProposals, proposalCount })
)
);
+3 -3
View File
@@ -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;
}