If you're interested in just a relatively small subset of popular ERC20 tokens, then looping over them might be okay.
But if you want everything, I think your best bet is to process the logs as you go and look for Transfer(address,address,uint256)
events. For each event, if the source looks like an ERC20 token (e.g. has name/symbol/decimals), do some accounting in your database where you're keeping track of these things.
Moreover, I think it could be more simplier way to get this info — that's why I'm here.
You're right, there is a different approach that is probably easier...
One issue is that you're approaching this from a 'legacy', centralised, fragile API mindset - all of the data that Etherscan is using; all of the data that you need is stored in Ethereum. The approach that you are taking could result in your code breaking if Etherscan charge their API, their service goes down or they decide to charge for it etc.
A better approach is to go straight to the source - Ethereum.
I want to get all Token Transfers from here
The Decentraland token is an ERC20 token; that means that we can be sure that every time that a token is transferred that the Transfer
event is fired:
event Transfer(address indexed from, address indexed to, uint tokens);
There are libraries such as web3js that let you process all of these events - historically and/or using an event subscription approach.
There are even services such as Infura that can be used to access the Ethereum data (so that you don't have to run your own local node).
And documentation on how to create a web3 enabled web application using Infura.
This is how I would suggest that you approach the problem.
Best Answer
The problem we are having here is that Token contract, in most cases, is not a contract which is selling Tokens. If you check ERC20 Standard https://en.wikipedia.org/wiki/ERC20 here then you will see there is no function to receive funds.
For this reason developers create Crowdsale contracts which own some amount of tokens ready to be sold and implement default payable fallback functions to receive ETH and send tokens back.
So to check what is value raised by each token you have to:
Get list of all token owners from ERC20 Token contract. You can check it by getting list of all Transfer transaction. ERC20 Standard has an event
Transfer(address indexed _from, address indexed _to, uint256 _value).
[Triggered when tokens are transferred.]You have to filter blockchain for such events for your token contract.
For each token holder you have to get list of transactions where ETH was send to this contract.
For each such transaction you have to check if Token Contract was called to transfer token to account address who send money to your selling token holder.
It is possible that sale contract received money, but did not transfer tokens back. I think we should not count such transaction as the one raising ETH for token
It is not as easy as it may look like. There is no one function which will tell you what is the amount raised by each token contract. It requires a bit of work and filtering by events and contract transactions. It is however doable.