Skip to main content
Glama

get_erc20_balance

Retrieve ERC20 token balances for a specific address on supported networks, including BSC, Ethereum, and others, by providing the token contract address and network details.

Instructions

Get ERC20 token balance for an address

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
addressYesThe address to check balance for
networkNoNetwork name (e.g. 'bsc', 'opbnb', 'ethereum', 'base', etc.) or chain ID. Supports others main popular networks. Defaults to BSC mainnet.bsc
tokenAddressYesThe ERC20 token contract address

Implementation Reference

  • The core handler function that resolves addresses, fetches ERC20 balance, symbol, and decimals from the blockchain, and formats the result.
    export async function getERC20Balance(
      tokenAddressOrEns: string,
      ownerAddressOrEns: string,
      network = "ethereum"
    ): Promise<{
      raw: bigint
      formatted: string
      symbol: string
      decimals: number
      network: string
      tokenAddress: Address
      ownerAddress: Address
    }> {
      // Resolve ENS names to addresses if needed
      const tokenAddress = await resolveAddress(tokenAddressOrEns, network)
      const ownerAddress = await resolveAddress(ownerAddressOrEns, network)
    
      const publicClient = getPublicClient(network)
    
      const contract = getContract({
        address: tokenAddress,
        abi: ERC20_ABI,
        client: publicClient
      })
    
      const [balance, symbol, decimals] = await Promise.all([
        contract.read.balanceOf([ownerAddress]) as Promise<bigint>,
        contract.read.symbol() as Promise<string>,
        contract.read.decimals() as Promise<number>
      ])
    
      return {
        raw: balance,
        formatted: formatUnits(balance, decimals),
        symbol,
        decimals,
        network,
        tokenAddress,
        ownerAddress
      }
    }
  • Registers the 'get_erc20_balance' MCP tool, including input schema validation with Zod and a wrapper handler that calls the core getERC20Balance service.
    server.tool(
      "get_erc20_balance",
      "Get ERC20 token balance for an address",
      {
        tokenAddress: z.string().describe("The ERC20 token contract address"),
        address: z.string().describe("The address to check balance for"),
        network: defaultNetworkParam,
        privateKey: privateKeyParam
      },
      async ({ network, tokenAddress, address, privateKey }) => {
        try {
          const res = await services.getERC20Balance(
            tokenAddress as Address,
            address || privateKeyToAccount(privateKey as Hex).address,
            network
          )
    
          return mcpToolRes.success(res)
        } catch (error) {
          return mcpToolRes.error(error, "fetching ERC20 token balance")
        }
      }
    )

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/bnb-chain/bnbchain-mcp'

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