diff --git a/src/renderer/features/proxies/model/proxies-model.ts b/src/renderer/features/proxies/model/proxies-model.ts index 2fe74cbe1..f81315e78 100644 --- a/src/renderer/features/proxies/model/proxies-model.ts +++ b/src/renderer/features/proxies/model/proxies-model.ts @@ -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, @@ -154,66 +153,31 @@ type ProxiedWalletsParams = { proxiedAccounts: PartialProxiedAccount[]; chains: Record; }; -type ProxiedWalletsResult = { - wallets: ProxiedWallet[]; - accounts: ProxiedAccount[]; -}; -const createProxiedWalletsFx = createEffect( - async ({ proxiedAccounts, chains }: ProxiedWalletsParams): Promise => { - 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, '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, '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, @@ -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), }); diff --git a/src/renderer/widgets/AddPureProxiedModal/model/add-pure-proxied-model.ts b/src/renderer/widgets/AddPureProxiedModal/model/add-pure-proxied-model.ts index 5401ba640..d2e3a9c48 100644 --- a/src/renderer/widgets/AddPureProxiedModal/model/add-pure-proxied-model.ts +++ b/src/renderer/widgets/AddPureProxiedModal/model/add-pure-proxied-model.ts @@ -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'; @@ -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 = { - walletId: wallet.wallets[0].id, + walletId: wallet.id, chainId: addProxyStore!.chain.chainId, proxiedAccountId: accountId, totalDeposit: addProxyStore!.proxyDeposit,