diff --git a/packages/api-derive/src/democracy/proposals.ts b/packages/api-derive/src/democracy/proposals.ts index 945c0d5e456f..b6f93b7dd263 100644 --- a/packages/api-derive/src/democracy/proposals.ts +++ b/packages/api-derive/src/democracy/proposals.ts @@ -10,27 +10,41 @@ import { Observable, combineLatest, of } from 'rxjs'; import { map, switchMap } from 'rxjs/operators'; import { ApiInterfaceRx } from '@polkadot/api/types'; import { Option, Vec } from '@polkadot/types'; +import { isFunction } from '@polkadot/util'; import { memo } from '../util'; -type Depositors = Option]>>; +type DepositorsNew = Option, Balance]>>; +type DepositorsOld = Option]>>; +type Depositors = DepositorsNew | DepositorsOld; type Proposals = Vec>; +type Result = [Proposals, (DeriveProposalImage | undefined)[], Depositors[]]; -function parse ([proposals, images, depositors]: [Proposals, (DeriveProposalImage | undefined)[], Depositors[]]): DeriveProposal[] { +// eslint-disable-next-line @typescript-eslint/no-unused-vars +function isNewDepositors (depositors: ITuple<[Vec, Balance]> | ITuple<[Balance, Vec]>): depositors is ITuple<[Vec, Balance]> { + // Detect balance... + // eslint-disable-next-line @typescript-eslint/unbound-method + return isFunction((depositors[1] as Balance).mul); +} + +function parse ([proposals, images, optDepositors]: Result): DeriveProposal[] { return proposals .filter(([, , proposer], index): boolean => - !!(depositors[index]?.isSome) && !proposer.isEmpty + !!(optDepositors[index]?.isSome) && !proposer.isEmpty ) .map(([index, imageHash, proposer], proposalIndex): DeriveProposal => { - const [balance, seconds] = depositors[proposalIndex].unwrap(); + const depositors = optDepositors[proposalIndex].unwrap(); return { - balance, + ...( + isNewDepositors(depositors) + ? { balance: depositors[1], seconds: depositors[0] } + : { balance: depositors[0], seconds: depositors[1] } + ), image: images[proposalIndex], imageHash, index, - proposer, - seconds + proposer }; }); }