Skip to content

Commit

Permalink
swap seach popular in rainbow (#1696)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Sinclair <[email protected]>
  • Loading branch information
greg-schrammel and DanielSinclair authored Oct 5, 2024
1 parent 5a6f572 commit e30c1d8
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 362 deletions.
21 changes: 21 additions & 0 deletions src/core/resources/search/parseTokenSearch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { AddressOrEth } from '~/core/types/assets';
import { ChainId } from '~/core/types/chains';
import { SearchAsset } from '~/core/types/search';
import { isNativeAsset } from '~/core/utils/chains';

export function parseTokenSearch(
asset: SearchAsset,
chainId: ChainId,
): SearchAsset {
const networkInfo = asset.networks[chainId];

return {
...asset,
address: networkInfo ? networkInfo.address : asset.address,
chainId,
decimals: networkInfo ? networkInfo.decimals : asset.decimals,
isNativeAsset: isNativeAsset(asset.address, chainId),
mainnetAddress: asset.uniqueId as AddressOrEth,
uniqueId: `${networkInfo?.address || asset.uniqueId}_${chainId}`,
};
}
101 changes: 0 additions & 101 deletions src/core/resources/search/swappableAddresses.ts

This file was deleted.

47 changes: 47 additions & 0 deletions src/core/resources/search/tokenDiscovery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { useQuery } from '@tanstack/react-query';

import { createHttpClient } from '~/core/network/internal/createHttpClient';
import { QueryFunctionArgs, createQueryKey } from '~/core/react-query';
import { ChainId } from '~/core/types/chains';
import { SearchAsset } from '~/core/types/search';

import { parseTokenSearch } from './parseTokenSearch';

const tokenSearchDiscoveryHttp = createHttpClient({
baseUrl: 'https://token-search.rainbow.me/v3/discovery',
timeout: 30000,
});

type TokenDiscoveryArgs = {
chainId: ChainId;
};

const tokenDiscoveryQueryKey = ({ chainId }: TokenDiscoveryArgs) =>
createQueryKey('TokenDiscovery', { chainId }, { persisterVersion: 1 });

async function tokenSearchQueryFunction({
queryKey: [{ chainId }],
}: QueryFunctionArgs<typeof tokenDiscoveryQueryKey>) {
const url = `/${chainId}`;

try {
const tokenSearch = await tokenSearchDiscoveryHttp.get<{
data: SearchAsset[];
}>(url);
return tokenSearch.data.data.map((asset) =>
parseTokenSearch(asset, chainId),
);
} catch (e) {
return [];
}
}

export function useTokenDiscovery({ chainId }: TokenDiscoveryArgs) {
return useQuery({
queryKey: tokenDiscoveryQueryKey({ chainId }),
queryFn: tokenSearchQueryFunction,
staleTime: 15 * 60 * 1000, // 15 min
gcTime: 24 * 60 * 60 * 1000, // 1 day
select: (data) => data.slice(0, 3),
});
}
43 changes: 5 additions & 38 deletions src/core/resources/search/tokenSearch.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { isAddress } from '@ethersproject/address';
import { useQueries, useQuery } from '@tanstack/react-query';
import qs from 'qs';
import { Address } from 'viem';

import { tokenSearchHttp } from '~/core/network/tokenSearch';
import {
Expand All @@ -11,11 +10,6 @@ import {
createQueryKey,
queryClient,
} from '~/core/react-query';
import {
BNB_BSC_ADDRESS,
ETH_ADDRESS,
POL_POLYGON_ADDRESS,
} from '~/core/references';
import { ChainId } from '~/core/types/chains';
import {
SearchAsset,
Expand All @@ -25,6 +19,8 @@ import {
} from '~/core/types/search';
import { getSupportedChains, isCustomChain } from '~/core/utils/chains';

import { parseTokenSearch } from './parseTokenSearch';

// ///////////////////////////////////////////////
// Query Types

Expand Down Expand Up @@ -90,43 +86,14 @@ async function tokenSearchQueryFunction({
const url = `/${chainId}/?${qs.stringify(queryParams)}`;
try {
const tokenSearch = await tokenSearchHttp.get<{ data: SearchAsset[] }>(url);
return parseTokenSearch(tokenSearch.data.data, chainId);
return tokenSearch.data.data.map((asset) =>
parseTokenSearch(asset, chainId),
);
} catch (e) {
return [];
}
}

function parseTokenSearch(assets: SearchAsset[], chainId: ChainId) {
return assets
.map((a) => {
const networkInfo = a.networks[chainId];

const asset: SearchAsset = {
...a,
address: networkInfo ? networkInfo.address : a.address,
chainId,
decimals: networkInfo ? networkInfo.decimals : a.decimals,
isNativeAsset: [
`${ETH_ADDRESS}_${ChainId.mainnet}`,
`${ETH_ADDRESS}_${ChainId.optimism}`,
`${ETH_ADDRESS}_${ChainId.arbitrum}`,
`${BNB_BSC_ADDRESS}_${ChainId.bsc}`,
`${POL_POLYGON_ADDRESS}_${ChainId.polygon}`,
`${ETH_ADDRESS}_${ChainId.base}`,
`${ETH_ADDRESS}_${ChainId.zora}`,
`${ETH_ADDRESS}_${ChainId.avalanche}`,
`${ETH_ADDRESS}_${ChainId.blast}`,
`${ETH_ADDRESS}_${ChainId.degen}`,
].includes(`${a.uniqueId}_${chainId}`),
mainnetAddress: a.uniqueId as Address,
uniqueId: `${networkInfo?.address || a.uniqueId}_${chainId}`,
};

return asset;
})
.filter(Boolean);
}

type TokenSearchResult = QueryFunctionResult<typeof tokenSearchQueryFunction>;

// ///////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion src/core/state/favorites/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import {
ETH_BLAST_ADDRESS,
ETH_OPTIMISM_ADDRESS,
ETH_ZORA_ADDRESS,
POL_POLYGON_ADDRESS,
OP_ADDRESS,
POL_POLYGON_ADDRESS,
SOCKS_ADDRESS,
SOCKS_ARBITRUM_ADDRESS,
USDB_BLAST_ADDRESS,
Expand Down
Loading

0 comments on commit e30c1d8

Please sign in to comment.