From 467b4a01e115502f55264b92efe6c4e9d798e3dc Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 12 May 2020 22:35:29 +0200 Subject: [PATCH 1/2] Cater for old/new style DepositOf tuples --- .../api-derive/src/democracy/proposals.ts | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/packages/api-derive/src/democracy/proposals.ts b/packages/api-derive/src/democracy/proposals.ts index 945c0d5e456f..639193e1b617 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 (api: ApiInterfaceRx, [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 }; }); } @@ -48,7 +62,7 @@ export function proposals (api: ApiInterfaceRx): () => Observable index)) ]) ), - map(parse) + map((result) => parse(api, result)) ) : of([]) ); From 1d07f4376c52fb1bc7bf0530840ca1104e992d4f Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 12 May 2020 22:37:04 +0200 Subject: [PATCH 2/2] Remove unused api on parse --- packages/api-derive/src/democracy/proposals.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/api-derive/src/democracy/proposals.ts b/packages/api-derive/src/democracy/proposals.ts index 639193e1b617..b6f93b7dd263 100644 --- a/packages/api-derive/src/democracy/proposals.ts +++ b/packages/api-derive/src/democracy/proposals.ts @@ -27,7 +27,7 @@ function isNewDepositors (depositors: ITuple<[Vec, Balance]> | ITuple return isFunction((depositors[1] as Balance).mul); } -function parse (api: ApiInterfaceRx, [proposals, images, optDepositors]: Result): DeriveProposal[] { +function parse ([proposals, images, optDepositors]: Result): DeriveProposal[] { return proposals .filter(([, , proposer], index): boolean => !!(optDepositors[index]?.isSome) && !proposer.isEmpty @@ -62,7 +62,7 @@ export function proposals (api: ApiInterfaceRx): () => Observable index)) ]) ), - map((result) => parse(api, result)) + map(parse) ) : of([]) );