get_token_balance_erc20
Retrieve the ERC20 token balance for a specific address by providing the wallet address and token contract address, compatible with EVM networks via the EVM MCP Server.
Instructions
Get ERC20 token balance for an address
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| address | Yes | The address to check balance for | |
| network | No | Network name or chain ID. Defaults to Ethereum mainnet. | |
| tokenAddress | Yes | The ERC20 token contract address |
Implementation Reference
- src/core/tools.ts:1345-1397 (registration)Registration and handler for the 'get_token_balance_erc20' MCP tool. Defines input schema with zod and the execution logic that delegates to services.getERC20Balance.server.tool( 'get_token_balance_erc20', 'Get ERC20 token balance for an address', { address: z.string().describe('The address to check balance for'), tokenAddress: z.string().describe('The ERC20 token contract address'), network: z .string() .optional() .describe('Network name or chain ID. Defaults to Ethereum mainnet.') }, async ({ address, tokenAddress, network = 'ethereum' }) => { try { const balance = await services.getERC20Balance( tokenAddress as Address, address as Address, network ); return { content: [ { type: 'text', text: JSON.stringify( { address, tokenAddress, network, balance: { raw: balance.raw.toString(), formatted: balance.formatted, decimals: balance.token.decimals } }, null, 2 ) } ] }; } catch (error) { return { content: [ { type: 'text', text: `Error fetching ERC20 balance for ${address}: ${error instanceof Error ? error.message : String(error)}` } ], isError: true }; } } );
- src/core/services/balance.ts:98-136 (helper)Core helper function getERC20Balance that reads the ERC20 contract using viem to fetch balance, symbol, and decimals. Supports ENS resolution.export async function getERC20Balance( tokenAddressOrEns: string, ownerAddressOrEns: string, network = 'ethereum' ): Promise<{ raw: bigint; formatted: string; token: { symbol: string; decimals: number; } }> { // 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: erc20Abi, client: publicClient, }); const [balance, symbol, decimals] = await Promise.all([ contract.read.balanceOf([ownerAddress]), contract.read.symbol(), contract.read.decimals() ]); return { raw: balance, formatted: formatUnits(balance, decimals), token: { symbol, decimals } }; }
- src/core/services/balance.ts:13-35 (helper)ERC20 ABI constants used for reading token balance, symbol, and decimals in getERC20Balance.const erc20Abi = [ { inputs: [], name: 'symbol', outputs: [{ type: 'string' }], stateMutability: 'view', type: 'function' }, { inputs: [], name: 'decimals', outputs: [{ type: 'uint8' }], stateMutability: 'view', type: 'function' }, { inputs: [{ type: 'address', name: 'account' }], name: 'balanceOf', outputs: [{ type: 'uint256' }], stateMutability: 'view', type: 'function' } ] as const;