Skip to main content
Glama

Aptos Blockchain MCP

getCoinBalance.ts•4.41 kB
import { Tool } from "@modelcontextprotocol/sdk/types.js"; import { getAptosClient } from "../../config.js"; import { formatAddress, formatCoinAmount } from "../../utils/format.js"; export const GET_COIN_BALANCE: Tool = { name: "get_coin_balance", description: "Get the balance of a specific coin type for an Aptos account. This is used for checking the balance of custom coins or tokens. Returns the coin balance and metadata.", inputSchema: { type: "object", properties: { account_address: { type: "string", description: "Aptos account address, e.g., 0x1 or 0x742d35Cc6634C0532925a3b8D6Ac0C4db9c8b3", }, coin_type: { type: "string", description: "Coin type identifier, e.g., '0x1::aptos_coin::AptosCoin' or custom coin type", }, }, required: ["account_address", "coin_type"], }, }; /** * Gets the balance of a specific coin type for an account * @param args The arguments containing account address and coin type * @returns The coin balance information */ export async function getCoinBalanceHandler(args: Record<string, any> | undefined) { if (!isGetCoinBalanceArgs(args)) { throw new Error("Invalid arguments for get_coin_balance"); } const { account_address, coin_type } = args; try { const results = await performGetCoinBalance(account_address, coin_type); return { content: [{ type: "text", text: results }], isError: false, }; } catch (error) { return { content: [ { type: "text", text: `Error getting coin balance: ${error instanceof Error ? error.message : String(error)}`, }, ], isError: true, }; } } /** * Gets the balance of a specific coin type for an account * @param accountAddress The Aptos account address * @param coinType The coin type identifier * @returns The coin balance information as a formatted string */ export async function performGetCoinBalance(accountAddress: string, coinType: string): Promise<string> { try { const aptos = getAptosClient(); // Get account coin balance const balance = await aptos.getAccountCoinAmount({ accountAddress, coinType: coinType as `${string}::${string}::${string}`, }); // Try to get coin info for additional details let coinInfo = null; try { // Note: getCoinInfo might not be available in all SDK versions // coinInfo = await aptos.getCoinInfo({ coinType }); } catch (error) { // Coin info might not be available for all coins console.warn('Could not fetch coin info:', error); } let result = `Coin Balance Information: Account: ${formatAddress(accountAddress)} Coin Type: ${coinType} Balance: ${balance}`; // Note: Coin info functionality is commented out due to SDK compatibility // if (coinInfo) { // const formattedBalance = formatCoinAmount(balance, coinInfo.decimals); // result += ` // Formatted Balance: ${formattedBalance} ${coinInfo.symbol} // Coin Name: ${coinInfo.name} // Coin Symbol: ${coinInfo.symbol} // Decimals: ${coinInfo.decimals}`; // } result += ` Full Account Address: ${accountAddress}`; return result; } catch (error) { console.error('Error getting coin balance:', error); // Check if account doesn't exist or doesn't have this coin if (error instanceof Error && (error.message.includes('not found') || error.message.includes('does not exist'))) { return `Coin Balance Information: Account: ${formatAddress(accountAddress)} Coin Type: ${coinType} Balance: 0 Status: Account does not have this coin type or account does not exist Note: Account needs to register for this coin type before receiving it`; } throw new Error(`Failed to get coin balance: ${error instanceof Error ? error.message : String(error)}`); } } /** * Checks if the provided arguments are valid for the getCoinBalance tool * @param args The arguments to check * @returns True if the arguments are valid, false otherwise */ export function isGetCoinBalanceArgs(args: unknown): args is { account_address: string; coin_type: string } { return ( typeof args === "object" && args !== null && "account_address" in args && typeof (args as any).account_address === "string" && "coin_type" in args && typeof (args as any).coin_type === "string" ); }

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/punkpeye/aptos-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server