diff --git a/packages/api-derive/src/elections/approvalsOf.ts b/packages/api-derive/src/elections/approvalsOf.ts index 6bb120389d..b1b3e896b6 100644 --- a/packages/api-derive/src/elections/approvalsOf.ts +++ b/packages/api-derive/src/elections/approvalsOf.ts @@ -5,12 +5,29 @@ import { AccountId, ApprovalFlag, SetIndex } from '@polkadot/types/interfaces'; import { ApiInterfaceRx } from '@polkadot/api/types'; -import { Observable } from 'rxjs'; +import { Observable, of } from 'rxjs'; import { Vec } from '@polkadot/types'; import { switchMap, map } from 'rxjs/operators'; import { approvalFlagsToBools } from '../util/approvalFlagsToBools'; import { memo } from '../util'; +function queryElections (api: ApiInterfaceRx, who: AccountId | string): Observable { + return api.query.elections.nextVoterSet().pipe( + switchMap((nextVoterSet: SetIndex): Observable[]> => + api.query.elections.approvalsOf.multi( + [...Array(nextVoterSet.toNumber() + 1).keys()].map((i): [string, number] => + [who.toString(), i] + ) + ) as any as Observable[]> + ), + map((votes: Vec[]): boolean[][] => + votes.map((flags: Vec): boolean[] => + approvalFlagsToBools(flags) + ) + ) + ); +} + /** * @name approvalsOf * @returns A nested array of boolean approvals for the given account in each voter set index, converted from its ApprovalFlag(s). @@ -23,20 +40,10 @@ import { memo } from '../util'; * }); * ``` */ -export function approvalsOf (api: ApiInterfaceRx): (who: AccountId) => Observable { +export function approvalsOf (api: ApiInterfaceRx): (who: AccountId | string) => Observable { return memo((who: AccountId | string): Observable => - api.query.elections.nextVoterSet().pipe( - switchMap((nextVoterSet: SetIndex): Observable[]> => - api.query.elections.approvalsOf.multi( - [...Array(nextVoterSet.toNumber() + 1).keys()].map((i): [string, number] => [ - who.toString(), i] - ) - ) as any as Observable[]> - ), - map((votes: Vec[]): boolean[][] => - votes.map((flags: Vec): boolean[] => - approvalFlagsToBools(flags) - ) - ) - )); + api.query.elections + ? queryElections(api, who) + : of([]) + ); } diff --git a/packages/api-derive/src/elections/approvalsOfAt.ts b/packages/api-derive/src/elections/approvalsOfAt.ts index 6dbb6c50fc..346954c3c6 100644 --- a/packages/api-derive/src/elections/approvalsOfAt.ts +++ b/packages/api-derive/src/elections/approvalsOfAt.ts @@ -5,7 +5,7 @@ import { AccountId, ApprovalFlag, SetIndex } from '@polkadot/types/interfaces'; import BN from 'bn.js'; -import { Observable } from 'rxjs'; +import { Observable, of } from 'rxjs'; import { map } from 'rxjs/operators'; import { ApiInterfaceRx } from '@polkadot/api/types'; import { Vec } from '@polkadot/types'; @@ -13,6 +13,12 @@ import { Vec } from '@polkadot/types'; import { approvalFlagsToBools } from '../util/approvalFlagsToBools'; import { memo } from '../util'; +function queryElections (api: ApiInterfaceRx, who: AccountId | string, at: SetIndex | BN | number): Observable { + return api.query.elections.approvalsOf>([who.toString(), at]).pipe( + map((flags: Vec): boolean[] => approvalFlagsToBools(flags)) + ); +} + /** * @name approvalsOfAt * @returns An array of boolean approvals for the account and set index, converted from the returned ApprovalFlag. @@ -27,7 +33,8 @@ import { memo } from '../util'; */ export function approvalsOfAt (api: ApiInterfaceRx): (who: AccountId, at: SetIndex) => Observable { return memo((who: AccountId | string, at: SetIndex | BN | number): Observable => - api.query.elections.approvalsOf>([who.toString(), at]).pipe( - map((flags: Vec): boolean[] => approvalFlagsToBools(flags)) - )); + api.query.elections + ? queryElections(api, who, at) + : of([]) + ); } diff --git a/packages/api-derive/src/elections/info.ts b/packages/api-derive/src/elections/info.ts index a826c46b7d..1050c438d3 100644 --- a/packages/api-derive/src/elections/info.ts +++ b/packages/api-derive/src/elections/info.ts @@ -94,10 +94,9 @@ function queryPhragmen (api: ApiInterfaceRx): Observable { * ``` */ export function info (api: ApiInterfaceRx): () => Observable { - const query = api.query.electionsPhragmen - ? queryPhragmen - : queryElections; - return memo((): Observable => - query(api)); + api.query.electionsPhragmen + ? queryPhragmen(api) + : queryElections(api) + ); } diff --git a/packages/api-derive/src/elections/voterPositions.ts b/packages/api-derive/src/elections/voterPositions.ts index 8861d86654..01e283d817 100644 --- a/packages/api-derive/src/elections/voterPositions.ts +++ b/packages/api-derive/src/elections/voterPositions.ts @@ -13,6 +13,36 @@ import { switchMap, map } from 'rxjs/operators'; import { memo } from '../util'; +function queryElections (api: ApiInterfaceRx): Observable { + return api.query.elections.nextVoterSet().pipe( + switchMap((nextVoterSet: SetIndex): Observable<[BN, Vec>[]]> => combineLatest( + of(api.consts.elections.voterSetSize) as any as Observable, + api.query.elections.voters.multi([...Array(+nextVoterSet + 1).keys()].map((_, i): [number] => [i])) as any as Observable>[]> + )), + map((result: [BN, Vec>[]]): DerivedVoterPositions => { + const [setSize, voters] = result; + + return voters.reduce((result: DerivedVoterPositions, vec, setIndex): DerivedVoterPositions => { + vec.forEach((e, index): void => { + // re-create the index based on position 0 is [0][0] and likewise + // 64 (0..63 in first) is [1][0] (the first index value in set 2) + const accountId: AccountId | null = e.unwrapOr(null); + + if (accountId) { + result[accountId.toString()] = { + globalIndex: setSize.muln(setIndex).addn(index), + index: new BN(index), + setIndex: createType('SetIndex', setIndex) + }; + } + }); + + return result; + }, {}); + }) + ); +} + /** * @name voterPositions * @returns An mapping of all current voter accounts to their voter set and global index. @@ -28,31 +58,8 @@ import { memo } from '../util'; */ export function voterPositions (api: ApiInterfaceRx): () => Observable { return memo((): Observable => - api.query.elections.nextVoterSet().pipe( - switchMap((nextVoterSet: SetIndex): Observable<[BN, Vec>[]]> => combineLatest( - of(api.consts.elections.voterSetSize) as any as Observable, - api.query.elections.voters.multi([...Array(+nextVoterSet + 1).keys()].map((_, i): [number] => [i])) as any as Observable>[]> - )), - map((result: [BN, Vec>[]]): DerivedVoterPositions => { - const [setSize, voters] = result; - - return voters.reduce((result: DerivedVoterPositions, vec, setIndex): DerivedVoterPositions => { - vec.forEach((e, index): void => { - // re-create the index based on position 0 is [0][0] and likewise - // 64 (0..63 in first) is [1][0] (the first index value in set 2) - const accountId: AccountId | null = e.unwrapOr(null); - - if (accountId) { - result[accountId.toString()] = { - globalIndex: setSize.muln(setIndex).addn(index), - index: new BN(index), - setIndex: createType('SetIndex', setIndex) - }; - } - }); - - return result; - }, {}); - }) - )); + api.query.elections + ? queryElections(api) + : of({}) + ); } diff --git a/packages/api-derive/src/elections/voters.ts b/packages/api-derive/src/elections/voters.ts index a10db72ed5..40f1990144 100644 --- a/packages/api-derive/src/elections/voters.ts +++ b/packages/api-derive/src/elections/voters.ts @@ -6,12 +6,24 @@ import { ApiInterfaceRx } from '@polkadot/api/types'; import { AccountId } from '@polkadot/types/interfaces'; import { DerivedVoterPositions } from '../types'; -import { Observable } from 'rxjs'; +import { Observable, of } from 'rxjs'; import { map } from 'rxjs/operators'; import { createType, Vec } from '@polkadot/types'; import { memo } from '../util'; +function queryElections (api: ApiInterfaceRx): Observable { + return api.derive.elections.voterPositions().pipe( + map((voterPositions: DerivedVoterPositions): Vec => + createType( + 'Vec', + Object.entries(voterPositions) + .sort((a, b): number => a[1].globalIndex.cmp(b[1].globalIndex)) + .map(([accountId]): AccountId => createType('AccountId', accountId)) + ) + ) + ); +} /** * @name voters * @returns An array of all current voters from all sets. @@ -24,16 +36,10 @@ import { memo } from '../util'; * }); * ``` */ -export function voters (api: ApiInterfaceRx): () => Observable> { - return memo((): Observable> => - api.derive.elections.voterPositions().pipe( - map((voterPositions: DerivedVoterPositions): Vec => - createType( - 'Vec', - Object.entries(voterPositions) - .sort((a, b): number => a[1].globalIndex.cmp(b[1].globalIndex)) - .map(([accountId]): AccountId => createType('AccountId', accountId)) - ) - ) - )); +export function voters (api: ApiInterfaceRx): () => Observable { + return memo((): Observable => + api.query.elections + ? queryElections(api) + : of([]) + ); }