Skip to content

Commit

Permalink
fix: multisig wallet select (#2697)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnthecat authored Nov 20, 2024
1 parent 3f03535 commit 74bdda1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 56 deletions.
18 changes: 10 additions & 8 deletions src/renderer/entities/wallet/model/wallet-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,20 @@ type CreateResult = {
accounts: Account[];
external: boolean;
};
const walletCreatedFx = createEffect(async ({ wallet, accounts }: CreateParams): Promise<CreateResult | undefined> => {
const dbWallet = await storageService.wallets.create({ ...wallet, isActive: false });
const walletCreatedFx = createEffect(
async ({ wallet, accounts, external }: CreateParams): Promise<CreateResult | undefined> => {
const dbWallet = await storageService.wallets.create({ ...wallet, isActive: false });

if (!dbWallet) return undefined;
if (!dbWallet) return undefined;

const accountsPayload = accounts.map((account) => ({ ...account, walletId: dbWallet.id }));
const dbAccounts = await storageService.accounts.createAll(accountsPayload);
const accountsPayload = accounts.map((account) => ({ ...account, walletId: dbWallet.id }));
const dbAccounts = await storageService.accounts.createAll(accountsPayload);

if (!dbAccounts) return undefined;
if (!dbAccounts) return undefined;

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

const multishardCreatedFx = createEffect(
async ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ import { type Wallet, type WalletFamily, WalletType } from '@/shared/core';
import { includes } from '@/shared/lib/utils';
import { walletUtils } from '@/entities/wallet';

export const walletSelectUtils = {
getWalletByGroups,
};

function getWalletByGroups(wallets: Wallet[], query = ''): Record<WalletFamily, Wallet[]> {
const getWalletByGroups = (wallets: Wallet[], query = ''): Record<WalletFamily, Wallet[]> => {
const accumulator: Record<WalletFamily, Wallet[]> = {
[WalletType.POLKADOT_VAULT]: [],
[WalletType.MULTISIG]: [],
Expand All @@ -32,4 +28,13 @@ function getWalletByGroups(wallets: Wallet[], query = ''): Record<WalletFamily,

return acc;
}, accumulator);
}
};

const getFirstWallet = (wallets: Wallet[]) => {
return getWalletByGroups(wallets)[WalletType.POLKADOT_VAULT].at(0) ?? null;
};

export const walletSelectUtils = {
getWalletByGroups,
getFirstWallet,
};
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { default as BigNumber } from 'bignumber.js';
import { attach, combine, createApi, createEvent, createStore, sample } from 'effector';
import { once, previous } from 'patronum';
import { once } from 'patronum';

import { type Account, type ID, type Wallet } from '@/shared/core';
import { dictionary, getRoundedValue, totalAmount } from '@/shared/lib/utils';
import { dictionary, getRoundedValue, nonNullable, totalAmount } from '@/shared/lib/utils';
import { balanceModel } from '@/entities/balance';
import { networkModel } from '@/entities/network';
import { currencyModel, priceProviderModel } from '@/entities/price';
Expand All @@ -25,18 +25,6 @@ const callbacksApi = createApi($callbacks, {
const $walletId = createStore<ID | null>(null);
const $filterQuery = createStore<string>('');

const $isWalletsRemoved = combine(
{
prevWallets: previous(walletModel.$wallets),
wallets: walletModel.$wallets,
},
({ prevWallets, wallets }) => {
if (!prevWallets) return false;

return prevWallets.length > wallets.length;
},
);

const $walletForDetails = combine(
{
walletId: $walletId,
Expand Down Expand Up @@ -100,32 +88,21 @@ sample({ clock: queryChanged, target: $filterQuery });

sample({ clock: walletIdSet, target: $walletId });

sample({
clock: $isWalletsRemoved,
source: walletModel.$wallets,
filter: (wallets, isWalletsRemoved) => {
if (!isWalletsRemoved || wallets.length === 0) return false;

return wallets.every((wallet) => !wallet.isActive);
},
fn: (wallets) => {
const groups = walletSelectUtils.getWalletByGroups(wallets);
const select = sample({
clock: walletModel.$wallets,
filter: (wallets) => wallets.every((wallet) => !wallet.isActive),
fn: (wallets) => walletSelectUtils.getFirstWallet(wallets)?.id ?? null,
});

return Object.values(groups).flat()[0].id;
},
sample({
clock: select.filter({ fn: nonNullable }),
target: walletModel.events.selectWallet,
});

sample({
clock: walletModel.events.walletCreatedDone,
source: walletModel.$wallets,
filter: (wallets, { wallet, external }) => {
const foundWallet = wallets.find((w) => w.id === wallet.id);
if (!foundWallet) return false;

return !walletUtils.isProxied(foundWallet) && !walletUtils.isMultisig(foundWallet) && !external;
},
fn: (_, { wallet }) => wallet.id,
filter: ({ external }) => !external,
fn: ({ wallet }) => wallet.id,
target: walletModel.events.selectWallet,
});

Expand Down
8 changes: 0 additions & 8 deletions src/renderer/widgets/CreateWallet/model/flow-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,6 @@ sample({
target: $error,
});

sample({
clock: walletModel.events.walletCreatedDone,
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],
});

// Submit

sample({
Expand Down

0 comments on commit 74bdda1

Please sign in to comment.