get_apt_balance
Check the APT token balance of an Aptos account by entering the account address. Returns the balance in both Octas and APT for accurate account monitoring.
Instructions
Get the native APT token balance of an Aptos account. This is used for checking the current balance of APT tokens in an account. Returns the account balance in both Octas and APT.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| account_address | Yes | Aptos account address, e.g., 0x1 or 0x742d35Cc6634C0532925a3b8D6Ac0C4db9c8b3 |
Implementation Reference
- src/tools/native/getBalance.ts:25-49 (handler)Main handler function for the get_apt_balance tool. Validates input arguments, calls the performGetAptBalance helper, and returns formatted success or error response.export async function getAptBalanceHandler(args: Record<string, any> | undefined) { if (!isGetAptBalanceArgs(args)) { throw new Error("Invalid arguments for get_apt_balance"); } const { account_address } = args; try { const results = await performGetAptBalance(account_address); return { content: [{ type: "text", text: results }], isError: false, }; } catch (error) { return { content: [ { type: "text", text: `Error getting APT balance: ${error instanceof Error ? error.message : String(error)}`, }, ], isError: true, }; } }
- src/tools/native/getBalance.ts:5-18 (schema)Tool schema definition including name, description, and input schema requiring 'account_address' string.export const GET_APT_BALANCE: Tool = { name: "get_apt_balance", description: "Get the native APT token balance of an Aptos account. This is used for checking the current balance of APT tokens in an account. Returns the account balance in both Octas and APT.", inputSchema: { type: "object", properties: { account_address: { type: "string", description: "Aptos account address, e.g., 0x1 or 0x742d35Cc6634C0532925a3b8D6Ac0C4db9c8b3", }, }, required: ["account_address"], }, };
- src/tools/native/getBalance.ts:56-84 (helper)Helper function that performs the actual APT balance query using Aptos client, formats the result, and handles account not found cases.export async function performGetAptBalance(accountAddress: string): Promise<string> { try { const aptos = getAptosClient(); // Get account APT balance const balance = await aptos.getAccountAPTAmount({ accountAddress }); return `APT Balance Information: Account: ${formatAddress(accountAddress)} Balance: ${balance} Octas Balance: ${formatAPT(balance)} APT Full Account Address: ${accountAddress}`; } catch (error) { console.error('Error getting APT balance:', error); // Check if account doesn't exist if (error instanceof Error && error.message.includes('not found')) { return `APT Balance Information: Account: ${formatAddress(accountAddress)} Balance: 0 Octas (0 APT) Status: Account does not exist or has not been initialized Note: Account needs to receive at least one transaction to be initialized on-chain`; } throw new Error(`Failed to get APT balance: ${error instanceof Error ? error.message : String(error)}`); } }
- src/tools/native/getBalance.ts:91-98 (schema)Type guard function for validating input arguments to the get_apt_balance tool.export function isGetAptBalanceArgs(args: unknown): args is { account_address: string } { return ( typeof args === "object" && args !== null && "account_address" in args && typeof (args as any).account_address === "string" ); }
- src/index.ts:37-57 (registration)Registration of the tool in the TOOLS_LIST array used for listTools response. Also imported at line 19 and dispatched in switch case at lines 120-121.const TOOLS_LIST = [ // Account tools CREATE_ACCOUNT, GET_ACCOUNT_INFO, FUND_ACCOUNT, // Native APT tools GET_APT_BALANCE, TRANSFER_APT, // Coin tools GET_COIN_BALANCE, TRANSFER_COIN, DEPLOY_COIN, MINT_COIN, REGISTER_COIN, // Transaction tools GET_TRANSACTION_STATUS, GET_ACCOUNT_TRANSACTIONS, ];