pharos-agent-kit
Version:
Connect AI Agents to Pharos protocols
49 lines • 2.19 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.get_erc20_balance = get_erc20_balance;
const viem_1 = require("viem");
const utils_1 = require("../../utils");
/**
* Get the balance of ETH or an ERC20 token for the agent's wallet
* @param agent PharosAgentKit 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 : 'ETH'} for ${contract_address ? contract_address : 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.getERC20Balance)(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