-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
swap seach popular in rainbow (#1696)
Co-authored-by: Daniel Sinclair <[email protected]>
- Loading branch information
1 parent
5a6f572
commit e30c1d8
Showing
9 changed files
with
139 additions
and
362 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`, | ||
}; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.