From bffbb908af8d19bbecf7ca85712ddc061347699c Mon Sep 17 00:00:00 2001 From: danilo neves cruz Date: Sat, 30 Nov 2024 23:09:25 +0700 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20preview:=20decode=20token=20amounts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- developer-preview/src/lib/getPreviewData.tsx | 23 ++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/developer-preview/src/lib/getPreviewData.tsx b/developer-preview/src/lib/getPreviewData.tsx index bdb05f9..607d248 100644 --- a/developer-preview/src/lib/getPreviewData.tsx +++ b/developer-preview/src/lib/getPreviewData.tsx @@ -2,6 +2,7 @@ import { type AbiFunction, createPublicClient, decodeFunctionData, + erc20Abi, getAbiItem, http, isAddress, @@ -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" &&