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

feat: update types and checks for multisigs #2693

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ type TxWrappersParams = {
* @returns {Array}
*/
function getTxWrappers({ wallet, ...params }: TxWrappersParams): TxWrapper[] {
if (walletUtils.isMultisig(wallet)) {
if (walletUtils.isRegularMultisig(wallet)) {
return getMultisigWrapper(params);
}

Expand Down
16 changes: 14 additions & 2 deletions src/renderer/entities/wallet/lib/__tests__/wallet-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,25 @@ describe('entities/wallet/lib/wallet-utils', () => {
test('isMultisig should return true when wallet type is Multisig', () => {
const wallet = { type: WalletType.MULTISIG } as Wallet;

expect(walletUtils.isRegularMultisig(wallet)).toEqual(true);
});

test('isFlexibleMultisig should return true when wallet type is Flexible Multisig', () => {
const wallet = { type: WalletType.FLEXIBLE_MULTISIG } as Wallet;

expect(walletUtils.isFlexibleMultisig(wallet)).toEqual(true);
});

test('isMultisig should return true when wallet type is Flexible Multisig', () => {
const wallet = { type: WalletType.FLEXIBLE_MULTISIG } as Wallet;

expect(walletUtils.isMultisig(wallet)).toEqual(true);
});

test('isMultisig should return false when wallet type is not Multisig', () => {
const wallet = { type: WalletType.NOVA_WALLET } as Wallet;

expect(walletUtils.isMultisig(wallet)).toEqual(false);
expect(walletUtils.isRegularMultisig(wallet)).toEqual(false);
});

test('isNovaWallet should return true when wallet type is NovaWallet', () => {
Expand All @@ -47,7 +59,7 @@ describe('entities/wallet/lib/wallet-utils', () => {
test('isNovaWallet should return false when wallet type is not NovaWallet', () => {
const wallet = { type: WalletType.POLKADOT_VAULT } as Wallet;

expect(walletUtils.isMultisig(wallet)).toEqual(false);
expect(walletUtils.isRegularMultisig(wallet)).toEqual(false);
});

test('isProxied should return true when wallet type is Proxied', () => {
Expand Down
9 changes: 7 additions & 2 deletions src/renderer/entities/wallet/lib/account-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ import { walletUtils } from './wallet-utils';
export const accountUtils = {
isBaseAccount,
isChainAccount,
isMultisigAccount,
isRegularMultisigAccount,
isFlexibleMultisigAccount,
isMultisigAccount,
isWcAccount,
isProxiedAccount,
isPureProxiedAccount,
Expand Down Expand Up @@ -74,14 +75,18 @@ function isShardAccount(account: Partial<Account>): account is ShardAccount {
return account.type === AccountType.SHARD;
}

function isMultisigAccount(account: Partial<Account>): account is MultisigAccount {
function isRegularMultisigAccount(account: Partial<Account>): account is MultisigAccount {
return account.type === AccountType.MULTISIG;
}

function isFlexibleMultisigAccount(account: Partial<Account>): account is FlexibleMultisigAccount {
return account.type === AccountType.FLEXIBLE_MULTISIG;
}

function isMultisigAccount(account: Partial<Account>): account is MultisigAccount | FlexibleMultisigAccount {
return isFlexibleMultisigAccount(account) || isRegularMultisigAccount(account);
}

function isProxiedAccount(account: Partial<Account>): account is ProxiedAccount {
return account.type === AccountType.PROXIED;
}
Expand Down
3 changes: 0 additions & 3 deletions src/renderer/entities/wallet/lib/permission-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ function canStake(wallet: Wallet): boolean {
function canCreateMultisigTx(wallet: Wallet): boolean {
if (walletUtils.isWatchOnly(wallet)) return false;
if (walletUtils.isMultisig(wallet)) return false;
if (walletUtils.isFlexibleMultisig(wallet)) return false;
if (walletUtils.isProxied(wallet)) {
const isAnyProxy = accountUtils.isAnyProxyType(wallet.accounts[0]);
const isNonTransfer = accountUtils.isNonTransferProxyType(wallet.accounts[0]);
Expand All @@ -57,7 +56,6 @@ function canCreateMultisigTx(wallet: Wallet): boolean {
function canApproveMultisigTx(wallet: Wallet): boolean {
if (walletUtils.isWatchOnly(wallet)) return false;
if (walletUtils.isMultisig(wallet)) return false;
if (walletUtils.isFlexibleMultisig(wallet)) return false;

if (walletUtils.isProxied(wallet)) {
return false;
Expand All @@ -74,7 +72,6 @@ function canApproveMultisigTx(wallet: Wallet): boolean {
function canRejectMultisigTx(wallet: Wallet): boolean {
if (walletUtils.isWatchOnly(wallet)) return false;
if (walletUtils.isMultisig(wallet)) return false;
if (walletUtils.isFlexibleMultisig(wallet)) return false;
if (walletUtils.isProxied(wallet)) {
return false;

Expand Down
11 changes: 8 additions & 3 deletions src/renderer/entities/wallet/lib/wallet-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const walletUtils = {
isSingleShard,
isMultisig,
isFlexibleMultisig,
isRegularMultisig,
isWatchOnly,
isNovaWallet,
isWalletConnect,
Expand Down Expand Up @@ -54,12 +55,16 @@ function isSingleShard(wallet?: Wallet): wallet is SingleShardWallet {
return wallet?.type === WalletType.SINGLE_PARITY_SIGNER;
}

function isMultisig(wallet?: Wallet): wallet is MultisigWallet {
function isFlexibleMultisig(wallet?: Wallet): wallet is FlexibleMultisigWallet {
return wallet?.type === WalletType.FLEXIBLE_MULTISIG;
}

function isRegularMultisig(wallet?: Wallet): wallet is FlexibleMultisigWallet {
return wallet?.type === WalletType.MULTISIG;
}

function isFlexibleMultisig(wallet?: Wallet): wallet is FlexibleMultisigWallet {
return wallet?.type === WalletType.FLEXIBLE_MULTISIG;
function isMultisig(wallet?: Wallet): wallet is MultisigWallet | FlexibleMultisigWallet {
return isFlexibleMultisig(wallet) || isRegularMultisig(wallet);
}

function isWatchOnly(wallet?: Wallet): wallet is WatchOnlyWallet {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
nonNullable,
toAccountId,
toAddress,
transferableAmount,
withdrawableAmountBN,
} from '@/shared/lib/utils';
import { createDepositCalculator, createFeeCalculator } from '@/shared/transactions';
import { balanceModel, balanceUtils } from '@/entities/balance';
Expand Down Expand Up @@ -218,7 +218,7 @@ const $isEnoughBalance = combine(
return fee
.add(multisigDeposit)
.add(new BN(proxyDeposit))
.lte(new BN(transferableAmount(balance)));
.lte(new BN(withdrawableAmountBN(balance)));
},
);

Expand Down Expand Up @@ -413,8 +413,6 @@ sample({
name: name.trim(),
accountId: multisigAccoutId!,
threshold: threshold,
// TODO get proxy account
proxyAccountId: multisigAccoutId!,
cryptoType: isEthereumChain ? CryptoType.ETHEREUM : CryptoType.SR25519,
chainType: isEthereumChain ? ChainType.ETHEREUM : ChainType.SUBSTRATE,
type: AccountType.FLEXIBLE_MULTISIG,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export const Confirmation = ({

<hr className="w-full border-filter-border pr-2" />

{accountUtils.isMultisigAccount(confirmStore.shards[0]) && (
{confirmStore.shards?.[0] && accountUtils.isMultisigAccount(confirmStore.shards[0]) && (
<DetailRow
className="text-text-primary"
label={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export const Confirmation = ({

<hr className="w-full border-filter-border pr-2" />

{accountUtils.isMultisigAccount(confirmStore.shards[0]) && (
{confirmStore.shards?.[0] && accountUtils.isMultisigAccount(confirmStore.shards[0]) && (
<DetailRow
className="text-text-primary"
label={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export const Confirmation = ({ id = 0, secondaryActionButton, hideSignButton, on

<hr className="w-full border-filter-border pr-2" />

{accountUtils.isMultisigAccount(confirmStore.shards[0]) && (
{confirmStore.shards?.[0] && accountUtils.isMultisigAccount(confirmStore.shards[0]) && (
<DetailRow
className="text-text-primary"
label={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export const Confirmation = ({ id = 0, onGoBack, secondaryActionButton, hideSign

<hr className="w-full border-filter-border pr-2" />

{accountUtils.isMultisigAccount(confirmStore.shards[0]) && (
{confirmStore.shards?.[0] && accountUtils.isMultisigAccount(confirmStore.shards[0]) && (
<DetailRow
className="text-text-primary"
label={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export const Confirmation = ({ id = 0, onGoBack, secondaryActionButton, hideSign
signatory={confirmStore.signatory}
proxied={confirmStore.proxiedAccount}
>
{accountUtils.isMultisigAccount(confirmStore.shards[0]) && (
{confirmStore.shards?.[0] && accountUtils.isMultisigAccount(confirmStore.shards[0]) && (
<DetailRow
className="text-text-primary"
label={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export const Confirmation = ({ id = 0, secondaryActionButton, hideSignButton, on
signatory={confirmStore.signatory}
proxied={confirmStore.proxiedAccount}
>
{accountUtils.isMultisigAccount(confirmStore.shards[0]) && (
{confirmStore.shards?.[0] && accountUtils.isMultisigAccount(confirmStore.shards[0]) && (
<DetailRow
className="text-text-primary"
label={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export const Confirmation = ({ id = 0, secondaryActionButton, hideSignButton, on
signatory={confirmStore.signatory}
proxied={confirmStore.proxiedAccount}
>
{accountUtils.isMultisigAccount(confirmStore.shards[0]) && (
{confirmStore.shards?.[0] && accountUtils.isMultisigAccount(confirmStore.shards[0]) && (
<DetailRow
className="text-text-primary"
label={
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/features/proxy-add-pure/model/form-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,13 +474,13 @@ sample({
filter: (_, account) => Boolean(account),
fn: ({ wallet, wallets }, account): Record<string, boolean> => {
if (!wallet) return { isMultisig: false, isProxy: false };
if (walletUtils.isMultisig(wallet)) return { isMultisig: true, isProxy: false };
if (walletUtils.isRegularMultisig(wallet)) return { isMultisig: true, isProxy: false };
if (!walletUtils.isProxied(wallet)) return { isMultisig: false, isProxy: false };

const accountWallet = walletUtils.getWalletById(wallets, account!.walletId);

return {
isMultisig: walletUtils.isMultisig(accountWallet),
isMultisig: walletUtils.isRegularMultisig(accountWallet),
isProxy: true,
};
},
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/features/proxy-add/model/form-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,13 +565,13 @@ sample({
filter: (_, account) => Boolean(account),
fn: ({ wallet, wallets }, account): Record<string, boolean> => {
if (!wallet) return { isMultisig: false, isProxy: false };
if (walletUtils.isMultisig(wallet)) return { isMultisig: true, isProxy: false };
if (walletUtils.isRegularMultisig(wallet)) return { isMultisig: true, isProxy: false };
if (!walletUtils.isProxied(wallet)) return { isMultisig: false, isProxy: false };

const accountWallet = walletUtils.getWalletById(wallets, account!.walletId);

return {
isMultisig: walletUtils.isMultisig(accountWallet),
isMultisig: walletUtils.isRegularMultisig(accountWallet),
isProxy: true,
};
},
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/features/proxy-remove-pure/model/form-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,13 +271,13 @@ sample({
filter: (_, account) => Boolean(account),
fn: ({ wallet, wallets }, account): Record<string, boolean> => {
if (!wallet) return { isMultisig: false, isProxy: false };
if (walletUtils.isMultisig(wallet)) return { isMultisig: true, isProxy: false };
if (walletUtils.isRegularMultisig(wallet)) return { isMultisig: true, isProxy: false };
if (!walletUtils.isProxied(wallet)) return { isMultisig: false, isProxy: false };

const accountWallet = walletUtils.getWalletById(wallets, account!.walletId);

return {
isMultisig: walletUtils.isMultisig(accountWallet),
isMultisig: walletUtils.isRegularMultisig(accountWallet),
isProxy: true,
};
},
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/features/proxy-remove/model/form-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,13 +271,13 @@ sample({
filter: (_, account) => Boolean(account),
fn: ({ wallet, wallets }, account): Record<string, boolean> => {
if (!wallet) return { isMultisig: false, isProxy: false };
if (walletUtils.isMultisig(wallet)) return { isMultisig: true, isProxy: false };
if (walletUtils.isRegularMultisig(wallet)) return { isMultisig: true, isProxy: false };
if (!walletUtils.isProxied(wallet)) return { isMultisig: false, isProxy: false };

const accountWallet = walletUtils.getWalletById(wallets, account!.walletId);

return {
isMultisig: walletUtils.isMultisig(accountWallet),
isMultisig: walletUtils.isRegularMultisig(accountWallet),
isProxy: true,
};
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const WalletDetails = ({ isOpen, wallet, onClose }: Props) => {
return <MultishardWalletDetails wallet={wallet} accounts={multiShardAccounts} onClose={onClose} />;
}

// TODO: Separate wallet details for regular and flexible multisig
if (walletUtils.isMultisig(wallet)) {
return (
<MultisigWalletDetails
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import { useUnit } from 'effector-react';
import { useMemo } from 'react';
import { Trans } from 'react-i18next';

import { type AccountId, type Contact, type MultisigWallet, type Wallet } from '@/shared/core';
import {
type AccountId,
type Contact,
type FlexibleMultisigWallet,
type MultisigWallet,
type Wallet,
} from '@/shared/core';
import { useI18n } from '@/shared/i18n';
import { useModalClose, useToggle } from '@/shared/lib/hooks';
import { toAddress } from '@/shared/lib/utils';
Expand Down Expand Up @@ -38,7 +44,7 @@ const {
} = proxyAddPureFeature;

type Props = {
wallet: MultisigWallet;
wallet: MultisigWallet | FlexibleMultisigWallet;
signatoryWallets: [Wallet, AccountId][];
signatoryContacts: Contact[];
signatoryPeople: AccountId[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function getWalletByGroups(wallets: Wallet[], query = ''): Record<WalletFamily,
let groupIndex: WalletFamily | undefined;

if (walletUtils.isPolkadotVaultGroup(wallet)) groupIndex = WalletType.POLKADOT_VAULT;
if (walletUtils.isMultisig(wallet)) groupIndex = WalletType.MULTISIG;
if (walletUtils.isRegularMultisig(wallet)) groupIndex = WalletType.MULTISIG;
if (walletUtils.isFlexibleMultisig(wallet)) groupIndex = WalletType.FLEXIBLE_MULTISIG;
if (walletUtils.isWatchOnly(wallet)) groupIndex = WalletType.WATCH_ONLY;
if (walletUtils.isWalletConnect(wallet)) groupIndex = WalletType.WALLET_CONNECT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function getWalletByGroups(wallets: Wallet[], query = ''): Record<WalletFamily,
let groupIndex: WalletFamily | undefined;

if (walletUtils.isPolkadotVaultGroup(wallet)) groupIndex = WalletType.POLKADOT_VAULT;
if (walletUtils.isMultisig(wallet)) groupIndex = WalletType.MULTISIG;
if (walletUtils.isRegularMultisig(wallet)) groupIndex = WalletType.MULTISIG;
if (walletUtils.isFlexibleMultisig(wallet)) groupIndex = WalletType.FLEXIBLE_MULTISIG;
if (walletUtils.isWatchOnly(wallet)) groupIndex = WalletType.WATCH_ONLY;
if (walletUtils.isWalletConnect(wallet)) groupIndex = WalletType.WALLET_CONNECT;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { useUnit } from 'effector-react';
import { useEffect, useState } from 'react';

import { type Account, type MultisigAccount, type MultisigTransaction, type Transaction } from '@/shared/core';
import {
type Account,
type FlexibleMultisigAccount,
type FlexibleMultisigTransaction,
type MultisigAccount,
type MultisigTransaction,
type Transaction,
} from '@/shared/core';
import { useI18n } from '@/shared/i18n';
import { getAssetById } from '@/shared/lib/utils';
import { DetailRow, Icon } from '@/shared/ui';
Expand All @@ -23,8 +30,8 @@ import { TransactionAmount } from '@/pages/Operations/components/TransactionAmou
import { Details } from '../Details';

type Props = {
tx: MultisigTransaction;
account: MultisigAccount;
tx: MultisigTransaction | FlexibleMultisigTransaction;
account: MultisigAccount | FlexibleMultisigAccount;
signAccount?: Account;
chainConnection: ExtendedChain;
feeTx?: Transaction;
Expand Down
6 changes: 4 additions & 2 deletions src/renderer/pages/Operations/components/Details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { Trans } from 'react-i18next';
import {
type Account,
type Address,
type FlexibleMultisigAccount,
type FlexibleMultisigTransaction,
type MultisigAccount,
type MultisigTransaction,
type Transaction,
Expand Down Expand Up @@ -52,8 +54,8 @@ import {
} from '../common/utils';

type Props = {
tx: MultisigTransaction;
account?: MultisigAccount;
tx: MultisigTransaction | FlexibleMultisigTransaction;
account?: MultisigAccount | FlexibleMultisigAccount;
signatory?: Account;
extendedChain?: ExtendedChain;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { type MultisigAccount } from '@/shared/core';
import { type FlexibleMultisigAccount, type MultisigAccount } from '@/shared/core';
import { useI18n } from '@/shared/i18n';
import { BodyText, Icon } from '@/shared/ui';

type Props = {
multisigAccount?: MultisigAccount;
multisigAccount?: MultisigAccount | FlexibleMultisigAccount;
isEmptyFromFilters: boolean;
};

Expand Down
Loading
Loading