diff --git a/packages/api-derive/src/council/votes.ts b/packages/api-derive/src/council/votes.ts index d4467a1b9c..009842856b 100644 --- a/packages/api-derive/src/council/votes.ts +++ b/packages/api-derive/src/council/votes.ts @@ -3,7 +3,7 @@ import type { ApiInterfaceRx } from '@polkadot/api/types'; import type { Vec } from '@polkadot/types'; -import type { AccountId, Balance } from '@polkadot/types/interfaces'; +import type { AccountId, Balance, Voter } from '@polkadot/types/interfaces'; import type { ITuple } from '@polkadot/types/types'; import type { Observable } from '@polkadot/x-rxjs'; import type { DeriveCouncilVote, DeriveCouncilVotes } from '../types'; @@ -13,6 +13,10 @@ import { map } from '@polkadot/x-rxjs/operators'; import { memo } from '../util'; +function isVoter (value: ITuple<[Balance, Vec]> | Voter): value is Voter { + return !Array.isArray(value); +} + function retrieveStakeOf (api: ApiInterfaceRx): Observable<[AccountId, Balance][]> { return (api.query.electionsPhragmen || api.query.elections).stakeOf.entries().pipe( map((entries) => @@ -56,11 +60,14 @@ function retrievePrev (api: ApiInterfaceRx): Observable { function retrieveCurrent (api: ApiInterfaceRx): Observable { const elections = (api.query.electionsPhragmen || api.query.elections); - return elections.voting.entries]>>().pipe( + return elections.voting.entries]> | Voter>().pipe( map((entries): DeriveCouncilVotes => - entries.map(([key, [stake, votes]]): [AccountId, DeriveCouncilVote] => - [key.args[0] as AccountId, { stake, votes }] - ) + entries.map(([key, value]): [AccountId, DeriveCouncilVote] => [ + key.args[0] as AccountId, + isVoter(value) + ? { stake: value.stake, votes: value.votes } + : { stake: value[0], votes: value[1] } + ]) ) ); } diff --git a/packages/api-derive/src/elections/info.ts b/packages/api-derive/src/elections/info.ts index 0730865e75..c51f97def6 100644 --- a/packages/api-derive/src/elections/info.ts +++ b/packages/api-derive/src/elections/info.ts @@ -3,7 +3,7 @@ import type { ApiInterfaceRx } from '@polkadot/api/types'; import type { u32, Vec } from '@polkadot/types'; -import type { AccountId, Balance, BlockNumber } from '@polkadot/types/interfaces'; +import type { AccountId, Balance, BlockNumber, SeatHolder } from '@polkadot/types/interfaces'; import type { ITuple } from '@polkadot/types/types'; import type { Observable } from '@polkadot/x-rxjs'; import type { DeriveElectionsInfo } from './types'; @@ -12,7 +12,17 @@ import { map } from '@polkadot/x-rxjs/operators'; import { memo } from '../util'; -function sortAccounts ([, balanceA]: ITuple<[AccountId, Balance]>, [, balanceB]: ITuple<[AccountId, Balance]>): number { +function isSeatHolder (value: ITuple<[AccountId, Balance]> | SeatHolder): value is SeatHolder { + return !Array.isArray(value); +} + +function getAccountTuple (value: ITuple<[AccountId, Balance]> | SeatHolder): [AccountId, Balance] { + return isSeatHolder(value) + ? [value.who, value.stake] + : value; +} + +function sortAccounts ([, balanceA]: [AccountId, Balance], [, balanceB]: [AccountId, Balance]): number { return balanceB.cmp(balanceA); } @@ -32,9 +42,9 @@ function queryElections (api: ApiInterfaceRx): Observable { desiredRunnersUp: api.consts[section].desiredRunnersUp as u32, desiredSeats: api.consts[section].desiredMembers as u32, members: members.length - ? members.sort(sortAccounts) + ? members.map(getAccountTuple).sort(sortAccounts) : councilMembers.map((accountId): [AccountId, Balance] => [accountId, api.registry.createType('Balance')]), - runnersUp: runnersUp.sort(sortAccounts), + runnersUp: runnersUp.map(getAccountTuple).sort(sortAccounts), termDuration: api.consts[section].termDuration as BlockNumber, votingBond: api.consts[section].votingBond as Balance }))