diff --git a/src/__swaps__/screens/Swap/resources/search/search.ts b/src/__swaps__/screens/Swap/resources/search/search.ts index e715bffe0bd..e41d96abed2 100644 --- a/src/__swaps__/screens/Swap/resources/search/search.ts +++ b/src/__swaps__/screens/Swap/resources/search/search.ts @@ -39,6 +39,11 @@ export type TokenSearchArgs = { shouldPersist?: boolean; }; +export type TokenSearchAllNetworksArgs = { + query?: string; + shouldPersist?: boolean; +}; + // /////////////////////////////////////////////// // Query Key @@ -50,8 +55,14 @@ const tokenSearchQueryKey = ({ chainId, fromChainId, keys, list, threshold, quer ); }; +const tokenSearchAllNetworksQueryKey = ({ query, shouldPersist }: TokenSearchAllNetworksArgs) => { + return createQueryKey('TokenSearch', { query }, { persisterVersion: shouldPersist ? 3 : undefined }); +}; + type TokenSearchQueryKey = ReturnType; +type TokenSearchAllNetworksQueryKey = ReturnType; + // /////////////////////////////////////////////// // Query Function const getImportedAsset = async (searchQuery: string, chainId: number = ChainId.mainnet): Promise => { @@ -149,6 +160,36 @@ async function tokenSearchQueryFunction({ } } +async function tokenSearchQueryFunctionAllNetworks({ queryKey: [{ query }] }: QueryFunctionArgs) { + const queryParams: { + query?: string; + } = { + query, + }; + + const isAddressSearch = query && isAddress(query); + + const url = `/?${qs.stringify(queryParams)}`; + + try { + if (isAddressSearch) { + const tokenSearch = await tokenSearchHttp.get<{ data: SearchAsset[] }>(url); + + if (tokenSearch && tokenSearch.data.data.length > 0) { + return parseTokenSearch(tokenSearch.data.data); + } + + return []; + } else { + const tokenSearch = await tokenSearchHttp.get<{ data: SearchAsset[] }>(url); + return parseTokenSearch(tokenSearch.data.data); + } + } catch (e) { + logger.error(new RainbowError('[tokenSearchQueryFunction]: Token search failed'), { url }); + return []; + } +} + export type TokenSearchResult = QueryFunctionResult; // /////////////////////////////////////////////// @@ -193,10 +234,10 @@ export function useTokenSearch( } export function useTokenSearchAllNetworks( - { query }: Omit, - config: QueryConfigWithSelect = {} + { query }: TokenSearchAllNetworksArgs, + config: QueryConfigWithSelect = {} ) { - return useQuery(tokenSearchQueryKey({ query }), tokenSearchQueryFunction, { + return useQuery(tokenSearchAllNetworksQueryKey({ query }), tokenSearchQueryFunctionAllNetworks, { ...config, keepPreviousData: true, });