Skip to content

Commit

Permalink
[support] Algo assets are not searchable/selectable on the bridge, us…
Browse files Browse the repository at this point in the history
…er id -736691227193245717 #1003 (#1045)
  • Loading branch information
danielisaacgeslin authored Aug 30, 2024
1 parent 3405ef3 commit 7cd54fb
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 67 deletions.
58 changes: 25 additions & 33 deletions src/components/TokenSelectors/AlgoTokenPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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]
);
Expand Down
82 changes: 48 additions & 34 deletions src/components/TokenSelectors/TokenPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<NFTParsedTokenAccount> =
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]
Expand All @@ -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]
);
Expand Down Expand Up @@ -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.");
}
}
}
);
Expand Down

0 comments on commit 7cd54fb

Please sign in to comment.