UNPKG

@shogun-sdk/money-legos

Version:

Shogun Money Legos: clients and types for quotes, memes, prices, balances, fees, validations, etc.

61 lines (55 loc) 1.72 kB
import { erc20Abi } from 'viem'; import { getEvmJsonRpcProvider } from '../utils/rpc.js'; import { berachain } from 'viem/chains'; type TokenData = { balance: bigint | undefined; decimals: number | undefined; symbol: string | undefined; name: string | undefined; }; export const getTokenData = async ( tokenAddress: string, chainId: number, walletAddress?: string, ): Promise<TokenData> => { const publicClient = getEvmJsonRpcProvider(chainId); // Fetch token data using multicall const results = await publicClient.multicall({ // @ts-ignore contracts: [ ...(walletAddress ? [ { address: tokenAddress as `0x${string}`, abi: erc20Abi, functionName: 'balanceOf', args: [walletAddress as `0x${string}`], }, ] : []), { address: tokenAddress as `0x${string}`, abi: erc20Abi, functionName: 'decimals', }, { address: tokenAddress as `0x${string}`, abi: erc20Abi, functionName: 'symbol', }, { address: tokenAddress as `0x${string}`, abi: erc20Abi, functionName: 'name', }, ], multicallAddress: chainId === berachain.id ? '0x8adce287716648df409faccbbecf76fe25c8fe22' : undefined, }); const [balance, decimals, symbol, name] = walletAddress ? results : [undefined, ...results]; return { balance: balance?.result ? BigInt(balance.result as string) : undefined, decimals: decimals?.result ? Number(decimals.result) : undefined, symbol: symbol?.result ? String(symbol.result) : undefined, name: name?.result ? String(name.result) : undefined, }; };