get-balance
Check the balance of any address on EVM-compatible chains like Ethereum, Polygon, or BSC. Input chain and address details to retrieve real-time or historical wallet balances.
Instructions
Get the balance of an address on any EVM-compatible chain
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| address | Yes | Address to check balance for | |
| blockTag | No | Block tag (latest, earliest, pending, or block number) | latest |
| chain | Yes | Chain identifier. Available: ethereum, polygon, bsc, arbitrum, optimism, avalanche, fantom, sepolia |
Implementation Reference
- src/rpc-service/index.ts:309-369 (handler)Handler function that retrieves the balance of a given address on the specified EVM chain using the ethers provider.async ({ chain, address, blockTag = "latest" }) => { try { const chainConfig = DEFAULT_CHAINS[chain.toLowerCase()]; if (!chainConfig) { return { content: [{ type: "text", text: `Error: Unsupported chain "${chain}". Available chains: ${Object.keys(DEFAULT_CHAINS).join(', ')}` }], isError: true }; } if (!ethers.isAddress(address)) { return { content: [{ type: "text", text: `Error: Invalid address: ${address}` }], isError: true }; } const provider = this.providers.get(chain.toLowerCase()); if (!provider) { return { content: [{ type: "text", text: `Error: Provider not initialized for chain: ${chain}` }], isError: true }; } const balance = await provider.getBalance(address, blockTag); return { content: [{ type: "text", text: `Balance Information: 🔗 Chain: ${chainConfig.name} (${chainConfig.chainId}) 👤 Address: ${address} 🏷️ Block: ${blockTag} 💰 Balance: ${ethers.formatEther(balance)} ${chainConfig.symbol} 🔢 Wei: ${balance.toString()} ${chainConfig.explorerUrl ? `🔍 Explorer: ${chainConfig.explorerUrl}/address/${address}` : ''}` }] }; } catch (error) { return { content: [{ type: "text", text: `Error getting balance: ${error instanceof Error ? error.message : String(error)}` }], isError: true }; } }
- src/rpc-service/index.ts:300-308 (schema)Input schema definition for the get-balance tool using Zod, specifying chain, address, and optional blockTag.{ title: "Get Balance", description: "Get the balance of an address on any EVM-compatible chain", inputSchema: { chain: z.string().describe(`Chain identifier. Available: ${Object.keys(DEFAULT_CHAINS).join(', ')}`), address: z.string().describe("Address to check balance for"), blockTag: z.string().optional().describe("Block tag (latest, earliest, pending, or block number)").default("latest") } },
- src/rpc-service/index.ts:298-370 (registration)Registration of the 'get-balance' tool with the MCP server, including schema and handler function.server.registerTool( "get-balance", { title: "Get Balance", description: "Get the balance of an address on any EVM-compatible chain", inputSchema: { chain: z.string().describe(`Chain identifier. Available: ${Object.keys(DEFAULT_CHAINS).join(', ')}`), address: z.string().describe("Address to check balance for"), blockTag: z.string().optional().describe("Block tag (latest, earliest, pending, or block number)").default("latest") } }, async ({ chain, address, blockTag = "latest" }) => { try { const chainConfig = DEFAULT_CHAINS[chain.toLowerCase()]; if (!chainConfig) { return { content: [{ type: "text", text: `Error: Unsupported chain "${chain}". Available chains: ${Object.keys(DEFAULT_CHAINS).join(', ')}` }], isError: true }; } if (!ethers.isAddress(address)) { return { content: [{ type: "text", text: `Error: Invalid address: ${address}` }], isError: true }; } const provider = this.providers.get(chain.toLowerCase()); if (!provider) { return { content: [{ type: "text", text: `Error: Provider not initialized for chain: ${chain}` }], isError: true }; } const balance = await provider.getBalance(address, blockTag); return { content: [{ type: "text", text: `Balance Information: 🔗 Chain: ${chainConfig.name} (${chainConfig.chainId}) 👤 Address: ${address} 🏷️ Block: ${blockTag} 💰 Balance: ${ethers.formatEther(balance)} ${chainConfig.symbol} 🔢 Wei: ${balance.toString()} ${chainConfig.explorerUrl ? `🔍 Explorer: ${chainConfig.explorerUrl}/address/${address}` : ''}` }] }; } catch (error) { return { content: [{ type: "text", text: `Error getting balance: ${error instanceof Error ? error.message : String(error)}` }], isError: true }; } } );
- src/rpc-service/index.ts:75-80 (helper)Constructor initializes ethers.JsonRpcProvider instances for each supported chain, used by the get-balance handler.constructor() { // Initialize providers for all default chains for (const [key, config] of Object.entries(DEFAULT_CHAINS)) { this.providers.set(key, new ethers.JsonRpcProvider(config.rpcUrl)); } }
- src/rpc-service/index.ts:13-70 (helper)Configuration object defining supported chains with RPC URLs, chain IDs, symbols, and explorer URLs, used to select provider and format responses in get-balance.const DEFAULT_CHAINS: Record<string, ChainConfig> = { ethereum: { name: "Ethereum Mainnet", chainId: 1, rpcUrl: "https://rpc.ankr.com/eth", symbol: "ETH", explorerUrl: "https://etherscan.io" }, polygon: { name: "Polygon", chainId: 137, rpcUrl: "https://rpc.ankr.com/polygon", symbol: "MATIC", explorerUrl: "https://polygonscan.com" }, bsc: { name: "BSC", chainId: 56, rpcUrl: "https://rpc.ankr.com/bsc", symbol: "BNB", explorerUrl: "https://bscscan.com" }, arbitrum: { name: "Arbitrum One", chainId: 42161, rpcUrl: "https://rpc.ankr.com/arbitrum", symbol: "ETH", explorerUrl: "https://arbiscan.io" }, optimism: { name: "Optimism", chainId: 10, rpcUrl: "https://rpc.ankr.com/optimism", symbol: "ETH", explorerUrl: "https://optimistic.etherscan.io" }, avalanche: { name: "Avalanche C-Chain", chainId: 43114, rpcUrl: "https://rpc.ankr.com/avalanche", symbol: "AVAX", explorerUrl: "https://snowtrace.io" }, fantom: { name: "Fantom", chainId: 250, rpcUrl: "https://rpc.ankr.com/fantom", symbol: "FTM", explorerUrl: "https://ftmscan.com" }, sepolia: { name: "Sepolia Testnet", chainId: 11155111, rpcUrl: "https://rpc.ankr.com/eth_sepolia", symbol: "ETH", explorerUrl: "https://sepolia.etherscan.io" } };