Derive for electionsPhragmen (#1519)
This commit is contained in:
@@ -8,24 +8,58 @@ import { Codec } from '@polkadot/types/types';
|
||||
import { Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
import { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
import { Vec, u32 } from '@polkadot/types';
|
||||
import { createType, Vec, u32 } from '@polkadot/types';
|
||||
|
||||
import { DerivedElectionsInfo } from '../types';
|
||||
import { drr } from '../util/drr';
|
||||
|
||||
type Result = [Vec<[AccountId, BlockNumber] & Codec>, Vec<AccountId>, u32, u32, SetIndex, BlockNumber, VoteIndex, SetIndex];
|
||||
type ResultElections = [Vec<AccountId>, u32, u32, Vec<[AccountId, BlockNumber] & Codec>, SetIndex, BlockNumber, VoteIndex, SetIndex];
|
||||
|
||||
function query (api: ApiInterfaceRx): Observable<Result> {
|
||||
return api.queryMulti<Result>([
|
||||
api.query.elections.members,
|
||||
type ResultPhragmen = [Vec<AccountId>, u32, Vec<AccountId>, BlockNumber];
|
||||
|
||||
function deriveElections ([candidates, candidateCount, desiredSeats, members, nextVoterSet, termDuration, voteCount, voterCount]: ResultElections): DerivedElectionsInfo {
|
||||
return {
|
||||
candidates,
|
||||
candidateCount,
|
||||
desiredSeats,
|
||||
nextVoterSet,
|
||||
members: members.map(([accountId]): AccountId => accountId),
|
||||
termDuration,
|
||||
voteCount,
|
||||
voterCount
|
||||
};
|
||||
}
|
||||
|
||||
function queryElections (api: ApiInterfaceRx): Observable<DerivedElectionsInfo> {
|
||||
return api.queryMulti<ResultElections>([
|
||||
api.query.elections.candidates,
|
||||
api.query.elections.candidateCount,
|
||||
api.query.elections.desiredSeats,
|
||||
api.query.elections.members,
|
||||
api.query.elections.nextVoterSet,
|
||||
api.query.elections.termDuration,
|
||||
api.query.elections.voteCount,
|
||||
api.query.elections.voterCount
|
||||
]);
|
||||
]).pipe(map(deriveElections), drr());
|
||||
}
|
||||
|
||||
function derivePhragmen ([candidates, desiredMembers, members, termDuration]: ResultPhragmen): DerivedElectionsInfo {
|
||||
return {
|
||||
candidates,
|
||||
candidateCount: createType('u32', candidates.length),
|
||||
desiredSeats: desiredMembers,
|
||||
members,
|
||||
termDuration
|
||||
};
|
||||
}
|
||||
|
||||
function queryPhragmen (api: ApiInterfaceRx): Observable<DerivedElectionsInfo> {
|
||||
return api.queryMulti<ResultPhragmen>([
|
||||
api.query.electionsPhragmen.candidates,
|
||||
api.query.electionsPhragmen.desiredMembers,
|
||||
api.query.electionsPhragmen.members,
|
||||
api.query.electionsPhragmen.termDuration
|
||||
]).pipe(map(derivePhragmen), drr());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -43,24 +77,8 @@ function query (api: ApiInterfaceRx): Observable<Result> {
|
||||
*/
|
||||
export function info (api: ApiInterfaceRx): () => Observable<DerivedElectionsInfo> {
|
||||
return (): Observable<DerivedElectionsInfo> => {
|
||||
return query(api).pipe(
|
||||
map(([members, candidates, candidateCount, desiredSeats, nextVoterSet, termDuration, voteCount, voterCount]): DerivedElectionsInfo => ({
|
||||
members: members.reduce(
|
||||
(record: Record<string, BlockNumber>, [accountId, blockNumber]): Record<string, BlockNumber> => {
|
||||
record[accountId.toString()] = blockNumber;
|
||||
return record;
|
||||
},
|
||||
{}
|
||||
),
|
||||
candidates,
|
||||
candidateCount,
|
||||
desiredSeats,
|
||||
nextVoterSet,
|
||||
termDuration,
|
||||
voteCount,
|
||||
voterCount
|
||||
})),
|
||||
drr()
|
||||
);
|
||||
return api.query.electionsPhragmen
|
||||
? queryPhragmen(api)
|
||||
: queryElections(api);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -34,14 +34,14 @@ export interface DerivedContractFees {
|
||||
}
|
||||
|
||||
export interface DerivedElectionsInfo {
|
||||
members: Record<string, BlockNumber>;
|
||||
candidates: AccountId[];
|
||||
candidateCount: u32;
|
||||
desiredSeats: u32;
|
||||
nextVoterSet: SetIndex;
|
||||
members: AccountId[];
|
||||
nextVoterSet?: SetIndex;
|
||||
termDuration: BlockNumber;
|
||||
voteCount: VoteIndex;
|
||||
voterCount: SetIndex;
|
||||
voteCount?: VoteIndex;
|
||||
voterCount?: SetIndex;
|
||||
}
|
||||
|
||||
export interface DerivedFees {
|
||||
|
||||
Reference in New Issue
Block a user