Skip to content

Commit

Permalink
fix: vote confirm screen with proxy and multisig (#3053)
Browse files Browse the repository at this point in the history
* fix: vote confirm screen with proxy and multisig

* fix: find coreTx
  • Loading branch information
sokolova-an authored Jan 29, 2025
1 parent 93a6292 commit f81dc81
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 56 deletions.
41 changes: 28 additions & 13 deletions src/renderer/entities/governance/lib/voteTransactionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,29 @@ import { BN, BN_ZERO } from '@polkadot/util';

import { type Conviction, type Transaction, TransactionType } from '@/shared/core';
import { toSerializable } from '@/shared/lib/utils';
import { isProxyTransaction } from '@/entities/transaction';
import {
type RemoveVoteTransaction,
type RevoteTransaction,
type TransactionSplitAbstainVote,
type TransactionStandardVote,
type TransactionVote,
type VoteTransaction,
} from '../types/voteTransaction';

import { votingService } from './votingService';

const getVote = (tx: Transaction): TransactionVote => {
const coreTx = getCoreTx(tx);

return coreTx?.args.vote;
};

const getCoreTx = (tx: Transaction): Transaction => {
if (isProxyTransaction(tx)) {
return tx.args.transaction;
}

return tx;
};

const isStandardVote = (vote: TransactionVote): vote is TransactionStandardVote => {
return 'Standard' in vote;
};
Expand All @@ -21,20 +33,26 @@ const isSplitAbstainVote = (vote: TransactionVote): vote is TransactionSplitAbst
return 'Standard' in vote;
};

