From 753c87f2c99ebcbcd9d91b0cfc033232061d5923 Mon Sep 17 00:00:00 2001 From: Callum McIntyre Date: Mon, 25 Sep 2023 13:57:52 +0000 Subject: [PATCH] Use UTL API directly for address token info Most addresses displayed are not tokens, so the UTL SDK fallback to making an on-chain request is inappropriate --- app/components/common/Address.tsx | 4 ++-- app/utils/token-info.ts | 36 ++++++++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/app/components/common/Address.tsx b/app/components/common/Address.tsx index f0e84ffa..21c35deb 100644 --- a/app/components/common/Address.tsx +++ b/app/components/common/Address.tsx @@ -10,7 +10,7 @@ import React from 'react'; import { useState } from 'react'; import useAsyncEffect from 'use-async-effect'; -import { getTokenInfo } from '@/app/utils/token-info'; +import { getTokenInfoWithoutOnChainFallback } from '@/app/utils/token-info'; import { Copyable } from './Copyable'; @@ -125,7 +125,7 @@ const useTokenInfo = (fetchTokenLabelInfo: boolean | undefined, pubkey: string) if (!fetchTokenLabelInfo) return; if (!info) { try { - const token = await getTokenInfo(new PublicKey(pubkey), cluster, url); + const token = await getTokenInfoWithoutOnChainFallback(new PublicKey(pubkey), cluster); if (isMounted()) { setInfo(token); } diff --git a/app/utils/token-info.ts b/app/utils/token-info.ts index c1543fb6..ad3c2afb 100644 --- a/app/utils/token-info.ts +++ b/app/utils/token-info.ts @@ -73,6 +73,36 @@ export async function getTokenInfo( return token; } +type UtlApiResponse = { + content: Token[] +} + +export async function getTokenInfoWithoutOnChainFallback( + address: PublicKey, + cluster: Cluster +): Promise { + const chainId = getChainId(cluster); + if (!chainId) return undefined; + + // Request token info directly from UTL API + // We don't use the SDK here because we don't want it to fallback to an on-chain request + const response = await fetch(`https://token-list-api.solana.cloud/v1/mints?chainId=${chainId}`, { + body: JSON.stringify({ addresses: [address.toBase58()] }), + headers: { + "Content-Type": "application/json", + }, + method: 'POST', + }) + + if (response.status >= 400) { + console.error(`Error calling UTL API for address ${address} on chain ID ${chainId}. Status ${response.status}`); + return undefined + } + + const fetchedData = await response.json() as UtlApiResponse; + return fetchedData.content[0]; +} + async function getFullLegacyTokenInfoUsingCdn( address: PublicKey, chainId: ChainId @@ -111,9 +141,9 @@ export async function getFullTokenInfo( if (!sdkTokenInfo) { return legacyCdnTokenInfo ? { - ...legacyCdnTokenInfo, - verified: true, - } + ...legacyCdnTokenInfo, + verified: true, + } : undefined; }