[Ethereum] JSON RPC Get Tokens At Address

abijson-rpctokens

Getting a balance for a token from the JSON RPC is simple. This is all I need to get a Tronix balance. The "0xf230b790e05390fc8295f4d3f60332c93bed42e2" part is the contract address for Tronix.

{"id":1,"jsonrpc":"2.0","method":"eth_call","params":[{"to":"0xf230b790e05390fc8295f4d3f60332c93bed42e2","data":"[ADDRESS ABI]"},"latest"]}

But, how do I check what contracts are at an address?

If I do a eth_getTransactionReceipt on a transaction, I can get the contract address, but I need to know what transactions are at the address first. I can't figure this one out… How can I know which transactions have hit an address?

This Ethsplorer API call is exactly what I'm looking for, but I can't figure out how to do this with the JSON RPC:
https://api.ethplorer.io/getAddressInfo/0xff71cb760666ab06aa73f34995b42dd4b85ea07b?apiKey=freekey

From doing some reading, a lot of people are saying that I have to scan the entire Blockchain, but I am 90% sure that it is possible to achieve what I am looking for because MyEtherWallet does it. When I connect my Trezor, it makes the above eth_call call and passes the Tronix contract address in to the call, so it knows that there is Tronix at the address, but how does it know this? It's clear that MEW is not scanning the entire Blockchain when I query my wallet, so either they have figured out how to do this with the normal JSON RPC with a call that I'm not understanding properly, or they are making a secret call to some server indexed by addresses.

Best Answer

Technically, ERC-20 tokens are not at the owners addresses. If you have a token, your address is registered in the smart contract of the ERC20 token as a list of [address]: [balance].

To check the register and see your balance, you need the address of the ERC20 token smart contract.

Anyone can make an ERC20 token, and there's no easy decentralized way to list all of them.

You could collect all the addresses listed on https://etherscan.io/tokens and write a script which checked your balance on all of them, but you would have to keep the list updated when new ERC-20 tokens are launched.

Even more ambitiously, you could set up an Ethereum client and parse all contract creations, defining a filter or pattern to identify ERC-20 contracts. But that's very hard to get right.

Related Topic