Skip to content

Commit

Permalink
add conviction voting vote tx content
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick-1979 committed May 9, 2024
1 parent 445ab51 commit 94c6480
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 0 deletions.
85 changes: 85 additions & 0 deletions packages/snap/src/ui/txContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import { copyable, panel, row, text } from '@metamask/snaps-sdk';
import { ApiPromise } from '@polkadot/api';
import { AnyTuple } from '@polkadot/types/types';
import type { PalletConvictionVotingVoteVoting } from '@polkadot/types/lookup';
import { amountToHuman } from '../util/amountToHuman';
import { Decoded } from '../util/decodeTxMethod';
import { getConviction, getVoteType } from '../util/governance';

export const txContent = (
api: ApiPromise,
Expand Down Expand Up @@ -79,6 +81,89 @@ export const txContent = (
}

return [row('Extra:', text(`**${extra}**`))];
case 'convictionVoting_vote':
const refId = `${args[0]}`;
const vote = args[1] as PalletConvictionVotingVoteVoting;
const type = getVoteType(vote);

if (vote.isStandard) {
const conviction = getConviction(vote.asStandard.vote);

return [
panel([
row('Referendum:', text(`**${refId}**`)),
row('Vote:', text(`**${type}**`)),
row(
'Amount:',
text(
`**${amountToHuman(
vote.asStandard.balance,
decimal,
)}** **${token}**`,
),
),
row('Conviction:', text(`**${conviction}** `)),
]),
];
}

if (vote.isSplit) {
return [
panel([
row('Referendum:', text(`**${refId}**`)),
row('Vote:', text(`**${type}**`)),
row(
'Aye:',
text(
`**${amountToHuman(vote.asSplit.aye, decimal)}** **${token}**`,
),
),
row(
'Nay:',
text(
`**${amountToHuman(vote.asSplit.nay, decimal)}** **${token}**`,
),
),
]),
];
}

if (vote.isSplitAbstain) {
return [
panel([
row('Referendum:', text(`**${refId}**`)),
row('Vote:', text(`**${type}**`)),
row(
'Abstain:',
text(
`**${amountToHuman(
vote.asSplitAbstain.abstain,
decimal,
)}** **${token}**`,
),
),
row(
'Aye:',
text(
`**${amountToHuman(
vote.asSplitAbstain.aye,
decimal,
)}** **${token}**`,
),
),
row(
'Nay:',
text(
`**${amountToHuman(
vote.asSplitAbstain.nay,
decimal,
)}** **${token}**`,
),
),
]),
];
}
break;
case 'noArgsMethods':
return [];

Expand Down
57 changes: 57 additions & 0 deletions packages/snap/src/util/governance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import type { AccountId32 } from '@polkadot/types/interfaces/runtime';

import { BN } from '@polkadot/util';

const AYE_BITS = 0b10000000;
const CON_MASK = 0b01111111;

export type Vote = {
standard?: {
vote: string;
balance: number;
};
delegations?: {
votes: BN;
capital: BN;
};
splitAbstain?: {
abstain: number;
aye: number;
nay: number;
};
delegating?: {
balance: BN;
aye?: boolean;
nay?: boolean;
abstain?: BN;
conviction: number;
target?: AccountId32;
voted?: boolean;
delegations: {
votes: BN;
capital: BN;
};
prior: any;
};
};

export const isAye = (vote: string) => (vote & AYE_BITS) === AYE_BITS;
export const getConviction = (vote: string) => (vote & CON_MASK) === 0 ? 0.1 : (vote & CON_MASK);

export const getVoteType = (vote: Vote | null | undefined) => {
if (vote) {
if (vote.isStandard) {
return isAye(vote.asStandard.vote) ? 'Aye' : 'Nay';
}

if (vote.isSplitAbstain) {
return 'Abstain';
}

if (vote.isSplit) {
return 'Split';
}
}

return undefined;
};

0 comments on commit 94c6480

Please sign in to comment.