const isVoteTransaction = (t: Transaction): t is VoteTransaction => {
return t.type === TransactionType.VOTE;
const isVoteTransaction = (t: Transaction): boolean => {
const coreTx = getCoreTx(t);

return coreTx.type === TransactionType.VOTE;
};

const isRevoteTransaction = (t: Transaction): t is RevoteTransaction => {
return t.type === TransactionType.REVOTE;
const isRevoteTransaction = (t: Transaction): boolean => {
const coreTx = getCoreTx(t);

return coreTx.type === TransactionType.REVOTE;
};

const isRemoveVoteTransaction = (t: Transaction): t is RemoveVoteTransaction => {
const isRemoveVoteTransaction = (t: Transaction): boolean => {
if (t.type === TransactionType.BATCH_ALL) {
return t.args.transactions?.some(isRemoveVoteTransaction);
}

return t.type === TransactionType.REMOVE_VOTE;
const coreTx = getCoreTx(t);

return coreTx.type === TransactionType.REMOVE_VOTE;
};

const createTransactionVote = (
Expand Down Expand Up @@ -65,9 +83,6 @@ const createTransactionVote = (
});
};

const getVoteAmount = (vote: TransactionVote) =>
new BN(isStandardVote(vote) ? vote.Standard.balance : vote.SplitAbstain.abstain);

const getDecision = (vote: TransactionVote): 'aye' | 'nay' | 'abstain' => {
if (isStandardVote(vote)) {
return vote.Standard.vote.vote === 'Aye' || vote.Standard.vote.aye ? 'aye' : 'nay';
Expand All @@ -87,9 +102,9 @@ const getVotes = (vote: TransactionVote): BN => {
};

export const voteTransactionService = {
getVoteAmount,
getDecision,
getVotes,
getVote,

isVoteTransaction,
isRevoteTransaction,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export const Confirmation = ({ id = 0, secondaryActionButton, hideSignButton }:
wallets={wallets}
initiator={[confirm.accounts.initiator]}
signatory={confirm.accounts.signer}
proxied={confirm.accounts.proxied || undefined}
>
{votingPower && amount && conviction ? (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export const Confirmation = ({ id = 0, secondaryActionButton, hideSignButton, on
return null;
}

const { vote } = wrappedTransactions.coreTx.args;
const vote = voteTransactionService.getVote(wrappedTransactions.coreTx);

const decision = voteTransactionService.isStandardVote(vote) ? (vote.Standard.vote.aye ? 'aye' : 'nay') : 'abstain';
const conviction = voteTransactionService.isStandardVote(vote) ? vote.Standard.vote.conviction : 'None';
Expand Down Expand Up @@ -110,7 +110,7 @@ export const Confirmation = ({ id = 0, secondaryActionButton, hideSignButton, on
wallets={wallets}
initiator={[confirm.accounts.initiator]}
signatory={confirm.accounts.signer}
proxied={confirm.accounts.proxy || undefined}
proxied={confirm.accounts.proxied || undefined}
>
<DetailRow label={t('governance.vote.field.decision')}>{t(`governance.referendum.${decision}`)}</DetailRow>
<DetailRow label={t('governance.vote.field.governanceLock')} wrapperClassName="items-start">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import { toAddress } from '@/shared/lib/utils';
import { operationsUtils } from '@/entities/operations';
import { type WrappedTransactions, isProxyTransaction } from '@/entities/transaction';
import { walletUtils } from '@/entities/wallet';
import { accountUtils, walletUtils } from '@/entities/wallet';

export type ConfirmInfo = {
id?: number;
Expand All @@ -27,12 +27,12 @@ export type ConfirmItem<Input extends ConfirmInfo = ConfirmInfo> = {
meta: Input;
wallets: {
initiator: Wallet;
proxy: Wallet | null;
proxied: Wallet | null;
signer: Wallet | null;
};
accounts: {
initiator: Account;
proxy?: ProxiedAccount | null;
proxied?: ProxiedAccount | null;
signer: Account | null;
};
};
Expand Down Expand Up @@ -61,60 +61,42 @@ export const createTransactionConfirmStore = <Input extends ConfirmInfo>({
if (!wallets.length) return {};

return store.reduce<ConfirmMap>((acc, meta, index) => {
const { wrappedTransactions, chain } = meta;
const { wrappedTransactions, chain, account } = meta;
const { wrappedTx, coreTx } = wrappedTransactions;
const { addressPrefix } = chain;

const initiatorAccount = walletUtils.getAccountBy(wallets, (account, wallet) => {
const isSameAccount = coreTx.address === toAddress(account.accountId, { prefix: addressPrefix });

if (isProxyTransaction(wrappedTx)) {
return walletUtils.isProxied(wallet) && isSameAccount;
const isProxyTx = isProxyTransaction(wrappedTx) || isProxyTransaction(coreTx);
const initiatorAccount = walletUtils.getAccountBy(wallets, (acc) => {
if (accountUtils.isProxiedAccount(account)) {
return acc.accountId == account.proxyAccountId;
}

const isSameAccount = coreTx.address === toAddress(acc.accountId, { prefix: addressPrefix });

return isSameAccount;
});

if (!initiatorAccount) return acc;

const initiatorWallet = walletUtils.getWalletFilteredAccounts(wallets, {
walletFn: (wallet) => !isProxyTransaction(wrappedTx) || walletUtils.isProxied(wallet),
accountFn: (account) => initiatorAccount.accountId === account.accountId,
});
const initiatorWallet = walletUtils.getWalletById(wallets, initiatorAccount.walletId);
if (!initiatorWallet) return acc;

const signerAccount = walletUtils.getAccountBy(
wallets,
(account, wallet) =>
walletUtils.isValidSignSignatory(wallet) &&
wrappedTx.address === toAddress(account.accountId, { prefix: addressPrefix }),
);
const signerWallet = walletUtils.getWalletFilteredAccounts(wallets, {
walletFn: walletUtils.isValidSignSignatory,
accountFn: (account) => signerAccount?.accountId === account.accountId,
});
const signerWallet = meta.signatory && walletUtils.getWalletById(wallets, meta.signatory?.walletId);

const proxyAccount = walletUtils.getAccountBy(
wallets,
(account, wallet) =>
!walletUtils.isProxied(wallet) &&
isProxyTransaction(wrappedTx) &&
wrappedTx.address === toAddress(account.accountId, { prefix: addressPrefix }),
) as ProxiedAccount | null;
const proxyWallet = walletUtils.getWalletFilteredAccounts(wallets, {
accountFn: (account) => proxyAccount?.accountId === account.accountId,
});
const proxiedAccount = isProxyTx && accountUtils.isProxiedAccount(account) ? account : null;
const proxiedWallet = proxiedAccount && walletUtils.getWalletById(wallets, proxiedAccount.walletId);

acc[meta.id ?? index] = {
meta,
wallets: {
signer: signerWallet || null,
initiator: initiatorWallet,
proxy: proxyWallet || null,
proxied: proxiedWallet || null,
},
accounts: {
signer: signerAccount,
signer: meta.signatory,
initiator: initiatorAccount,
proxy: proxyAccount,
proxied: proxiedAccount,
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,22 @@ const selectSignatory = createEvent<Account>();
const $signatory = createStore<Account | null>(null);

const $signatories = combine($accounts, walletModel.$wallets, (accounts, wallets) => {
if (!accounts[0] || !accountUtils.isMultisigAccount(accounts[0])) {
const account = accounts.at(0);
if (nullable(account)) return [];

const multisigAcc = accountUtils.isProxiedAccount(account)
? walletUtils.getAccountBy(wallets, (a) => a.accountId === account.proxyAccountId)
: account;

if (nullable(multisigAcc) || !accountUtils.isMultisigAccount(multisigAcc)) {
return [];
}

const a = accounts[0].signatories.map((signatory) =>
const acc = multisigAcc.signatories.map((signatory) =>
walletUtils.getAccountBy(wallets, (a) => a.accountId === signatory.accountId),
);

return a.filter((option) => option !== null);
return acc.filter((option) => option !== null);
});

const $votesList = combine($accounts, flow.state, (accounts, { votes, chain }) => {
Expand Down Expand Up @@ -241,7 +248,7 @@ sample({

return {
signingPayloads: Object.values(confirms).map(({ meta, accounts }) => ({
account: accounts.proxy || accounts.initiator,
account: accounts.initiator,
chain: meta.chain,
transaction: meta.wrappedTransactions.wrappedTx,
signatory: accounts.signer,
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/widgets/VoteModal/aggregates/voteModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ sample({

return {
signingPayloads: Object.values(confirms).map(({ meta, accounts }) => ({
account: accounts.proxy || accounts.initiator,
account: accounts.initiator,
chain: meta.chain,
transaction: meta.wrappedTransactions.wrappedTx,
signatory: accounts.signer,
Expand Down

0 comments on commit f81dc81

Please sign in to comment.