diff --git a/src/chains/index.ts b/src/chains/index.ts deleted file mode 100644 index 7915cccdf51..00000000000 --- a/src/chains/index.ts +++ /dev/null @@ -1,208 +0,0 @@ -import { Chain } from 'viem/chains'; -import { queryClient } from '@/react-query'; -import { backendNetworksQueryKey, BackendNetworksResponse } from '@/resources/metadata/backendNetworks'; -import { ChainId, BackendNetwork, BackendNetworkServices, chainHardhat, chainHardhatOptimism } from './types'; -import { transformBackendNetworksToChains } from './utils/backendNetworks'; -import { gasUtils } from '@/utils'; -import { useConnectedToHardhatStore } from '@/state/connectedToHardhat'; -import { IS_TEST } from '@/env'; -import buildTimeNetworks from '@/references/networks.json'; - -// NOTE: Prefer runtime data from backendNetworksQueryKey, but fallback to buildTimeNetworks if needed -const backendNetworks = queryClient.getQueryData(backendNetworksQueryKey()) ?? buildTimeNetworks; - -const BACKEND_CHAINS = transformBackendNetworksToChains(backendNetworks.networks); - -export const SUPPORTED_CHAINS: Chain[] = IS_TEST ? [...BACKEND_CHAINS, chainHardhat, chainHardhatOptimism] : BACKEND_CHAINS; - -export const SUPPORTED_CHAIN_IDS_ALPHABETICAL: ChainId[] = SUPPORTED_CHAINS.sort((a, b) => a.name.localeCompare(b.name)).map(c => c.id); - -export const defaultChains: Record = SUPPORTED_CHAINS.reduce( - (acc, chain) => { - acc[chain.id] = chain; - return acc; - }, - {} as Record -); - -export const SUPPORTED_CHAIN_IDS = SUPPORTED_CHAINS.map(chain => chain.id); - -export const SUPPORTED_MAINNET_CHAINS: Chain[] = SUPPORTED_CHAINS.filter(chain => !chain.testnet); - -export const SUPPORTED_MAINNET_CHAIN_IDS: ChainId[] = SUPPORTED_MAINNET_CHAINS.map(chain => chain.id); - -export const needsL1SecurityFeeChains = backendNetworks.networks - .filter((backendNetwork: BackendNetwork) => backendNetwork.opStack) - .map((backendNetwork: BackendNetwork) => parseInt(backendNetwork.id, 10)); - -export const chainsNativeAsset: Record = backendNetworks.networks.reduce( - (acc, backendNetwork: BackendNetwork) => { - acc[parseInt(backendNetwork.id, 10)] = backendNetwork.nativeAsset; - return acc; - }, - {} as Record -); - -export const chainsLabel: Record = backendNetworks.networks.reduce( - (acc, backendNetwork: BackendNetwork) => { - acc[parseInt(backendNetwork.id, 10)] = backendNetwork.label; - return acc; - }, - {} as Record -); - -export const chainsName: Record = backendNetworks.networks.reduce( - (acc, backendNetwork: BackendNetwork) => { - acc[parseInt(backendNetwork.id, 10)] = backendNetwork.name; - return acc; - }, - {} as Record -); - -export const chainsIdByName: Record = backendNetworks.networks.reduce( - (acc, backendNetwork: BackendNetwork) => { - acc[backendNetwork.name] = parseInt(backendNetwork.id, 10); - return acc; - }, - {} as Record -); - -const defaultGasSpeeds = (chainId: ChainId) => { - switch (chainId) { - case ChainId.bsc: - case ChainId.goerli: - case ChainId.polygon: - return [gasUtils.NORMAL, gasUtils.FAST, gasUtils.URGENT]; - case ChainId.gnosis: - return [gasUtils.NORMAL]; - default: - return [gasUtils.NORMAL, gasUtils.FAST, gasUtils.URGENT, gasUtils.CUSTOM]; - } -}; - -export const chainsGasSpeeds: Record = backendNetworks.networks.reduce( - (acc, backendNetwork: BackendNetwork) => { - acc[parseInt(backendNetwork.id, 10)] = defaultGasSpeeds(parseInt(backendNetwork.id, 10)); - return acc; - }, - {} as Record -); - -const defaultPollingIntervals = (chainId: ChainId) => { - switch (chainId) { - case ChainId.polygon: - return 2_000; - case ChainId.arbitrum: - case ChainId.bsc: - return 3_000; - default: - return 5_000; - } -}; - -export const chainsSwapPollingInterval: Record = backendNetworks.networks.reduce( - (acc, backendNetwork: BackendNetwork) => { - acc[parseInt(backendNetwork.id, 10)] = defaultPollingIntervals(parseInt(backendNetwork.id, 10)); - return acc; - }, - {} as Record -); - -const defaultSimplehashNetworks = (chainId: ChainId) => { - switch (chainId) { - case ChainId.apechain: - return 'apechain'; - case ChainId.arbitrum: - return 'arbitrum'; - case ChainId.avalanche: - return 'avalanche'; - case ChainId.base: - return 'base'; - case ChainId.blast: - return 'blast'; - case ChainId.bsc: - return 'bsc'; - case ChainId.degen: - return 'degen'; - case ChainId.gnosis: - return 'gnosis'; - case ChainId.goerli: - return 'ethereum-goerli'; - case ChainId.mainnet: - return 'ethereum'; - case ChainId.optimism: - return 'optimism'; - case ChainId.polygon: - return 'polygon'; - case ChainId.zora: - return 'zora'; - default: - return ''; - } -}; - -export const chainsSimplehashNetwork: Record = backendNetworks.networks.reduce( - (acc, backendNetwork: BackendNetwork) => { - acc[parseInt(backendNetwork.id, 10)] = defaultSimplehashNetworks(parseInt(backendNetwork.id, 10)); - return acc; - }, - {} as Record -); - -const filterChainIdsByService = (servicePath: (services: BackendNetworkServices) => boolean): number[] => { - return backendNetworks.networks - .filter((network: BackendNetwork) => { - const services = network?.enabledServices; - return services && servicePath(services); - }) - .map((network: BackendNetwork) => parseInt(network.id, 10)); -}; - -export const meteorologySupportedChainIds = filterChainIdsByService(services => services.meteorology.enabled); - -export const supportedSwapChainIds = filterChainIdsByService(services => services.swap.enabled); - -export const supportedSwapExactOutputChainIds = filterChainIdsByService(services => services.swap.swapExactOutput); - -export const supportedBridgeExactOutputChainIds = filterChainIdsByService(services => services.swap.bridgeExactOutput); - -export const supportedNotificationsChainIds = filterChainIdsByService(services => services.notifications.enabled); - -export const supportedApprovalsChainIds = filterChainIdsByService(services => services.addys.approvals); - -export const supportedTransactionsChainIds = filterChainIdsByService(services => services.addys.transactions); - -export const supportedAssetsChainIds = filterChainIdsByService(services => services.addys.assets); - -export const supportedPositionsChainIds = filterChainIdsByService(services => services.addys.positions); - -export const supportedTokenSearchChainIds = filterChainIdsByService(services => services.tokenSearch.enabled); - -export const supportedNftChainIds = filterChainIdsByService(services => services.nftProxy.enabled); - -export const supportedFlashbotsChainIds = [ChainId.mainnet]; - -export const shouldDefaultToFastGasChainIds = [ChainId.mainnet, ChainId.polygon, ChainId.goerli]; - -const chainsGasUnits = backendNetworks.networks.reduce( - (acc, backendNetwork: BackendNetwork) => { - acc[parseInt(backendNetwork.id, 10)] = backendNetwork.gasUnits; - return acc; - }, - {} as Record -); - -export const getChainGasUnits = (chainId?: number) => { - return (chainId ? chainsGasUnits[chainId] : undefined) || chainsGasUnits[ChainId.mainnet]; -}; - -export const getChainDefaultRpc = (chainId: ChainId) => { - switch (chainId) { - case ChainId.mainnet: - return useConnectedToHardhatStore.getState().connectedToHardhat - ? 'http://127.0.0.1:8545' - : defaultChains[ChainId.mainnet].rpcUrls.default.http[0]; - default: - return defaultChains[chainId].rpcUrls.default.http[0]; - } -}; diff --git a/src/components/coin-icon/ChainImage.tsx b/src/components/coin-icon/ChainImage.tsx index 8b19ff96809..d7d250858f5 100644 --- a/src/components/coin-icon/ChainImage.tsx +++ b/src/components/coin-icon/ChainImage.tsx @@ -1,4 +1,4 @@ -import React, { forwardRef, useMemo } from 'react'; +import React, { useMemo, forwardRef } from 'react'; import { ChainId } from '@/state/backendNetworks/types'; import ApechainBadge from '@/assets/badges/apechain.png'; diff --git a/src/state/assets/userAssets.ts b/src/state/assets/userAssets.ts index d667880acdb..e161ec1dc99 100644 --- a/src/state/assets/userAssets.ts +++ b/src/state/assets/userAssets.ts @@ -10,7 +10,52 @@ import { swapsStore } from '@/state/swaps/swapsStore'; import { ChainId } from '@/state/backendNetworks/types'; import { useBackendNetworksStore } from '@/state/backendNetworks/backendNetworks'; import { useSelector } from 'react-redux'; +<<<<<<< HEAD import { getUniqueId } from '@/utils/ethereumUtils'; +======= + +const SEARCH_CACHE_MAX_ENTRIES = 50; + +const escapeRegExp = (string: string) => string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); +const getSearchQueryKey = ({ filter, searchQuery }: { filter: UserAssetFilter; searchQuery: string }) => `${filter}${searchQuery}`; + +const getDefaultCacheKeys = (): Set => { + const queryKeysToPreserve = new Set(); + queryKeysToPreserve.add('all'); + + for (const chainId of useBackendNetworksStore.getState().getSupportedChainIds()) { + queryKeysToPreserve.add(`${chainId}`); + } + return queryKeysToPreserve; +}; + +const CACHE_ITEMS_TO_PRESERVE = getDefaultCacheKeys(); + +export interface UserAssetsState { + chainBalances: Map; + currentAbortController: AbortController; + filter: UserAssetFilter; + idsByChain: Map; + inputSearchQuery: string; + searchCache: Map; + userAssets: Map; + getBalanceSortedChainList: () => ChainId[]; + getChainsWithBalance: () => ChainId[]; + getFilteredUserAssetIds: () => UniqueId[]; + getHighestValueNativeAsset: () => ParsedSearchAsset | null; + getUserAsset: (uniqueId: UniqueId) => ParsedSearchAsset | null; + getUserAssets: () => ParsedSearchAsset[]; + selectUserAssetIds: (selector: (asset: ParsedSearchAsset) => boolean, filter?: UserAssetFilter) => Generator; + selectUserAssets: (selector: (asset: ParsedSearchAsset) => boolean) => Generator<[UniqueId, ParsedSearchAsset], void, unknown>; + setSearchCache: (queryKey: string, filteredIds: UniqueId[]) => void; + setSearchQuery: (query: string) => void; + setUserAssets: (userAssets: Map | ParsedSearchAsset[]) => void; + + hiddenAssets: Set; + getHiddenAssetsIds: () => UniqueId[]; + setHiddenAssets: (uniqueIds: UniqueId[]) => void; +} +>>>>>>> e67803ca7 (convert network accessors to functions (#6219)) type UserAssetsStateToPersist = Omit< Partial,