Serdar D.
Serdar D.

Reputation: 3391

Get token amount from ETH address

I am trying to get token amount of an ETH address in the browser. I have tried it with web3.getBalance() method but it only gets ETH amount.

I need to get token amounts under an ETH address. For example, this address has more than 30 token types, I have to get the amount of a specific one: https://etherscan.io/address/0xe853c56864a2ebe4576a807d26fdc4a0ada51919

Which method should I use or is there an API that I can get that information?

Upvotes: -1

Views: 3249

Answers (2)

carver
carver

Reputation: 2349

Look up the balance of a specific token

Choose the token

this address has more than 30 token types, I have to get the amount of a specific one

I'll interpret this to mean that you already know which token you want to track.

Get the token address

Do you know what address the token is at? If not, there are several options for finding out:

  1. Find out the address from the creating team - Watch for scams / fake twitter accounts / etc
  2. Click on the token in the Etherscan dropdown and read the value from the "ERC20 Contract" field. (eg~ for OmiseGo, the contract is at 0xd26114cd6EE289AccF82350c8d8487fedB8A0C07)
  3. Look up the address via ENS by using the token symbol under thetoken.eth, like omg.thetoken.eth.
  4. Ask other trusted third parties

The most reliable method is to cross-check across multiple sources.

The most reputable teams will set up an ENS name for their token contract, though not necessarily under thetoken.eth.

Get the balance from the contract

So now you have the address of the token contract. Let's further assume that it is ERC-20 compatible.

Since you said:

I would love to hear if there is a solution in other languages

I'll use ethtoken.py* for the quickest possible example, then dive into how it works:

>>> from ethtoken import token

# Use the ENS name that points to your token contract here:
>>> omg = token("omg.thetoken.eth")

>>> omg.token_balance("0xE853c56864A2ebe4576a807D26Fdc4A0adA51919")
Decimal('246224.517546226920613329')

* ethtoken.py is a tiny open-source package I wrote.

How does ethtoken do it?

  1. Generate the ERC20 ABI from the spec
  2. Create a contract using omg = w3.eth.contract(address, abi) from Web3.py v4-beta
  3. Wrap the contract object in a ConciseContract for short contract calls
  4. Get the token balance using omg.balanceOf(address)
  5. Move the decimal place of the balance to the left this many times: omg.decimals()

Listing "all" tokens

As Adam mentioned, token balances are stored in a ledger in a contract, not as state on your account. There is (typically) one contract for each token, and there is no perfect list of all token contracts.

But let's presume that some list of tokens is good enough, like MyEtherWallet's Big List-o-Tokens.

You can iterate through the list of token addresses from that list and apply the same approach, getting the balance for each one.

Upvotes: 2

Adam Kipnis
Adam Kipnis

Reputation: 10971

Tokens are different from ETH in that they are held as state inside a smart contract as opposed to being associated directly with an address. An Ethereum address can be either a contract or an EOA (externally owned account). Both of these address types can hold ETH. Using web3.getBalance() returns the ETH associated with an address (again, that address can be either a contract or an EOA).

On the other hand, tokens are held as part of the state inside of a contract. Usually, you'll see something like mapping(address => uint256) balance in the contract itself which will store all of the balances for a specific token.

Then end result is that if you want to know the token balances of one particular address across multiple token types, you need to call the contract's balanceOf method for each token's contract address you're interested in.

Upvotes: 2

Related Questions