@coinbase/agentkit
Version:
Coinbase AgentKit core primitives
55 lines (54 loc) • 1.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTokenDetails = getTokenDetails;
const viem_1 = require("viem");
/**
* Gets the details of an ERC20 token including name, decimals, and balance.
*
* @param walletProvider - The wallet provider to use for the multicall.
* @param contractAddress - The contract address of the ERC20 token.
* @param address - The address to check the balance for. If not provided, uses the wallet's address.
* @returns A promise that resolves to TokenDetails or null if there's an error.
*/
async function getTokenDetails(walletProvider, contractAddress, address) {
try {
const results = await walletProvider.getPublicClient().multicall({
contracts: [
{
address: contractAddress,
abi: viem_1.erc20Abi,
functionName: "name",
args: [],
},
{
address: contractAddress,
abi: viem_1.erc20Abi,
functionName: "decimals",
args: [],
},
{
address: contractAddress,
abi: viem_1.erc20Abi,
functionName: "balanceOf",
args: [(address || walletProvider.getAddress())],
},
],
});
const name = results[0].result;
const decimals = results[1]?.result;
const balance = results[2]?.result;
if (balance === undefined || decimals === undefined || name === undefined) {
return null;
}
const formattedBalance = (0, viem_1.formatUnits)(BigInt(balance), decimals);
return {
name,
decimals,
balance: BigInt(balance),
formattedBalance,
};
}
catch {
return null;
}
}