Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 36 additions & 5 deletions packages/agent-sdk/src/evm/erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,42 @@ export async function checkAllowance(
});
}

const NON_ETH_NATIVES: Record<number, { symbol: string; name: string }> = {
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<TokenInfo> {
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,
};
}

Expand Down Expand Up @@ -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<string> {
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}`);
}
}
1 change: 1 addition & 0 deletions packages/agent-sdk/src/evm/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export interface TokenInfo {
address: Address;
decimals: number;
symbol: string;
name: string;
}
11 changes: 10 additions & 1 deletion packages/agent-sdk/tests/evm/token.spec.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 () => {
Expand Down