Skip to content

Commit

Permalink
[APP-379]: Update client to use new token search aggregator across ne…
Browse files Browse the repository at this point in the history
…tworks (#5190)

* update rainbow fetch to be typed and update token search endpoint

* fix swap currency list

* update interfaces and transforms

* idkwtf i'm doing

* remove unused mapping

* revert swap changes and fix icon_url

* fix lint
  • Loading branch information
walmat authored Dec 1, 2023
1 parent c16745e commit e85c3fe
Show file tree
Hide file tree
Showing 7 changed files with 775 additions and 21 deletions.
24 changes: 24 additions & 0 deletions src/entities/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ChainId } from '@rainbow-me/swaps';
import { AssetType } from './assetTypes';
import { EthereumAddress } from '.';
import { Network } from '@/helpers';
import { Chain } from '@wagmi/chains';

export interface ZerionAssetPrice {
value: number;
Expand Down Expand Up @@ -88,6 +89,29 @@ export interface SwappableAsset extends ParsedAddressAsset {
network?: Network;
}

export interface TokenSearchNetwork {
address: string;
decimals: number;
}

export interface TokenSearchToken {
decimals: number;
highLiquidity: boolean;
name: string;
symbol: string;
uniqueId: string;
colors: { primary: string; fallback: string };
icon_url: string;
color: string;
shadowColor: string;
rainbowMetadataId: number;
isRainbowCurated: boolean;
isVerified: boolean;
networks: {
[chainId in Chain['id']]: TokenSearchNetwork;
};
}

export interface RainbowToken extends Asset {
color?: string;
highLiquidity?: boolean;
Expand Down
67 changes: 66 additions & 1 deletion src/handlers/tokenSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import {
} from '@/entities';
import { logger, RainbowError } from '@/logger';
import { EthereumAddress } from '@rainbow-me/swaps';
import { RainbowToken, TokenSearchToken } from '@/entities/tokens';
import ethereumUtils from '@/utils/ethereumUtils';

type TokenSearchApiResponse = {
data: TokenSearchToken[];
};

const tokenSearchApi = new RainbowFetchClient({
baseURL: 'https://token-search.rainbow.me/v2',
Expand All @@ -18,7 +24,7 @@ const tokenSearchApi = new RainbowFetchClient({
timeout: 30000,
});

export const tokenSearch = async (searchParams: {
export const swapSearch = async (searchParams: {
chainId: number;
fromChainId?: number | '';
keys: TokenSearchUniswapAssetKey[];
Expand Down Expand Up @@ -60,6 +66,65 @@ export const tokenSearch = async (searchParams: {
}
};

export const tokenSearch = async (searchParams: {
chainId: number;
fromChainId?: number | '';
keys: TokenSearchUniswapAssetKey[];
list: TokenSearchTokenListId;
threshold: TokenSearchThreshold;
query: string;
}): Promise<RainbowToken[]> => {
const queryParams: {
keys: TokenSearchUniswapAssetKey[];
list: TokenSearchTokenListId;
threshold: TokenSearchThreshold;
query?: string;
fromChainId?: number;
} = {
keys: searchParams.keys,
list: searchParams.list,
threshold: searchParams.threshold,
query: searchParams.query,
};

try {
if (isAddress(searchParams.query)) {
// @ts-ignore
params.keys = `networks.${params.chainId}.address`;
}
const url = `/?${qs.stringify(queryParams)}`;
const tokenSearch = await tokenSearchApi.get<TokenSearchApiResponse>(url);
if (!tokenSearch.data?.data) {
return [];
}

return tokenSearch.data.data.map(token => {
const networkKeys = Object.keys(token.networks);
const type = ethereumUtils.getAssetTypeFromNetwork(
ethereumUtils.getNetworkFromChainId(Number(networkKeys[0]))
);
return {
...token,
address:
token.networks['1']?.address ||
token.networks[Number(networkKeys[0])]?.address,
type,
mainnet_address: token.networks['1']?.address,
};
});
} catch (e: any) {
logger.error(
new RainbowError(`An error occurred while searching for query`),
{
query: searchParams.query,
message: e.message,
}
);

return [];
}
};

export const walletFilter = async (params: {
addresses: EthereumAddress[];
fromChainId: number;
Expand Down
1 change: 1 addition & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ export {
useHardwareBackOnFocus,
} from './useHardwareBack';
export { default as useSwapCurrencyList } from './useSwapCurrencyList';
export { default as useSearchCurrencyList } from './useSearchCurrencyList';
export { default as useWalletENSAvatar } from './useWalletENSAvatar';
export { default as useImagePicker } from './useImagePicker';
export { default as useLatestCallback } from './useLatestCallback';
Expand Down
Loading

0 comments on commit e85c3fe

Please sign in to comment.