From ea6bb83c0179d26a35e66cae30d766b82ebc776c Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Fri, 24 Apr 2020 10:41:30 +0200 Subject: [PATCH] Voting delegations (#2205) --- .../src/democracy/referendumsInfo.ts | 37 +++++++++++++++---- packages/api-derive/src/democracy/types.ts | 1 + 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/packages/api-derive/src/democracy/referendumsInfo.ts b/packages/api-derive/src/democracy/referendumsInfo.ts index 0d1c6c6435..d089d192c1 100644 --- a/packages/api-derive/src/democracy/referendumsInfo.ts +++ b/packages/api-derive/src/democracy/referendumsInfo.ts @@ -3,7 +3,7 @@ // of the Apache-2.0 license. See the LICENSE file for details. import { ApiInterfaceRx } from '@polkadot/api/types'; -import { AccountId, ReferendumInfo, ReferendumInfoTo239, Vote, Voting, VotingDirectVote } from '@polkadot/types/interfaces'; +import { AccountId, ReferendumInfo, ReferendumInfoTo239, Vote, Voting, VotingDirectVote, VotingDelegating } from '@polkadot/types/interfaces'; import { DeriveBalancesAccount, DeriveReferendum, DeriveReferendumVote, DeriveReferendumVotes } from '../types'; import BN from 'bn.js'; @@ -33,6 +33,7 @@ function votesPrev (api: ApiInterfaceRx, referendumId: BN): Observable ({ accountId, balance: balances[index].votingBalance || api.registry.createType('Balance'), + isDelegating: false, vote: votes[index] || api.registry.createType('Vote') })) ) @@ -41,10 +42,9 @@ function votesPrev (api: ApiInterfaceRx, referendumId: BN): Observable { return api.query.democracy.votingOf.entries().pipe( - map((allVoting): DeriveReferendumVote[] => - allVoting - .map(([key, voting]): [AccountId, Voting] => [key.args[0] as AccountId, voting]) - // FIXME We are ignoring delegated votes + map((allVoting): DeriveReferendumVote[] => { + const mapped = allVoting.map(([key, voting]): [AccountId, Voting] => [key.args[0] as AccountId, voting]); + const votes = mapped .filter(([, voting]) => voting.isDirect) .map(([accountId, voting]): [AccountId, VotingDirectVote[]] => [ accountId, @@ -57,14 +57,37 @@ function votesCurr (api: ApiInterfaceRx, referendumId: BN): Observable voting.isDelegating) + .map(([accountId, voting]): [AccountId, VotingDelegating] => [accountId, voting.asDelegating]); + + // add delegations + delegations.forEach(([accountId, { balance, conviction, target }]): void => { + // Are we delegating to a delegator + const toDelegator = delegations.find(([accountId]) => accountId.eq(target)); + const to = votes.find(({ accountId }) => accountId.eq(toDelegator ? toDelegator[0] : target)); + + // this delegation has a target + if (to) { + votes.push({ + accountId, + balance, + isDelegating: true, + vote: api.registry.createType('Vote', { aye: to.vote.isAye, conviction }) + }); + } + }); + + return votes; + }) ); } diff --git a/packages/api-derive/src/democracy/types.ts b/packages/api-derive/src/democracy/types.ts index bf38e3c213..69c518430b 100644 --- a/packages/api-derive/src/democracy/types.ts +++ b/packages/api-derive/src/democracy/types.ts @@ -46,6 +46,7 @@ export interface DeriveReferendum { export interface DeriveReferendumVote { accountId: AccountId; balance: Balance; + isDelegating: boolean; vote: Vote; }