You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在浏览器中获取ETH地址的指定代币余额?

How to Get ERC20 Token Balance for an Ethereum Address

Got it, let's break this down clearly. The web3.getBalance() method only works for native Ethereum (ETH) because that's its explicit purpose—ERC20 and other standard tokens are managed by their own independent smart contracts, so you need to interact directly with those contracts to fetch a user's token balance.

Here are the two most reliable approaches to get a specific token's balance:

1. Directly Call the Token Contract's balanceOf Method via Web3

This is the most decentralized way, as you're interacting directly with the Ethereum blockchain. Here's how to do it:

  • First, grab the contract address of the token you care about (you can find this on any Ethereum block browser, like the token's detail page linked from the address you mentioned).
  • Use a minimal ERC20 ABI snippet—you don't need the full contract ABI, just the part for the balanceOf and decimals methods (since tokens use different decimal places):
    [
      {
        "constant": true,
        "inputs": [{"name": "_owner", "type": "address"}],
        "name": "balanceOf",
        "outputs": [{"name": "", "type": "uint256"}],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
      },
      {
        "constant": true,
        "inputs": [],
        "name": "decimals",
        "outputs": [{"name": "", "type": "uint8"}],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
      }
    ]
    
  • Initialize the contract with your Web3 instance and call the methods:
    // Assume you've already set up a Web3 instance connected to an Ethereum mainnet node
    const targetAddress = "0xe853c56864a2ebe4576a807d26fdc4a0ada51919";
    const tokenContractAddress = "0x..."; // Replace with your token's contract address
    const tokenAbi = [/* Paste the minimal ABI above here */];
    
    const tokenContract = new web3.eth.Contract(tokenAbi, tokenContractAddress);
    
    // Fetch raw balance (uint256 value, not human-readable)
    const rawBalance = await tokenContract.methods.balanceOf(targetAddress).call();
    // Fetch token decimals to convert to readable format
    const decimals = await tokenContract.methods.decimals().call();
    // Convert to human-readable balance
    const readableBalance = rawBalance / (10 ** decimals);
    
    Important: The raw balance returned is a big integer representing the smallest unit of the token (e.g., wei for ETH, gwei for some tokens). Dividing by 10^decimals gives you the balance you'd see on a block browser.

2. Use a Third-Party Node Service API

If you want to avoid handling contract calls manually, most Ethereum node providers offer wrapped APIs that simplify fetching token balances. These services typically have endpoints like getTokenBalances where you can pass the target address and token contract address, and they'll return the formatted balance directly.

Additionally, block browser APIs also provide this functionality—you can use their token balance endpoint by supplying the address and token contract address, along with an API key (which you'll need to register for).

Key Notes

  • Make sure your Web3 instance is connected to a fully synced Ethereum mainnet node to get up-to-date balances.
  • For non-ERC20 tokens (like ERC721 NFTs or ERC1155 multi-token contracts), the method differs:
    • ERC721: balanceOf returns the number of NFTs held, but you'll need tokenOfOwnerByIndex to get specific token IDs.
    • ERC1155: Use balanceOf with both the target address and token ID as parameters.
  • If you need balances for multiple tokens at once, third-party APIs are more efficient than calling each contract individually.

内容的提问来源于stack exchange,提问作者Serdar D.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:36:53