9

Is there a way with the Etherscan API so that you can get all the erc20 token balances for a specific address?

As explained here, you can see them with their web UI (the token tracker dropdown). Does their API provide equivalent functionality? As far as I have seen, you can only query for a specific token of which you already have the address. I need functionality equivalent to Ethplorer's API endopint getAddressInfo/{address}.

  • 3
    did you found a way to do it? – Eduardo Jan 12 '18 at 15:48
  • I had the same issue and solved it using web scraping. You can find my code here: github.com/aaronk6/ERC20-MoneyMoney Additionally, I’ve contacted Etherscan support to find out if they plan to add those routes to their API. Let’s see. – aaronk6 Mar 12 '18 at 20:31
5

As previously mentioned it doesn't appear possible using etherscan. But it's possible using the (free) ethexplorer.io API.

The format is:

http://api.ethplorer.io/getAddressInfo/[YOUR_ADDRESS]?apiKey=freekey

For example:

http://api.ethplorer.io/getAddressInfo/0x32Be343B94f860124dC4fEe278FDCBD38C102D88?apiKey=freekey

API documentation is here:

https://github.com/EverexIO/Ethplorer/wiki/Ethplorer-API

Website:

https://ethplorer.io

2

This is not possible with their current API.

Depending on your project, a workaround could be web scraping. Here’s an example using Lua and xpath:

Get all contract addresses for which the given ETH address holds tokens:

function requestContractAddressesForEthAddress(ethAddress)

  -- No API method for this (as of Mar 11, 2018), therefore using web scraping

  local connection = Connection()
  local html = HTML(connection:get("https://etherscan.io/address/" .. ethAddress))
  local elements = html:xpath("//ul[@id='balancelist']/li/a")
  local addresses = {}

  elements:each(function (index, element)
    local href = element:attr('href')
    local address = string.match(href, "^%/token%/(0x[0-9a-fA-F]+)")
    table.insert(addresses, address)
  end)

  return addresses
end

Get info on a token (name, USD price, divisor to calculate correct amount) for a given contract address:

function requestTokenInfo(contractAddress)

  -- No API method for this (as of Mar 11, 2018), therefore using web scraping

  local connection = Connection()
  local html = HTML(connection:get("https://etherscan.io/token/" .. contractAddress))
  local name = html:xpath("//*[@id='address']"):text()
  local summary = html:xpath("//*[@id='ContentPlaceHolder1_divSummary']"):text()
  local decimals = tonumber(string.match(summary, "Token Decimals:%s+([%d,]+)"))

  return {
    name = name,
    price = tonumber(string.match(summary, "Value per Token:%s+$([%d%.,]+)")),
    divisor = math.pow(10, decimals)
  }
end

Please be aware that web scraping is considered bad practice. This can break without notice, as it is obviously relying on how the frontend is rendered.

I’ve contacted Etherscan.io support on Mar 12, 2018 to find out if those API calls are on their roadmap. The answer was that providing paid custom APIs services is not their short-term focus.

1

You can do this with any node, no external API needed, with the help of a smart contract. I just recently wrote a smart contract and library for this, and put it into an NPM package. You can check it out here: https://www.npmjs.com/package/eth-balance-checker

The call looks like this:

import Web3 from 'web3';
import { getAddressBalances } from 'eth-balance-checker/lib/web3';

const web3 = new Web3(...);
const address = '0x123...';
const tokens = ['0x0', '0x456...'];
getAddressBalances(web3, address, tokens).then(balances => {
  console.log(balances); // { "0x0": "100", "0x456...": "200" }
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.