token.ts
1 import { assert } from "@std/assert"; 2 import { Address, PublicClient, toBytes, zeroAddress } from "viem"; 3 import { abi, tokenAddresses } from "@massmarket/contracts"; 4 5 // Any utility functions for tokens 6 export const getTokenAddress = ( 7 symbol: string, 8 chainId: number, 9 ): Uint8Array => { 10 if (symbol === "ETH") return toBytes(zeroAddress); 11 if (symbol === "EDD") return toBytes(abi.eddiesAddress); 12 const addresses: { 13 [key: string]: { 14 [key: string]: string; 15 }; 16 } = tokenAddresses; 17 const tokenAddress = addresses[chainId][symbol] as Address; 18 19 if (!tokenAddress) { 20 throw new Error(`Token not found for ${symbol} on chainId: ${chainId}`); 21 } 22 return toBytes(tokenAddress); 23 }; 24 25 export const getTokenInformation = ( 26 publicClient: PublicClient, 27 tokenAddress: `0x${string}`, 28 ): Promise<[string, number]> => { 29 assert(publicClient.chain, "publicClient.chain is undefined"); 30 if (tokenAddress === zeroAddress) { 31 return new Promise((resolve) => { 32 resolve(["ETH", 18]); 33 }); 34 } 35 const symbol = publicClient.readContract({ 36 address: tokenAddress, 37 abi: abi.eddiesAbi, 38 functionName: "symbol", 39 args: [], 40 }) as Promise<string>; 41 const decimal = publicClient.readContract({ 42 address: tokenAddress, 43 abi: abi.eddiesAbi, 44 functionName: "decimals", 45 args: [], 46 }) as Promise<number>; 47 return Promise.all([symbol, decimal]); 48 };