UNPKG

sei-agent-kit

Version:

A package for building AI agents on the SEI blockchain

49 lines 2.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.get_erc20_balance = get_erc20_balance; const viem_1 = require("viem"); const utils_1 = require("../../utils"); /** * Get ERC-20 token balance for a wallet * @param agent SeiAgentKit instance * @param contract_address Optional ERC-20 token contract address * @returns Promise with formatted balance as string */ async function get_erc20_balance(agent, contract_address) { console.log(`Querying balance of ${contract_address ? contract_address : 'SEI'} for ${agent.wallet_address}...`); try { if (!contract_address) { // Handle native token balance if (!agent.publicClient) { throw new Error("Public client not initialized"); } if (!agent.wallet_address) { throw new Error("Wallet address not specified"); } const balance = await agent.publicClient.getBalance({ address: agent.wallet_address }); return (0, viem_1.formatEther)(balance); } // Handle ERC-20 token balance if (!agent.publicClient || !agent.wallet_address) { throw new Error("Public client or wallet address not initialized"); } const balance = await (0, utils_1.getBalance)(agent, contract_address); if (balance === null || balance === undefined) { throw new Error(`Failed to retrieve balance for contract: ${contract_address}`); } const decimals = await (0, utils_1.getTokenDecimals)(agent, contract_address); if (decimals === null || decimals === undefined) { throw new Error(`Failed to retrieve token decimals for contract: ${contract_address}`); } const formatBalance = (0, utils_1.formatWei)(Number(balance), decimals); return formatBalance.toString() || '0'; } catch (error) { const errorMsg = error instanceof Error ? error?.message : String(error); console.error(errorMsg); throw error; } } //# sourceMappingURL=balance.js.map