get-account-balance
Retrieve SOL and token balances for a specified account on the Pump.fun Solana platform to monitor cryptocurrency holdings.
Instructions
Get the SOL and token balances for an account
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| accountName | No | Name of the account to check | default |
| tokenAddress | No | Optional token address to check balance for |
Implementation Reference
- src/get-token-balance.ts:15-60 (handler)Main handler function that loads the account keypair from .keys folder, fetches SOL balance, and optionally fetches SPL token balance using getSPLBalance helper.export async function getAccountBalance( accountName: string = "default", tokenAddress?: string ): Promise<string> { try { const { connection } = initializeSDK(); const keysFolder = path.resolve(rootDir, ".keys"); const accountFilePath = path.join(keysFolder, `${accountName}.json`); if (!fs.existsSync(accountFilePath)) { throw new Error(`Account file not found for ${accountName}`); } const keypairData = JSON.parse(fs.readFileSync(accountFilePath, "utf-8")); const keypair = Keypair.fromSecretKey(new Uint8Array(keypairData)); const solBalance = await connection.getBalance(keypair.publicKey); let response = [ `Account: ${accountName} (${keypair.publicKey.toString()})`, `SOL Balance: ${solBalance / LAMPORTS_PER_SOL} SOL`, ]; if (tokenAddress) { const mintPublicKey = new PublicKey(tokenAddress); const tokenBalance = await getSPLBalance( connection, mintPublicKey, keypair.publicKey ); response.push( `Token Balance (${tokenAddress}): ${ tokenBalance !== null ? tokenBalance : "No token account found" }` ); } return response.join("\n"); } catch (error: any) { console.error("Error getting account balance:", error); return `Error getting account balance: ${ error?.message || "Unknown error" }`; } }
- src/index.ts:275-313 (registration)Tool registration with McpServer.tool(), including description, Zod input schema, and execution handler that calls the getAccountBalance function.server.tool( "get-account-balance", "Get the SOL and token balances for an account", { accountName: z .string() .default("default") .describe("Name of the account to check"), tokenAddress: z .string() .optional() .describe("Optional token address to check balance for"), }, async ({ accountName, tokenAddress }) => { try { const result = await getAccountBalance(accountName, tokenAddress); return { content: [ { type: "text", text: result, }, ], }; } catch (error: any) { console.error("Error getting account balance:", error); return { content: [ { type: "text", text: `Error getting account balance: ${ error?.message || "Unknown error" }`, }, ], }; } } );
- src/index.ts:278-287 (schema)Zod input schema defining parameters: accountName (string, default 'default') and optional tokenAddress (string).{ accountName: z .string() .default("default") .describe("Name of the account to check"), tokenAddress: z .string() .optional() .describe("Optional token address to check balance for"), },
- src/utils.ts:60-86 (helper)Helper function to retrieve the SPL token balance for a specific mint and owner public key, used when tokenAddress is provided.async function getSPLBalance( connection: Connection, mint: PublicKey, owner: PublicKey ): Promise<number | null> { try { const tokenAccounts = await connection.getParsedTokenAccountsByOwner( owner, { mint, } ); if (tokenAccounts.value.length === 0) { return null; } const tokenAccount = tokenAccounts.value[0]; const parsedInfo = tokenAccount.account.data.parsed.info; const balance = parsedInfo.tokenAmount.uiAmount; return balance; } catch (error) { console.error("Error getting SPL balance:", error); return null; } }