Skip to content

Commit

Permalink
Update RainbowToken search result to SearchAsset for imported asset r…
Browse files Browse the repository at this point in the history
…esult
  • Loading branch information
jinchung committed Dec 30, 2024
1 parent 4dde26e commit 87b3b3f
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions src/__swaps__/screens/Swap/resources/search/search.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable no-nested-ternary */
import { ChainId } from '@/state/backendNetworks/types';
import { useBackendNetworksStore } from '@/state/backendNetworks/backendNetworks';
import { SearchAsset, TokenSearchAssetKey, TokenSearchListId, TokenSearchThreshold } from '@/__swaps__/types/search';
import { RainbowError, logger } from '@/logger';
import { RainbowFetchClient } from '@/rainbow-fetch';
Expand All @@ -11,7 +10,6 @@ import { Contract } from '@ethersproject/contracts';
import { useQuery } from '@tanstack/react-query';
import qs from 'qs';
import { parseTokenSearch } from './utils';
import { RainbowToken } from '@/entities';
import { getProvider } from '@/handlers/web3';
import { erc20ABI } from '@/references';

Expand Down Expand Up @@ -40,8 +38,7 @@ export type TokenSearchArgs = {
};

export type TokenSearchAllNetworksArgs = {
query?: string;
shouldPersist?: boolean;
query: string;
};

// ///////////////////////////////////////////////
Expand All @@ -55,8 +52,9 @@ const tokenSearchQueryKey = ({ chainId, fromChainId, keys, list, threshold, quer
);
};

const tokenSearchAllNetworksQueryKey = ({ query, shouldPersist }: TokenSearchAllNetworksArgs) => {
return createQueryKey('TokenSearch', { query }, { persisterVersion: shouldPersist ? 3 : undefined });
const tokenSearchAllNetworksQueryKey = ({ query }: TokenSearchAllNetworksArgs) => {
const shouldPersist = query === '';
return createQueryKey('TokenSearchAllNetworks', { query }, { persisterVersion: shouldPersist ? 1 : undefined });
};

type TokenSearchQueryKey = ReturnType<typeof tokenSearchQueryKey>;
Expand All @@ -65,7 +63,7 @@ type TokenSearchAllNetworksQueryKey = ReturnType<typeof tokenSearchAllNetworksQu

// ///////////////////////////////////////////////
// Query Function
const getImportedAsset = async (searchQuery: string, chainId: number = ChainId.mainnet): Promise<RainbowToken[] | null> => {
const getImportedAsset = async (searchQuery: string, chainId: number = ChainId.mainnet): Promise<SearchAsset[]> => {
if (isAddress(searchQuery)) {
const provider = getProvider({ chainId });
const tokenContract = new Contract(searchQuery, erc20ABI, provider);
Expand All @@ -83,10 +81,10 @@ const getImportedAsset = async (searchQuery: string, chainId: number = ChainId.m
chainId,
address,
decimals,
favorite: false,
highLiquidity: false,
isRainbowCurated: false,
isVerified: false,
mainnetAddress: address,
name,
networks: {
[chainId]: {
Expand All @@ -95,16 +93,15 @@ const getImportedAsset = async (searchQuery: string, chainId: number = ChainId.m
},
},
symbol,
network: useBackendNetworksStore.getState().getChainsName()[chainId],
uniqueId,
} as RainbowToken,
} as SearchAsset,
];
} catch (e) {
logger.warn('[useSearchCurrencyList]: error getting token data', { error: (e as Error).message });
return null;
logger.warn('[getImportedAsset]: error getting imported token data', { error: (e as Error).message });
return [];
}
}
return null;
return [];
};

async function tokenSearchQueryFunction({
Expand Down Expand Up @@ -179,7 +176,8 @@ async function tokenSearchQueryFunctionAllNetworks({ queryKey: [{ query }] }: Qu
return parseTokenSearch(tokenSearch.data.data);
}

return [];
const result = await getImportedAsset(query);
return result;
} else {
const tokenSearch = await tokenSearchHttp.get<{ data: SearchAsset[] }>(url);
return parseTokenSearch(tokenSearch.data.data);
Expand Down

0 comments on commit 87b3b3f

Please sign in to comment.