Skip to content

Commit

Permalink
Fix: Assets page for multisig (#2676)
Browse files Browse the repository at this point in the history
  • Loading branch information
tuul-wq authored Nov 21, 2024
1 parent f7df940 commit af4b908
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useEffect, useState } from 'react';

import { chainsService } from '@/shared/api/network';
import { type Account, type Chain } from '@/shared/core';
import { isStringsMatchQuery } from '@/shared/lib/utils';
import { isStringsMatchQuery, nullable } from '@/shared/lib/utils';
import { AssetsListView, EmptyAssetsState } from '@/entities/asset';
import { balanceModel } from '@/entities/balance';
import { networkModel, networkUtils } from '@/entities/network';
Expand Down Expand Up @@ -34,26 +34,25 @@ export const AssetsChainView = ({ query, activeShards, hideZeroBalances, assetsV
if (!activeWallet || assetsView !== AssetsListView.CHAIN_CENTRIC || !activeShards.length) return;

const isMultisig = walletUtils.isMultisig(activeWallet);
const multisigChainToInclude = isMultisig ? activeWallet.accounts[0].chainId : undefined;

const availableChains = Object.values(chains).filter((chain) => {
return activeWallet.accounts.some((account) => {
return (
activeWallet &&
accountUtils.isNonBaseVaultAccount(account, activeWallet) &&
accountUtils.isChainAndCryptoMatch(account, chain)
);
});
});

const filteredChains = availableChains.filter((c) => {
if (!connections[c.chainId]) {
return false;
}
const connection = connections[c.chainId];

const isDisabled = networkUtils.isDisabledConnection(connections[c.chainId]);
const hasMultiPallet = !isMultisig || networkUtils.isMultisigSupported(c.options);
if (nullable(connection)) return false;
if (networkUtils.isDisabledConnection(connection)) return false;
if (!isMultisig) return true;

return !isDisabled && hasMultiPallet;
return networkUtils.isMultisigSupported(c.options) || multisigChainToInclude === c.chainId;
});

const sortedChains = chainsService.sortChainsByBalance(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createEffect, createEvent, createStore, restore, sample } from 'effecto
import { once } from 'patronum';

import { type Account, type AssetByChains, type Balance, type Chain, type ChainId, type Wallet } from '@/shared/core';
import { includes } from '@/shared/lib/utils';
import { includes, nullable } from '@/shared/lib/utils';
import { AssetsListView } from '@/entities/asset';
import { balanceModel } from '@/entities/balance';
import { networkModel, networkUtils } from '@/entities/network';
Expand Down Expand Up @@ -34,8 +34,9 @@ type UpdateTokenParams = {

const getUpdatedTokensFx = createEffect(({ activeWallet, chains }: UpdateTokenParams): AssetByChains[] => {
const tokens = tokensService.getTokensData();
const updatedTokens: AssetByChains[] = [];

return tokens.reduce((acc, token) => {
for (const token of tokens) {
const filteredChains = token.chains.filter((chain) => {
return activeWallet?.accounts.some((account) => {
return (
Expand All @@ -45,12 +46,12 @@ const getUpdatedTokensFx = createEffect(({ activeWallet, chains }: UpdateTokenPa
});
});

if (filteredChains.length > 0) {
acc.push({ ...token, chains: filteredChains });
}
if (filteredChains.length === 0) continue;

updatedTokens.push({ ...token, chains: filteredChains });
}

return acc;
}, [] as AssetByChains[]);
return updatedTokens;
});

type PopulateBalanceParams = {
Expand All @@ -61,15 +62,17 @@ type PopulateBalanceParams = {

const populateTokensBalanceFx = createEffect(
({ activeTokens, balances, accounts }: PopulateBalanceParams): AssetByChains[] => {
return activeTokens.reduce<AssetByChains[]>((acc, token) => {
const tokens: AssetByChains[] = [];

for (const token of activeTokens) {
const [chainsWithBalance, totalBalance] = tokensService.getChainWithBalance(balances, token.chains, accounts);

if (chainsWithBalance.length > 0) {
acc.push({ ...token, chains: chainsWithBalance, totalBalance });
}
if (chainsWithBalance.length === 0) continue;

return acc;
}, []);
tokens.push({ ...token, chains: chainsWithBalance, totalBalance });
}

return tokens;
},
);

Expand Down Expand Up @@ -105,22 +108,29 @@ sample({
},
fn: ({ connections, chains, tokens, activeWallet }): AssetByChains[] => {
const isMultisigWallet = walletUtils.isMultisig(activeWallet);
const hasAccounts = activeWallet!.accounts.length > 0;
const multisigChainToInclude = isMultisigWallet && hasAccounts ? activeWallet.accounts[0].chainId : undefined;

const activeTokens: AssetByChains[] = [];

return tokens.reduce<AssetByChains[]>((acc, token) => {
for (const token of tokens) {
const filteredChains = token.chains.filter((c) => {
if (!connections[c.chainId]) return false;
const isDisabled = networkUtils.isDisabledConnection(connections[c.chainId]);
const hasMultiPallet = networkUtils.isMultisigSupported(chains[c.chainId].options);
const connection = connections[c.chainId];

return !isDisabled && (!isMultisigWallet || hasMultiPallet);
if (nullable(connection)) return false;
if (networkUtils.isDisabledConnection(connection)) return false;
if (nullable(chains[c.chainId])) return false;
if (!isMultisigWallet) return true;

return networkUtils.isMultisigSupported(chains[c.chainId].options) || multisigChainToInclude === c.chainId;
});

if (filteredChains.length > 0) {
acc.push({ ...token, chains: filteredChains });
}
if (filteredChains.length === 0) continue;

activeTokens.push({ ...token, chains: filteredChains });
}

return acc;
}, []);
return activeTokens;
},
target: $activeTokens,
});
Expand Down Expand Up @@ -148,7 +158,9 @@ sample({
clock: [$activeTokensWithBalance, queryChanged],
source: { activeTokensWithBalance: $activeTokensWithBalance, query: $query },
fn: ({ activeTokensWithBalance, query }) => {
return activeTokensWithBalance.reduce<AssetByChains[]>((acc, token) => {
const filteredTokens: AssetByChains[] = [];

for (const token of activeTokensWithBalance) {
const filteredChains = token.chains.filter((chain) => {
const hasSymbol = includes(chain.assetSymbol, query);
const hasAssetName = includes(token.name, query);
Expand All @@ -157,12 +169,12 @@ sample({
return hasSymbol || hasAssetName || hasChainName;
});

if (filteredChains.length > 0) {
acc.push({ ...token, chains: filteredChains });
}
if (filteredChains.length === 0) continue;

filteredTokens.push({ ...token, chains: filteredChains });
}

return acc;
}, []);
return filteredTokens;
},
target: $filteredTokens,
});
Expand Down

0 comments on commit af4b908

Please sign in to comment.