Reputation: 3391
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
Reputation: 2349
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.
Do you know what address the token is at? If not, there are several options for finding out:
0xd26114cd6EE289AccF82350c8d8487fedB8A0C07
)thetoken.eth
, like omg.thetoken.eth
.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
.
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.
ethtoken
do it?omg = w3.eth.contract(address, abi)
from Web3.py v4-betaConciseContract
for short contract callsomg.balanceOf(address)
omg.decimals()
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
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