fetch_balance
Retrieve wallet balances from EVM chains by providing a wallet address and chain ID to check cryptocurrency holdings.
Instructions
Get the balance of a wallet
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| walletAddress | Yes | ||
| chainId | Yes |
Implementation Reference
- src/tools/FetchBalance.ts:12-26 (handler)The main handler function that fetches the balance for a given wallet address on a specified chain using viem client.export const fetchBalance = async (options: FetchBalanceOptions) => { const { walletAddress, chainId } = options; const chain = getChainById(chainId); const client = createClient(chain); const balance = await client.getBalance({ address: walletAddress as `0x${string}`, }); return { balance: balance.toString(), formattedBalance: formatUnits(balance, chain.nativeCurrency.decimals), }; };
- src/tools/FetchBalance.ts:6-9 (schema)Zod schema defining the input options for the fetch_balance tool: walletAddress and chainId.export const FetchBalanceSchema = z.object({ walletAddress: z.string().startsWith("0x"), chainId: z.number(), });
- src/index.ts:36-38 (registration)Tool registration in the listTools handler, specifying name, description, and input schema.name: "fetch_balance", description: "Get the balance of a wallet", inputSchema: z.toJSONSchema(FetchBalanceSchema),
- src/index.ts:84-102 (registration)Dispatch handler in callToolRequest that parses arguments with schema and invokes the fetchBalance function, formatting the response.case "fetch_balance": { const args = FetchBalanceSchema.parse(request.params.arguments); const result = await fetchBalance(args); return { content: [ { type: "text", text: result.balance, description: "The balance of the wallet", }, { type: "text", text: result.formattedBalance, description: "The formatted balance of the wallet", }, ], }; }