Say I have an ERC20 token and I want to distribute it 1:1 to all ETH token holders at a given block. Is there a straight forward way to make a smart contract that anyone could call in order to receive their tokens?
you can of course do this. Gavcoin is a very early example. The following link should help.
ERC-20 tokens do not maintain an iterable list of current token holders, the used
mapping type allows you only to check for a known address - balance. To get a list of all token holders, you need to process this offline and build it yourself from blockchain data. You cannot do this in a smart contract (it is too resource heavy operation).
After you have the list of token holders, there are various tools that allow you to send airdrops to given address list.
I have created a standalone tool which collects ERC-20 token holders and transactions to SQLite database
Take a token contract address
Iterate over all
Transferevents for token using
Build a local database of these events
Allow you to use SQL to query any account balance on any point of time (block num)
The tool maintains a local SQLite database. The subsequent command runs scan the new blocks, since last run, and write the new transactions to the database. Exporting this from SQL based on a timestamp should be quite easy.
There are some quirks here and there: for example detecting mint / creation event for some tokens is not straightforward. Thus, you will may negative balance on the account receiving initial total supply if you rely on
Transfer event only.
What you can do is to ask the ETH holders to call a custom function in you contract (claim tokens).
Basically this function will do the following:
- Get the sender info (address and balance)
- If the address is new, send X amount of tokens according to his ETH balance.
- Save the address in a mapping so that he cannot claim more tokens using this function.