get_balance
Retrieve the SOL balance for a specified Solana wallet to monitor cryptocurrency holdings.
Instructions
Get SOL balance for a wallet
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| walletName | Yes | Name of the wallet |
Implementation Reference
- src/index.ts:583-603 (handler)The handler function that executes the get_balance tool logic. It retrieves the wallet by name, ensures connection, fetches the balance using Solana's connection.getBalance with timeout, converts to SOL, and returns formatted balance info.async function handleGetBalance(args: any) { const { walletName } = args; const wallet = wallets.get(walletName); if (!wallet) { throw new Error(`Wallet '${walletName}' not found`); } ensureConnection(); const balance = await withTimeout(connection.getBalance(wallet.keypair.publicKey)); const solBalance = balance / LAMPORTS_PER_SOL; return { wallet: walletName, address: wallet.keypair.publicKey.toString(), balance: { lamports: balance, sol: solBalance } }; }
- src/index.ts:117-126 (schema)The input schema definition for the get_balance tool, specifying the required 'walletName' parameter.inputSchema: { type: "object", properties: { walletName: { type: "string", description: "Name of the wallet" } }, required: ["walletName"] }
- src/index.ts:114-127 (registration)The tool registration entry in the tools array, including name, description, and input schema. This is returned by ListToolsRequestSchema.{ name: "get_balance", description: "Get SOL balance for a wallet", inputSchema: { type: "object", properties: { walletName: { type: "string", description: "Name of the wallet" } }, required: ["walletName"] } },
- src/index.ts:1294-1296 (registration)The dispatch case in the CallToolRequestSchema handler that routes 'get_balance' calls to the handleGetBalance function.case "get_balance": result = await handleGetBalance(args); break;
- src/index.ts:64-70 (helper)Helper function withTimeout used by get_balance handler to wrap connection.getBalance with a 10-second timeout.async function withTimeout<T>(promise: Promise<T>, timeoutMs: number = 10000): Promise<T> { const timeoutPromise = new Promise<never>((_, reject) => { setTimeout(() => reject(new Error('Operation timed out')), timeoutMs); }); return Promise.race([promise, timeoutPromise]); }