Skip to content

Commit

Permalink
fix: correct proxied creation process (#2679)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnthecat authored Nov 19, 2024
1 parent 5d77609 commit 2f47940
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 76 deletions.
92 changes: 20 additions & 72 deletions src/renderer/features/proxies/model/proxies-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { GraphQLClient } from 'graphql-request';
import keyBy from 'lodash/keyBy';
import { once, spread } from 'patronum';

import { storageService } from '@/shared/api/storage';
import {
type Account,
type AccountId,
Expand Down Expand Up @@ -154,66 +153,31 @@ type ProxiedWalletsParams = {
proxiedAccounts: PartialProxiedAccount[];
chains: Record<ChainId, Chain>;
};
type ProxiedWalletsResult = {
wallets: ProxiedWallet[];
accounts: ProxiedAccount[];
};
const createProxiedWalletsFx = createEffect(
async ({ proxiedAccounts, chains }: ProxiedWalletsParams): Promise<ProxiedWalletsResult> => {
const proxiedWallets = proxiedAccounts.map((proxied) => {
const walletName = proxyUtils.getProxiedName(proxied, chains[proxied.chainId].addressPrefix);
const wallet = {
name: walletName,
type: WalletType.PROXIED,
signingType: SigningType.WATCH_ONLY,
} as Wallet;

const isEthereumChain = networkUtils.isEthereumBased(chains[proxied.chainId].options);

const accounts = [
{
...proxied,
name: walletName,
type: AccountType.PROXIED,
chainType: isEthereumChain ? ChainType.ETHEREUM : ChainType.SUBSTRATE,
cryptoType: isEthereumChain ? CryptoType.ETHEREUM : CryptoType.SR25519,
} as ProxiedAccount,
];

return { wallet, accounts };
});

const dbWalletsAndAccounts = await Promise.all(
proxiedWallets.map(async ({ wallet, accounts }) => {
const dbWallet = await storageService.wallets.create({ ...wallet, isActive: false });

if (!dbWallet) return undefined;

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

if (!dbAccounts) return undefined;

return { wallet: dbWallet, accounts: dbAccounts as ProxiedAccount[] };
}),
);

return dbWalletsAndAccounts.reduce(
(acc, proxiedCreatedResult) => {
if (!proxiedCreatedResult) return acc;
const createProxiedWalletsFx = createEffect(async ({ proxiedAccounts, chains }: ProxiedWalletsParams) => {
return proxiedAccounts.map((proxied) => {
const walletName = proxyUtils.getProxiedName(proxied, chains[proxied.chainId].addressPrefix);
const wallet: Omit<NoID<ProxiedWallet>, 'accounts' | 'isActive'> = {
name: walletName,
type: WalletType.PROXIED,
signingType: SigningType.WATCH_ONLY,
};

acc.accounts.push(...proxiedCreatedResult.accounts);
acc.wallets.push(proxiedCreatedResult.wallet as ProxiedWallet);
const isEthereumChain = networkUtils.isEthereumBased(chains[proxied.chainId].options);

return acc;
},
const accounts: Omit<NoID<ProxiedAccount>, 'walletId'>[] = [
{
wallets: [] as ProxiedWallet[],
accounts: [] as ProxiedAccount[],
...proxied,
name: walletName,
type: AccountType.PROXIED,
chainType: isEthereumChain ? ChainType.ETHEREUM : ChainType.SUBSTRATE,
cryptoType: isEthereumChain ? CryptoType.ETHEREUM : CryptoType.SR25519,
},
);
},
);
];

return { wallet, accounts, external: true };
});
});

sample({
clock: workerStarted,
Expand Down Expand Up @@ -312,22 +276,6 @@ sample({

sample({
clock: createProxiedWalletsFx.doneData,
filter: ({ wallets, accounts }) => wallets.length > 0 && accounts.length > 0,
fn: (data) => {
const accountsMap = dictionary(data.accounts, 'walletId');

const newWallets = data.wallets.map((wallet) => {
const account = accountsMap[wallet.id];

return {
wallet,
accounts: account ? [account] : [],
external: false,
};
});

return newWallets;
},
target: series(walletModel.events.proxiedCreated),
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
ProxyVariant,
type Timepoint,
type Transaction,
WalletType,
} from '@/shared/core';
import { nonNullable, toAddress } from '@/shared/lib/utils';
import { type PathType, Paths } from '@/shared/routes';
Expand Down Expand Up @@ -307,17 +308,17 @@ sample({

sample({
clock: combineEvents({
events: [getPureProxyFx.doneData, proxiesModel.output.walletsCreated],
events: [getPureProxyFx.doneData, walletModel.events.walletCreatedDone],
reset: flowStarted,
}),
source: {
addProxyStore: $addProxyStore,
proxyGroups: proxyModel.$proxyGroups,
},
filter: ({ addProxyStore }, [_, wallet]) => Boolean(wallet.wallets[0].id) && Boolean(addProxyStore),
fn: ({ addProxyStore, proxyGroups }, [{ accountId }, wallet]) => {
filter: ({ addProxyStore }, [_, { wallet }]) => wallet.type === WalletType.PROXIED && nonNullable(addProxyStore),
fn: ({ addProxyStore, proxyGroups }, [{ accountId }, { wallet }]) => {
const newProxyGroup: NoID<ProxyGroup> = {
walletId: wallet.wallets[0].id,
walletId: wallet.id,
chainId: addProxyStore!.chain.chainId,
proxiedAccountId: accountId,
totalDeposit: addProxyStore!.proxyDeposit,
Expand Down

0 comments on commit 2f47940

Please sign in to comment.