From bb29c0d005473a93a75e9ee0d8ad0c83dda46578 Mon Sep 17 00:00:00 2001 From: Benjamin Smith Date: Thu, 3 Jul 2025 09:54:54 +0200 Subject: [PATCH] Add Token Names to Token Fetcher --- packages/agent-sdk/src/evm/erc20.ts | 41 +++++++++++++++++++--- packages/agent-sdk/src/evm/types.ts | 1 + packages/agent-sdk/tests/evm/token.spec.ts | 11 +++++- 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/packages/agent-sdk/src/evm/erc20.ts b/packages/agent-sdk/src/evm/erc20.ts index bb9a872..aaa053a 100644 --- a/packages/agent-sdk/src/evm/erc20.ts +++ b/packages/agent-sdk/src/evm/erc20.ts @@ -58,27 +58,42 @@ export async function checkAllowance( }); } +const NON_ETH_NATIVES: Record = { + 100: { symbol: "xDAI", name: "xDAI" }, + 137: { symbol: "MATIC", name: "MATIC" }, + 43114: { symbol: "AVAX", name: "AVAX" }, +}; + +const ETHER_NATIVE = { + decimals: 18, + // Not all Native Assets are ETH, but enough are. + symbol: "ETH", + name: "Ether", +}; + export async function getTokenInfo( chainId: number, - address: Address, + address?: Address, ): Promise { - if (address.toLowerCase() === NATIVE_ASSET.toLowerCase()) { + if (!address || address.toLowerCase() === NATIVE_ASSET.toLowerCase()) { + const native = NON_ETH_NATIVES[chainId] || ETHER_NATIVE; return { address: NATIVE_ASSET, decimals: 18, - // Not all Native Assets are ETH, but enough are. - symbol: "ETH", + ...native, }; } - const [decimals, symbol] = await Promise.all([ + const [decimals, symbol, name] = await Promise.all([ getTokenDecimals(chainId, address), getTokenSymbol(chainId, address), + getTokenName(chainId, address), ]); return { address, decimals, symbol, + name, }; } @@ -113,3 +128,19 @@ export async function getTokenSymbol( throw new Error(`Error fetching token decimals: ${error}`); } } + +export async function getTokenName( + chainId: number, + address: Address, +): Promise { + const client = getClientForChain(chainId); + try { + return await client.readContract({ + address, + abi: erc20Abi, + functionName: "name", + }); + } catch (error: unknown) { + throw new Error(`Error fetching token name: ${error}`); + } +} diff --git a/packages/agent-sdk/src/evm/types.ts b/packages/agent-sdk/src/evm/types.ts index 57503f4..b15cff1 100644 --- a/packages/agent-sdk/src/evm/types.ts +++ b/packages/agent-sdk/src/evm/types.ts @@ -4,4 +4,5 @@ export interface TokenInfo { address: Address; decimals: number; symbol: string; + name: string; } diff --git a/packages/agent-sdk/tests/evm/token.spec.ts b/packages/agent-sdk/tests/evm/token.spec.ts index 6a34faf..55612d6 100644 --- a/packages/agent-sdk/tests/evm/token.spec.ts +++ b/packages/agent-sdk/tests/evm/token.spec.ts @@ -1,5 +1,5 @@ import { zeroAddress } from "viem"; -import { getTokenDetails } from "../../src"; +import { getTokenDetails, getTokenInfo } from "../../src"; describe("getTokenDetails", () => { it("should fail to get token details for zero address", async () => { await expect(getTokenDetails(100, zeroAddress)).rejects.toThrow(); // or .rejects.toThrow("specific error message") if you want to check the message @@ -21,9 +21,18 @@ describe("getTokenDetails", () => { expect(tokenDetails).toStrictEqual({ address: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", + name: "Ether", decimals: 18, symbol: "ETH", }); + + const xDai = await getTokenInfo(100); + expect(xDai).toEqual({ + address: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", + name: "xDAI", + decimals: 18, + symbol: "xDAI", + }); }); it("should return the token details for a given symbol", async () => {