Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: poll only popular network #12658

Merged
merged 25 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,14 @@ describe('TokenFilterBottomSheet', () => {
it('renders correctly with the default option (All Networks) selected', () => {
const { queryByText } = render(<TokenFilterBottomSheet />);

expect(queryByText('All Networks')).toBeTruthy();
expect(queryByText('Popular networks')).toBeTruthy();
expect(queryByText('Current Network')).toBeTruthy();
});

it('sets filter to All Networks and closes bottom sheet when first option is pressed', async () => {
const { queryByText } = render(<TokenFilterBottomSheet />);

fireEvent.press(queryByText('All Networks'));
fireEvent.press(queryByText('Popular networks'));
salimtb marked this conversation as resolved.
Show resolved Hide resolved

await waitFor(() => {
expect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ const TokenFilterBottomSheet = () => {
verticalAlignment={VerticalAlignment.Center}
>
<Text style={styles.bottomSheetText}>
{strings('wallet.all_networks')}
{`${strings('app_settings.popular')} ${strings(
'app_settings.networks',
)}`}
</Text>
</ListItemSelect>
<ListItemSelect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ exports[`Tokens Portfolio View should match the snapshot when portfolio view is
}
}
>
All Networks
Ethereum Main Network
</Text>
<SvgMock
color="#141618"
Expand Down
30 changes: 16 additions & 14 deletions app/components/UI/Tokens/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useRef, useState, LegacyRef, useMemo, useEffect } from 'react';
import React, { useRef, useState, LegacyRef, useMemo } from 'react';
import { Hex } from '@metamask/utils';
import { View, Text } from 'react-native';
import ActionSheet from '@metamask/react-native-actionsheet';
Expand Down Expand Up @@ -56,6 +56,8 @@ import { selectNetworkName } from '../../../selectors/networkInfos';
import ButtonIcon from '../../../component-library/components/Buttons/ButtonIcon';
import { selectAccountTokensAcrossChains } from '../../../selectors/multichain';
import { filterAssets } from './util/filterAssets';
import { PopularList } from '../../../util/networks/customNetworks';
import { CHAIN_IDS } from '@metamask/transaction-controller';

// this will be imported from TokenRatesController when it is exported from there
// PR: https://github.com/MetaMask/core/pull/4622
Expand Down Expand Up @@ -141,6 +143,13 @@ const Tokens: React.FC<TokensI> = ({ tokens }) => {

const styles = createStyles(colors);

const isPopularNetwork = PopularList.some(
(network) =>
network.chainId === currentChainId ||
currentChainId === CHAIN_IDS.MAINNET ||
currentChainId === CHAIN_IDS.LINEA_MAINNET,
);

const tokensList = useMemo((): TokenI[] => {
if (isPortfolioViewEnabled()) {
// MultiChain implementation
Expand Down Expand Up @@ -404,15 +413,6 @@ const Tokens: React.FC<TokensI> = ({ tokens }) => {
const onActionSheetPress = (index: number) =>
index === 0 ? removeToken() : null;

useEffect(() => {
const { PreferencesController } = Engine.context;
if (isTestNet(currentChainId)) {
PreferencesController.setTokenNetworkFilter({
[currentChainId]: true,
});
}
}, [currentChainId]);

return (
<View
style={styles.wrapper}
Expand All @@ -425,19 +425,21 @@ const Tokens: React.FC<TokensI> = ({ tokens }) => {
label={
<Text style={styles.controlButtonText} numberOfLines={1}>
{isAllNetworks
? strings('wallet.all_networks')
? `${strings('app_settings.popular')} ${strings(
'app_settings.networks',
)}`
: networkName ?? strings('wallet.current_network')}
</Text>
}
isDisabled={isTestNet(currentChainId)}
isDisabled={isTestNet(currentChainId) || !isPopularNetwork}
onPress={showFilterControls}
endIconName={IconName.ArrowDown}
style={
isTestNet(currentChainId)
isTestNet(currentChainId) || !isPopularNetwork
? styles.controlButtonDisabled
: styles.controlButton
}
disabled={isTestNet(currentChainId)}
disabled={isTestNet(currentChainId) || !isPopularNetwork}
/>
<View style={styles.controlButtonInnerWrapper}>
<ButtonIcon
Expand Down
77 changes: 37 additions & 40 deletions app/components/UI/Tokens/util/enableAllNetworksFilter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,46 +66,6 @@ describe('enableAllNetworksFilter', () => {
expect(result).toEqual({});
});

it('should work with NETWORK_CHAIN_ID constants', () => {
const mockNetworks: FlareTestNetworkConfigurations = {
[NETWORK_CHAIN_ID.FLARE_MAINNET]: {
chainId: NETWORK_CHAIN_ID.FLARE_MAINNET,
name: 'Flare Mainnet',
blockExplorerUrls: ['https://flare.network'],
defaultRpcEndpointIndex: 0,
nativeCurrency: 'FLR',
rpcEndpoints: [
{
type: RpcEndpointType.Custom,
networkClientId: NETWORK_CHAIN_ID.FLARE_MAINNET,
url: 'https://flare-rpc.com',
},
],
},
[NETWORK_CHAIN_ID.SONGBIRD_TESTNET]: {
chainId: NETWORK_CHAIN_ID.SONGBIRD_TESTNET,
name: 'Songbird Testnet',
blockExplorerUrls: ['https://songbird.flare.network'],
defaultRpcEndpointIndex: 0,
nativeCurrency: 'SGB',
rpcEndpoints: [
{
type: RpcEndpointType.Custom,
networkClientId: NETWORK_CHAIN_ID.SONGBIRD_TESTNET,
url: 'https://songbird-rpc.flare.network',
},
],
},
};

const result = enableAllNetworksFilter(mockNetworks);

expect(result).toEqual({
[NETWORK_CHAIN_ID.FLARE_MAINNET]: true,
[NETWORK_CHAIN_ID.SONGBIRD_TESTNET]: true,
});
});

it('should handle networks with different property values', () => {
const mockNetworks: MultiNetworkConfigurations = {
[NETWORK_CHAIN_ID.MAINNET]: {
Expand Down Expand Up @@ -161,4 +121,41 @@ describe('enableAllNetworksFilter', () => {
NETWORK_CHAIN_ID.BASE,
]);
});

it('should return empty object if all networks are not popular', () => {
const mockNetworks: FlareTestNetworkConfigurations = {
[NETWORK_CHAIN_ID.FLARE_MAINNET]: {
chainId: NETWORK_CHAIN_ID.FLARE_MAINNET,
name: 'Flare Mainnet',
blockExplorerUrls: ['https://flare.network'],
defaultRpcEndpointIndex: 0,
nativeCurrency: 'FLR',
rpcEndpoints: [
{
type: RpcEndpointType.Custom,
networkClientId: NETWORK_CHAIN_ID.FLARE_MAINNET,
url: 'https://flare-rpc.com',
},
],
},
[NETWORK_CHAIN_ID.SONGBIRD_TESTNET]: {
chainId: NETWORK_CHAIN_ID.SONGBIRD_TESTNET,
name: 'Songbird Testnet',
blockExplorerUrls: ['https://songbird.flare.network'],
defaultRpcEndpointIndex: 0,
nativeCurrency: 'SGB',
rpcEndpoints: [
{
type: RpcEndpointType.Custom,
networkClientId: NETWORK_CHAIN_ID.SONGBIRD_TESTNET,
url: 'https://songbird-rpc.flare.network',
},
],
},
};

const result = enableAllNetworksFilter(mockNetworks);

expect(result).toEqual({});
});
});
16 changes: 14 additions & 2 deletions app/components/UI/Tokens/util/enableAllNetworksFilter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NetworkConfiguration } from '@metamask/network-controller';
import { Hex } from '@metamask/utils';
import { NETWORK_CHAIN_ID } from '../../../../util/networks/customNetworks';
import { NETWORK_CHAIN_ID, PopularList } from '../../../../util/networks/customNetworks';
import { CHAIN_IDS } from '@metamask/transaction-controller';

export type KnownNetworkConfigurations = {
[K in (typeof NETWORK_CHAIN_ID)[keyof typeof NETWORK_CHAIN_ID]]: NetworkConfiguration;
Expand All @@ -12,7 +13,18 @@ export function enableAllNetworksFilter(
const allOpts: Record<Hex, boolean> = {};
Object.keys(networks).forEach((chainId) => {
const hexChainId = chainId as Hex;
allOpts[hexChainId] = true;

const isPopularNetwork = PopularList.some(
(item) => item.chainId === hexChainId,
);

if (
isPopularNetwork ||
hexChainId === CHAIN_IDS.MAINNET ||
hexChainId === CHAIN_IDS.LINEA_MAINNET
) {
allOpts[hexChainId] = true;
}
});
return allOpts;
}
13 changes: 11 additions & 2 deletions app/components/Views/NetworkSelector/NetworkSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { useSelector } from 'react-redux';
import {
selectNetworkConfigurations,
selectIsAllNetworks,
selectChainId,
} from '../../../selectors/networkController';
import { selectShowTestNetworks } from '../../../selectors/preferencesController';
import Networks, {
Expand Down Expand Up @@ -127,6 +128,7 @@ const NetworkSelector = () => {
const sheetRef = useRef<BottomSheetRef>(null);
const showTestNetworks = useSelector(selectShowTestNetworks);
const isAllNetworks = useSelector(selectIsAllNetworks);
const currentChainId = useSelector(selectChainId);

const networkConfigurations = useSelector(selectNetworkConfigurations);

Expand Down Expand Up @@ -177,16 +179,23 @@ const NetworkSelector = () => {
isReadOnly: false,
});

const isPopularNetwork = PopularList.some(
(network) =>
network.chainId === currentChainId ||
currentChainId === CHAIN_IDS.MAINNET ||
currentChainId === CHAIN_IDS.LINEA_MAINNET,
);

const setTokenNetworkFilter = useCallback(
(chainId: string) => {
const { PreferencesController } = Engine.context;
if (!isAllNetworks) {
if (!isAllNetworks && isPopularNetwork && !isTestNet(chainId)) {
PreferencesController.setTokenNetworkFilter({
[chainId]: true,
});
}
},
[isAllNetworks],
[isAllNetworks, isPopularNetwork],
);

const onRpcSelect = useCallback(
Expand Down
Loading
Loading