From 3f7c8b7fe3ff25a24ad36b08d7624b55936d66fe Mon Sep 17 00:00:00 2001 From: Daniel Isaac Geslin Date: Tue, 27 Aug 2024 11:32:13 +0200 Subject: [PATCH] [support] Algo assets are not searchable/selectable on the bridge, user id -736691227193245717 #1003 --- .../TokenSelectors/AlgoTokenPicker.tsx | 58 ++++++------- src/components/TokenSelectors/TokenPicker.tsx | 82 +++++++++++-------- 2 files changed, 73 insertions(+), 67 deletions(-) diff --git a/src/components/TokenSelectors/AlgoTokenPicker.tsx b/src/components/TokenSelectors/AlgoTokenPicker.tsx index 52fbddbdc..1a88404f6 100644 --- a/src/components/TokenSelectors/AlgoTokenPicker.tsx +++ b/src/components/TokenSelectors/AlgoTokenPicker.tsx @@ -43,44 +43,36 @@ export default function AlgoTokenPicker(props: AlgoTokenPickerProps) { ); const lookupAlgoAddress = useCallback( - (lookupAsset: string) => { - if (!walletAddress) { - return Promise.reject("Wallet not connected"); - } + async (lookupAsset: string) => { + if (!walletAddress) throw new Error("Wallet not connected"); const algodClient = new Algodv2( ALGORAND_HOST.algodToken, ALGORAND_HOST.algodServer, ALGORAND_HOST.algodPort ); - return fetchSingleMetadata(lookupAsset, algodClient) - .then((metadata) => { - return algodClient - .accountInformation(walletAddress) - .do() - .then((accountInfo) => { - for (const asset of accountInfo.assets) { - const assetId = asset["asset-id"]; - if (assetId.toString() === lookupAsset) { - const amount = asset.amount; - return createParsedTokenAccount( - walletAddress, - assetId.toString(), - amount, - metadata.decimals, - parseFloat(formatUnits(amount, metadata.decimals)), - formatUnits(amount, metadata.decimals).toString(), - metadata.symbol, - metadata.tokenName, - undefined, - false - ); - } - } - return Promise.reject(); - }) - .catch(() => Promise.reject()); - }) - .catch(() => Promise.reject()); + const metadata = await fetchSingleMetadata(lookupAsset, algodClient); + const accountInfo = await algodClient + .accountInformation(walletAddress) + .do(); + + const asset = accountInfo.assets.find( + (asset: any) => asset["asset-id"]?.toString() === lookupAsset + ); + + if (!asset) throw new Error("Asset not found"); + + return createParsedTokenAccount( + walletAddress, + asset["asset-id"]?.toString(), + asset.amount, + metadata.decimals, + parseFloat(formatUnits(asset.amount, metadata.decimals)), + formatUnits(asset.amount, metadata.decimals).toString(), + metadata.symbol, + metadata.tokenName, + undefined, + false + ); }, [walletAddress] ); diff --git a/src/components/TokenSelectors/TokenPicker.tsx b/src/components/TokenSelectors/TokenPicker.tsx index a3c2c32e3..d6960d718 100644 --- a/src/components/TokenSelectors/TokenPicker.tsx +++ b/src/components/TokenSelectors/TokenPicker.tsx @@ -305,31 +305,40 @@ export default function TokenPicker({ const handleSelectOption = useCallback( async (option: NFTParsedTokenAccount) => { setSelectionError(""); - let newOption = null; try { - //Covalent balances tend to be stale, so we make an attempt to correct it at selection time. - if (getAddress && !option.isNativeAsset) { - newOption = await getAddress(option.mintKey, option.tokenId); - newOption = { - ...option, - ...newOption, - // keep logo and uri from covalent / market list / etc (otherwise would be overwritten by undefined) - logo: option.logo || newOption.logo, - uri: option.uri || newOption.uri, - } as NFTParsedTokenAccount; - } else { - newOption = option; - } - await onChange(newOption); + // covalent balances tend to be stale, so we make an attempt to correct it at selection time. + const updatedOption: Partial = + typeof getAddress === "function" && !option.isNativeAsset + ? await (async () => { + try { + const response = await getAddress( + option.mintKey, + option.tokenId + ); + return response; + } catch (error: any) { + // using stale value when updated value can't be found + if (error?.message === "Asset not found") return option; + throw error; + } + })() + : {}; + + await onChange({ + ...option, + ...updatedOption, + // keep logo and uri from covalent / market list / etc (otherwise would be overwritten by undefined) + logo: option.logo || updatedOption?.logo, + uri: option.uri || updatedOption?.uri, + } as NFTParsedTokenAccount); closeDialog(); } catch (e: any) { - if (e.message?.includes("v1")) { - setSelectionError(e.message); - } else { - setSelectionError( - "Unable to retrieve required information about this token. Ensure your wallet is connected, then refresh the list." - ); - } + console.error(e); + setSelectionError( + e?.message?.includes("v1") + ? e.message + : "Unable to retrieve required information about this token. Ensure your wallet is connected, then refresh the list." + ); } }, [getAddress, onChange, closeDialog] @@ -347,17 +356,18 @@ export default function TokenPicker({ if (!holderString) { return true; } - const optionString = ( - (option.publicKey || "") + - " " + - (option.mintKey || "") + - " " + - (option.symbol || "") + - " " + - (option.name || " ") - ).toLowerCase(); - const searchString = holderString.toLowerCase(); - return optionString.includes(searchString); + + const optionString = [ + option.publicKey, + option.mintKey, + option.symbol, + option.name, + ] + .filter(Boolean) + .join(" ") + .toLowerCase(); + + return optionString.includes(holderString.toLowerCase()); }, [holderString] ); @@ -465,7 +475,11 @@ export default function TokenPicker({ (error) => { if (!cancelled) { setLocalLoading(false); - setLoadingError("Could not find the specified address."); + console.error(error); + // leaving default "no results" + if (error?.message !== "Asset not found") { + setLoadingError("Could not find the specified address."); + } } } );