Skip to content

Commit

Permalink
fix: multisig wallet select should work fine now (#2677)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnthecat authored Nov 19, 2024
1 parent 6b59deb commit 5d77609
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 89 deletions.
13 changes: 9 additions & 4 deletions src/renderer/entities/wallet/model/wallet-model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { combine, createEffect, createEvent, createStore, sample } from 'effector';
import { type UnitValue, combine, createEffect, createEvent, createStore, sample } from 'effector';
import groupBy from 'lodash/groupBy';
import { combineEvents, readonly } from 'patronum';

Expand All @@ -23,6 +23,9 @@ type DbWallet = Omit<Wallet, 'accounts'>;
type CreateParams<T extends Account = Account> = {
wallet: Omit<NoID<Wallet>, 'isActive' | 'accounts'>;
accounts: Omit<NoID<T>, 'walletId'>[];
// external means wallet was created by someone else and discovered later
// TODO this flag is related to multisig creation and should disappear after wallet feature decomposition
external: boolean;
};

const walletStarted = createEvent();
Expand Down Expand Up @@ -85,6 +88,7 @@ const fetchAllWalletsFx = createEffect(async (): Promise<DbWallet[]> => {
type CreateResult = {
wallet: DbWallet;
accounts: Account[];
external: boolean;
};
const walletCreatedFx = createEffect(async ({ wallet, accounts }: CreateParams): Promise<CreateResult | undefined> => {
const dbWallet = await storageService.wallets.create({ ...wallet, isActive: false });
Expand All @@ -96,14 +100,15 @@ const walletCreatedFx = createEffect(async ({ wallet, accounts }: CreateParams):

if (!dbAccounts) return undefined;

return { wallet: dbWallet, accounts: dbAccounts };
return { wallet: dbWallet, accounts: dbAccounts, external: false };
});

const multishardCreatedFx = createEffect(
async ({
wallet,
accounts,
}: CreateParams<BaseAccount | ChainAccount | ShardAccount>): Promise<CreateResult | undefined> => {
external,
}: UnitValue<typeof multishardCreated>): Promise<(CreateResult & { external: boolean }) | undefined> => {
const dbWallet = await storageService.wallets.create({ ...wallet, isActive: false });

if (!dbWallet) return undefined;
Expand Down Expand Up @@ -148,7 +153,7 @@ const multishardCreatedFx = createEffect(
multishardAccounts.push(...dbChainAccounts);
}

return { wallet: dbWallet, accounts: multishardAccounts };
return { wallet: dbWallet, accounts: multishardAccounts, external };
},
);

Expand Down
1 change: 1 addition & 0 deletions src/renderer/features/proxies/model/proxies-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ sample({
return {
wallet,
accounts: account ? [account] : [],
external: false,
};
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ sample({
sample({
clock: walletModel.events.walletCreatedDone,
source: walletModel.$wallets,
filter: (wallets, { wallet }) => {
filter: (wallets, { wallet, external }) => {
const foundWallet = wallets.find((w) => w.id === wallet.id);
if (!foundWallet) return false;

return !walletUtils.isProxied(foundWallet) && !walletUtils.isMultisig(foundWallet);
return !walletUtils.isProxied(foundWallet) && !walletUtils.isMultisig(foundWallet) && !external;
},
fn: (_, { wallet }) => wallet.id,
target: walletModel.events.selectWallet,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ export const ManageMultishard = ({ seedInfo, onBack, onClose, onComplete }: Prop
signingType: SigningType.PARITY_SIGNER,
},
accounts: accountsToSave,
external: false,
});

onComplete();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export const ManageSingleshard = ({ seedInfo, onBack, onClose, onComplete }: Pro
if (!accountId || accountId.length === 0) return;

walletModel.events.singleshardCreated({
external: false,
wallet: {
name: walletName,
type: WalletType.SINGLE_PARITY_SIGNER,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ sample({
return {
wallet,
accounts: [root, ...accounts],
external: false,
};
},
target: walletModel.events.multishardCreated,
Expand Down
1 change: 1 addition & 0 deletions src/renderer/pages/Onboarding/WalletConnect/ManageStep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export const ManageStep = ({ accounts, type, pairingTopic, sessionTopic, onBack,
});

walletModel.events.walletConnectCreated({
external: false,
wallet: {
name: walletName.trim(),
type,
Expand Down
1 change: 1 addition & 0 deletions src/renderer/pages/Onboarding/WatchOnly/WatchOnly.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const WatchOnly = ({ isOpen, onClose, onComplete }: Props) => {
const isEthereum = isEthereumAccountId(accountId);

walletModel.events.watchOnlyCreated({
external: false,
wallet: {
name: walletName,
type: WalletType.WATCH_ONLY,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,11 @@
import { allSettled, fork } from 'effector';

import {
AccountType,
ChainOptions,
ChainType,
ConnectionType,
CryptoType,
ExternalType,
SigningType,
WalletType,
} from '@/shared/core';
import { AccountType, ChainOptions, ConnectionType } from '@/shared/core';
import { multisigService } from '@/entities/multisig';
import { networkModel } from '@/entities/network';
import { walletModel } from '@/entities/wallet';
import { multisigsModel } from '../multisigs-model';

const signatories = [
{
accountId: '0x01',
address: 'F7NZ',
},
{
accountId: '0x02',
address: 'F7Pa',
},
{
accountId: '0x03',
address: 'F7XB',
},
];

describe('features/multisigs/model/multisigs-model', () => {
beforeEach(() => {
jest.restoreAllMocks();
Expand All @@ -42,62 +18,6 @@ describe('features/multisigs/model/multisigs-model', () => {
]);
});

test('should create multisigs', async () => {
const multisigCreation = jest.spyOn(walletModel.events, 'multisigCreated');

const newMultisig = {
wallet: {
name: 'F7Hs',
type: WalletType.MULTISIG,
signingType: SigningType.MULTISIG,
},
accounts: [
{
threshold: 2,
accountId: '0x00',
signatories: signatories.map(({ accountId, address }) => ({
accountId,
address,
})),
name: 'F7Hs',
chainId: '0x01',
cryptoType: CryptoType.SR25519,
chainType: ChainType.SUBSTRATE,
type: AccountType.MULTISIG,
},
],
};
const scope = fork({
values: new Map()
.set(walletModel._test.$allWallets, [
{ id: 1111, accounts: [{ walletId: 1111, accountId: '0x11', type: AccountType.CHAIN, chainId: '0x01' }] },
])
.set(networkModel.$chains, {
'0x01': {
chainId: '0x01',
name: 'Westend',
options: [ChainOptions.MULTISIG],
externalApi: { [ExternalType.PROXY]: [{ url: 'http://mock-url' }] },
},
}),
});

await allSettled(networkModel.$connections, {
scope,
params: {
'0x01': {
id: 1,
chainId: '0x01',
connectionType: ConnectionType.AUTO_BALANCE,
customNodes: [],
},
},
});
allSettled(multisigsModel.events.multisigsDiscoveryStarted, { scope });

expect(multisigCreation).toHaveBeenCalledWith(newMultisig);
});

test('should not create a multisig we already have', async () => {
const multisigCreation = jest.spyOn(walletModel.events, 'multisigCreated');

Expand Down
6 changes: 5 additions & 1 deletion src/renderer/processes/multisigs/model/multisigs-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { multisigUtils } from '../lib/mulitisigs-utils';
type SaveMultisigParams = {
wallet: Omit<NoID<Wallet>, 'isActive' | 'accounts'>;
accounts: Omit<NoID<MultisigAccount>, 'walletId'>[];
external: boolean;
};

const MULTISIG_DISCOVERY_TIMEOUT = 30000;
Expand Down Expand Up @@ -134,7 +135,10 @@ sample({
fn: ({ indexedMultisigs, chain }) => {
return indexedMultisigs.map(
({ threshold, accountId, signatories }) =>
multisigUtils.buildMultisig({ threshold, accountId, signatories, chain }) as SaveMultisigParams,
({
...multisigUtils.buildMultisig({ threshold, accountId, signatories, chain }),
external: true,
}) as SaveMultisigParams,
);
},
target: saveMultisigFx,
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/widgets/CreateWallet/model/flow-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ sample({
signingType: SigningType.MULTISIG,
},
accounts: [account],
external: false,
};
},
target: walletModel.events.multisigCreated,
Expand All @@ -265,7 +266,7 @@ sample({

sample({
clock: walletModel.events.walletCreatedDone,
filter: ({ wallet }) => wallet.type === WalletType.MULTISIG,
filter: ({ wallet, external }) => wallet.type === WalletType.MULTISIG && !external,
fn: ({ wallet }) => wallet.id,
// wallet selection shouldn't be here, but here we are
target: [walletModel.events.selectWallet, walletProviderModel.events.completed],
Expand Down

0 comments on commit 5d77609

Please sign in to comment.