Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: vote confirm screen with proxy and multisig #3053

Merged
merged 3 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions src/renderer/entities/governance/lib/voteTransactionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ import { BN, BN_ZERO } from '@polkadot/util';
import { type Conviction, type Transaction, TransactionType } from '@/shared/core';
import { toSerializable } from '@/shared/lib/utils';
import {
type RemoveVoteTransaction,
type RevoteTransaction,
type TransactionSplitAbstainVote,
type TransactionStandardVote,
type TransactionVote,
type VoteTransaction,
} from '../types/voteTransaction';

import { votingService } from './votingService';
Expand All @@ -21,20 +18,20 @@ 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 => {
return t.type === TransactionType.VOTE || (t.args?.transaction && isVoteTransaction(t.args?.transaction));
};

const isRevoteTransaction = (t: Transaction): t is RevoteTransaction => {
return t.type === TransactionType.REVOTE;
const isRevoteTransaction = (t: Transaction): boolean => {
return t.type === TransactionType.REVOTE || (t.args?.transaction && isRevoteTransaction(t.args.transaction));
};

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;
return t.type === TransactionType.REMOVE_VOTE || (t.args?.transaction && isRemoveVoteTransaction(t.args.transaction));
};

const createTransactionVote = (
Expand Down Expand Up @@ -65,9 +62,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,7 +81,6 @@ const getVotes = (vote: TransactionVote): BN => {
};

export const voteTransactionService = {
getVoteAmount,
getDecision,
getVotes,

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 = wrappedTransactions.coreTx.args.vote || wrappedTransactions.coreTx.args?.transaction?.args.vote;

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
Loading