Skip to content

Commit

Permalink
Merge branch 'feat/flexible-multisig' into feat/add-existential-deposit
Browse files Browse the repository at this point in the history
  • Loading branch information
sokolova-an committed Nov 21, 2024
2 parents 77c0773 + 560271f commit f4be89f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 63 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 @@ -92,18 +92,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 walletSelectService = {
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 Down Expand Up @@ -34,4 +30,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 walletSelectService = {
getWalletByGroups,
getFirstWallet,
};
45 changes: 11 additions & 34 deletions src/renderer/features/wallet-select/model/wallet-select-model.ts
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, restore, sample } from 'effector';
import { once, previous } from 'patronum';
import { once } from 'patronum';

import { type Account } 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 @@ -23,18 +23,6 @@ const callbacksApi = createApi($callbacks, {

const $filterQuery = restore(queryChanged, '');

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

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

const $filteredWalletGroups = combine(
{
query: $filterQuery,
Expand Down Expand Up @@ -86,32 +74,21 @@ sample({
target: $filterQuery,
});

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 = walletSelectService.getWalletByGroups(wallets);
const select = sample({
clock: walletModel.$wallets,
filter: (wallets) => wallets.every((wallet) => !wallet.isActive),
fn: (wallets) => walletSelectService.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
17 changes: 11 additions & 6 deletions src/renderer/features/wallet-select/service/walletSelectService.ts
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 walletSelectService = {
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 Down Expand Up @@ -34,4 +30,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 walletSelectService = {
getWalletByGroups,
getFirstWallet,
};
9 changes: 0 additions & 9 deletions src/renderer/widgets/CreateWallet/model/flow-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import { transactionService } from '@/entities/transaction';
import { accountUtils, walletModel, walletUtils } from '@/entities/wallet';
import { signModel } from '@/features/operations/OperationSign/model/sign-model';
import { ExtrinsicResult, submitModel, submitUtils } from '@/features/operations/OperationSubmit';
import { proxiesModel } from '@/features/proxies';
import { walletPairingModel } from '@/features/wallets';
import { type AddMultisigStore, type FormSubmitEvent } from '../lib/types';

Expand Down Expand Up @@ -266,14 +265,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, proxiesModel.events.workerStarted],
});

// Submit

sample({
Expand Down

0 comments on commit f4be89f

Please sign in to comment.