Skip to content

Commit

Permalink
fix: portfolio search with zero balance (#2465)
Browse files Browse the repository at this point in the history
  • Loading branch information
sokolova-an authored Oct 15, 2024
1 parent 953d7d4 commit fdda613
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const tokensService = {
getChainWithBalance,
sumTokenBalances,
sortTokensByBalance,
hideZeroBalances,
};

function getTokensData(): AssetByChains[] {
Expand Down Expand Up @@ -61,7 +62,6 @@ function getSelectedAccountIds(accounts: Account[], chainId: ChainId): AccountId
function getChainWithBalance(
balances: Balance[],
chains: AssetChain[],
hideZeroBalances: boolean,
accounts: Account[],
): [AssetChain[], AssetBalance] {
let totalBalance = {} as AssetBalance;
Expand All @@ -82,7 +82,7 @@ function getChainWithBalance(

totalBalance = sumTokenBalances(assetBalance, totalBalance);

if (!hideZeroBalances || assetBalance.verified === false || totalAmount(assetBalance) !== ZERO_BALANCE) {
if (assetBalance.verified !== false) {
acc.push({ ...chain, balance: assetBalance });
}

Expand All @@ -92,6 +92,12 @@ function getChainWithBalance(
return [chainsWithBalance, totalBalance];
}

function hideZeroBalances(hideZeroBalance: boolean, activeTokensWithBalance: AssetByChains[]): AssetByChains[] {
return activeTokensWithBalance.filter((token) => {
return hideZeroBalance && totalAmount(token.totalBalance) === ZERO_BALANCE ? false : true;
});
}

function sortTokensByBalance(
tokens: AssetByChains[],
assetsPrices: PriceObject | null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,12 @@ type PopulateBalanceParams = {
activeTokens: AssetByChains[];
balances: Balance[];
accounts: Account[];
hideZeroBalances: boolean;
};

const populateTokensBalanceFx = createEffect(
({ activeTokens, balances, accounts, hideZeroBalances }: PopulateBalanceParams): AssetByChains[] => {
({ activeTokens, balances, accounts }: PopulateBalanceParams): AssetByChains[] => {
return activeTokens.reduce<AssetByChains[]>((acc, token) => {
const [chainsWithBalance, totalBalance] = tokensService.getChainWithBalance(
balances,
token.chains,
hideZeroBalances,
accounts,
);
const [chainsWithBalance, totalBalance] = tokensService.getChainWithBalance(balances, token.chains, accounts);

if (chainsWithBalance.length > 0) {
acc.push({ ...token, chains: chainsWithBalance, totalBalance });
Expand Down Expand Up @@ -98,7 +92,7 @@ sample({
});

sample({
clock: [networkModel.$connections, $tokens, $hideZeroBalances],
clock: [networkModel.$connections, $tokens],
source: {
activeView: $activeView,
activeWallet: walletModel.$activeWallet,
Expand Down Expand Up @@ -132,13 +126,12 @@ sample({
});

sample({
clock: [balanceModel.$balances, networkModel.$connections, $accounts, $tokens, $hideZeroBalances],
clock: [balanceModel.$balances, networkModel.$connections, $accounts, $tokens],
source: {
activeView: $activeView,
activeTokens: $activeTokens,
accounts: $accounts,
balances: balanceModel.$balances,
hideZeroBalances: $hideZeroBalances,
},
filter: ({ activeView, balances }) => {
return Boolean(activeView === AssetsListView.TOKEN_CENTRIC && balances.length > 0);
Expand Down Expand Up @@ -175,17 +168,20 @@ sample({
});

sample({
clock: [$activeTokensWithBalance, $filtredTokens],
clock: [$activeTokensWithBalance, $filtredTokens, $hideZeroBalances],
source: {
query: $query,
activeTokensWithBalance: $activeTokensWithBalance,
$hideZeroBalances: $hideZeroBalances,
filtredTokens: $filtredTokens,
assetsPrices: priceProviderModel.$assetsPrices,
fiatFlag: priceProviderModel.$fiatFlag,
currency: currencyModel.$activeCurrency,
},
fn: ({ query, activeTokensWithBalance, filtredTokens, assetsPrices, fiatFlag, currency }) => {
const tokenList = query ? filtredTokens : activeTokensWithBalance;
fn: ({ query, activeTokensWithBalance, filtredTokens, $hideZeroBalances, assetsPrices, fiatFlag, currency }) => {
const tokenList = query
? filtredTokens
: tokensService.hideZeroBalances($hideZeroBalances, activeTokensWithBalance);

return tokensService.sortTokensByBalance(tokenList, assetsPrices, fiatFlag ? currency?.coingeckoId : undefined);
},
Expand Down

0 comments on commit fdda613

Please sign in to comment.