Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web3 JS Library (& Metamask), Get Token Balance

I want to query the eth balance and all token balances of a public key. So far I managed to load web3 and call the getBalance method.

Now, I want to see the balance for ERC20 tokens which are stored with the same public key? E.g. I want to see the balance of OMG tokens?

So far I found that each token contract address has to be queried: https://ethereum.stackexchange.com/questions/15372/how-can-i-view-all-the-tokens-and-contracts-associated-with-an-ethereum-address

omgContractAddress = "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07"
OmgContract = web3.eth.contract(abi)
omgContract = OmgContract.at(omgContractAddress)
someUsersAddress = "0x75087d9faa28d653750f3e4c86e7dcf6aff0a916"
omgContract.balanceOf someUsersAddress, (err, balance)-> #some owner
        console.error err
        console.info balance.toNumber()

Questions:

1) Do I need the abi of each token? Or can I use a standardized abi for ERC20 tokens as long as I just want to use standardized methods?

2) Where do I find the abi? Or do I need to recompile each contract?

E.g. OMG: https://etherscan.io/token/OmiseGo

like image 528
Andi Giga Avatar asked Nov 20 '17 20:11

Andi Giga


1 Answers

I could find the abis of several tokens on https://etherscan.io/address/<token_address>#code so far they have the same method common in their abi. I just copied the method directly into my source code without reading the original abi. E.g.

abi = [{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"}]
like image 88
Andi Giga Avatar answered Nov 08 '22 16:11

Andi Giga