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

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
28 changes: 24 additions & 4 deletions app/components/UI/Tokens/index.tsx
Original file line number Diff line number Diff line change
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,22 @@ 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,
);

useEffect(() => {
const { PreferencesController } = Engine.context;
if (!isPopularNetwork) {
PreferencesController.setTokenNetworkFilter({
[currentChainId]: true,
});
salimtb marked this conversation as resolved.
Show resolved Hide resolved
}
}, [selectedAccountTokensChains, currentChainId, isPopularNetwork]);

const tokensList = useMemo((): TokenI[] => {
if (isPortfolioViewEnabled()) {
// MultiChain implementation
Expand Down Expand Up @@ -425,19 +443,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;
}
Loading
Loading