Skip to main content
Glama
EmanuelJr
by EmanuelJr

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
NameRequiredDescriptionDefault
walletAddressYes
chainIdYes

Implementation Reference

  • 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),
      };
    };
  • 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",
          },
        ],
      };
    }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/EmanuelJr/web3-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server