Skip to content

Commit

Permalink
fix: fix native tokens filter when all networks is selected
Browse files Browse the repository at this point in the history
  • Loading branch information
sahar-fehri committed Dec 11, 2024
1 parent 259faa3 commit 3d74765
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions app/components/UI/Tokens/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { WalletViewSelectorsIDs } from '../../../../e2e/selectors/wallet/WalletV
import { strings } from '../../../../locales/i18n';
import { IconName } from '../../../component-library/components/Icons/Icon';
import {
selectIsTokenNetworkFilterEqualCurrentNetwork,
selectTokenNetworkFilter,
selectTokenSortConfig,
} from '../../../selectors/preferencesController';
Expand Down Expand Up @@ -104,6 +105,9 @@ const Tokens: React.FC<TokensI> = ({ tokens }) => {
const hideZeroBalanceTokens = useSelector(
(state: RootState) => state.settings.hideZeroBalanceTokens,
);
const isUserOnCurrentNetwork = useSelector(
selectIsTokenNetworkFilterEqualCurrentNetwork,
);

const tokenExchangeRates = useSelector(selectContractExchangeRates);
const currentCurrency = useSelector(selectCurrentCurrency);
Expand Down Expand Up @@ -144,17 +148,36 @@ const Tokens: React.FC<TokensI> = ({ tokens }) => {
selectedAccountTokensChains,
).flat() as TokenI[];

// First filter zero balance tokens if setting is enabled
const tokensToDisplay = hideZeroBalanceTokens
? allTokens.filter((curToken) => {
/*
If hideZeroBalanceTokens is ON and user is on "all Networks" we respect the setting and filter native and ERC20 tokens when zero
If user is on "current Network" we want to show native tokens, even with zero balance
*/
let tokensToDisplay = [];
if (hideZeroBalanceTokens) {
if (isUserOnCurrentNetwork) {
tokensToDisplay = allTokens.filter((curToken) => {
const multiChainTokenBalances =
multiChainTokenBalance?.[selectedInternalAccountAddress as Hex]?.[
curToken.chainId as Hex
];
const balance = multiChainTokenBalances?.[curToken.address as Hex];
return !isZero(balance) || curToken.isNative || curToken.isStaked;
})
: allTokens;
});
} else {
tokensToDisplay = allTokens.filter((curToken) => {
const multiChainTokenBalances =
multiChainTokenBalance?.[selectedInternalAccountAddress as Hex]?.[
curToken.chainId as Hex
];
const balance =
multiChainTokenBalances?.[curToken.address as Hex] ||
curToken.balance;
return !isZero(balance) || curToken.isStaked;
});
}
} else {
tokensToDisplay = allTokens;
}

// Then apply network filters
const filteredAssets = filterAssets(tokensToDisplay, [
Expand Down Expand Up @@ -273,6 +296,7 @@ const Tokens: React.FC<TokensI> = ({ tokens }) => {
multiChainTokenBalance,
networkConfigurationsByChainId,
selectedInternalAccountAddress,
isUserOnCurrentNetwork,
]);

const showRemoveMenu = (token: TokenI) => {
Expand Down

0 comments on commit 3d74765

Please sign in to comment.