Skip to content

Commit

Permalink
✨ preview: decode token amounts
Browse files Browse the repository at this point in the history
  • Loading branch information
cruzdanilo committed Dec 3, 2024
1 parent 4a7d6d1 commit bffbb90
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions developer-preview/src/lib/getPreviewData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
type AbiFunction,
createPublicClient,
decodeFunctionData,
erc20Abi,
getAbiItem,
http,
isAddress,
Expand Down Expand Up @@ -56,6 +57,28 @@ const processFields = (
Number(value) / 10 ** Number(field.params?.decimals ?? 0)
}${field.params?.base ? ` ${String(field.params.base)}` : ""}`;
break;
case "tokenAmount":
if (typeof field.params?.tokenPath === "string") {
const token = get(values, field.params.tokenPath);
if (isHex(token) && isAddress(token)) {
try {
const [decimals, symbol] = await Promise.all([
publicClient.readContract({
abi: erc20Abi,
address: token,
functionName: "decimals",
}),
publicClient.readContract({
abi: erc20Abi,
address: token,
functionName: "symbol",
}),
]);
displayValue = `${Number(value) / 10 ** decimals}${symbol ? ` ${symbol}` : ""}`;
break;
} catch {}
}
}
case "addressName":
if (
typeof value === "string" &&
Expand Down

0 comments on commit bffbb90

Please sign in to comment.