get_balance
Retrieve the balance of a wallet address for native or ERC20 tokens on the Rootstock blockchain using standardized APIs from the MCP Server.
Instructions
Get the balance of a wallet address (native tokens or ERC20 tokens)
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| address | Yes | Wallet address to check balance for | |
| tokenAddress | No | Optional ERC20 token contract address |
Input Schema (JSON Schema)
{
"properties": {
"address": {
"description": "Wallet address to check balance for",
"type": "string"
},
"tokenAddress": {
"description": "Optional ERC20 token contract address",
"type": "string"
}
},
"required": [
"address"
],
"type": "object"
}
Implementation Reference
- src/smithery-server.ts:182-223 (handler)MCP tool registration, input schema, and handler for 'get_balance'. Handles both native and ERC20 token balances by delegating to RootstockClient methods.server.tool( "get_balance", "Get the balance of a wallet address (native tokens or ERC20 tokens)", { address: z.string().describe("Wallet address to check balance for"), tokenAddress: z.string().optional().describe("Optional ERC20 token contract address"), }, async ({ address, tokenAddress }) => { try { if (tokenAddress) { const tokenBalance = await rootstockClient.getTokenBalance(address, tokenAddress); return { content: [ { type: "text", text: `Token Balance:\n\nAddress: ${address}\nToken: ${tokenBalance.name} (${tokenBalance.symbol})\nBalance: ${tokenBalance.balance} ${tokenBalance.symbol}`, }, ], }; } else { const balance = await rootstockClient.getBalance(address); return { content: [ { type: "text", text: `Native Balance:\n\nAddress: ${address}\nBalance: ${balance} ${rootstockClient.getCurrencySymbol()}`, }, ], }; } } catch (error) { return { content: [ { type: "text", text: `Error getting balance: ${error instanceof Error ? error.message : String(error)}`, }, ], }; } } );
- src/rootstock-client.ts:75-82 (helper)Core helper function to fetch native token (tRBTC) balance using ethers.JsonRpcProvider.getBalance and format to ether units.async getBalance(address: string): Promise<string> { try { const balance = await this.getProvider().getBalance(address); return ethers.formatEther(balance); } catch (error) { throw new Error(`Failed to get balance: ${error}`); } }
- src/rootstock-client.ts:87-117 (helper)Core helper function to fetch ERC20 token balance, including token metadata (name, symbol, decimals), using direct contract calls.async getTokenBalance(address: string, tokenAddress: string): Promise<TokenBalance> { try { const tokenContract = new ethers.Contract( tokenAddress, [ 'function balanceOf(address) view returns (uint256)', 'function decimals() view returns (uint8)', 'function symbol() view returns (string)', 'function name() view returns (string)', ], this.getProvider() ); const [balance, decimals, symbol, name] = await Promise.all([ tokenContract.balanceOf(address), tokenContract.decimals(), tokenContract.symbol(), tokenContract.name(), ]); return { tokenAddress, balance: ethers.formatUnits(balance, decimals), decimals, symbol, name, }; } catch (error) { throw new Error(`Failed to get token balance: ${error}`); } }
- src/smithery-server.ts:182-223 (registration)Registration of the 'get_balance' tool in the MCP server.server.tool( "get_balance", "Get the balance of a wallet address (native tokens or ERC20 tokens)", { address: z.string().describe("Wallet address to check balance for"), tokenAddress: z.string().optional().describe("Optional ERC20 token contract address"), }, async ({ address, tokenAddress }) => { try { if (tokenAddress) { const tokenBalance = await rootstockClient.getTokenBalance(address, tokenAddress); return { content: [ { type: "text", text: `Token Balance:\n\nAddress: ${address}\nToken: ${tokenBalance.name} (${tokenBalance.symbol})\nBalance: ${tokenBalance.balance} ${tokenBalance.symbol}`, }, ], }; } else { const balance = await rootstockClient.getBalance(address); return { content: [ { type: "text", text: `Native Balance:\n\nAddress: ${address}\nBalance: ${balance} ${rootstockClient.getCurrencySymbol()}`, }, ], }; } } catch (error) { return { content: [ { type: "text", text: `Error getting balance: ${error instanceof Error ? error.message : String(error)}`, }, ], }; } } );
- src/smithery-server.ts:186-188 (schema)Input schema for the get_balance tool using Zod validation.address: z.string().describe("Wallet address to check balance for"), tokenAddress: z.string().optional().describe("Optional ERC20 token contract address"), },