Skip to content

Commit

Permalink
Cater for old/new style DepositOf tuples (#2272)
Browse files Browse the repository at this point in the history
* Cater for old/new style DepositOf tuples

* Remove unused api on parse
  • Loading branch information
jacogr authored May 12, 2020
1 parent fc7c831 commit 6f2d62b
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions packages/api-derive/src/democracy/proposals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ITuple<[Balance, Vec<AccountId>]>>;
type DepositorsNew = Option<ITuple<[Vec<AccountId>, Balance]>>;
type DepositorsOld = Option<ITuple<[Balance, Vec<AccountId>]>>;
type Depositors = DepositorsNew | DepositorsOld;
type Proposals = Vec<ITuple<[PropIndex, Hash, AccountId]>>;
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<AccountId>, Balance]> | ITuple<[Balance, Vec<AccountId>]>): depositors is ITuple<[Vec<AccountId>, 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
};
});
}
Expand Down

0 comments on commit 6f2d62b

Please sign in to comment.