0

I've got this very simple Airdrop contract below:


contract ERC20 {
    function transfer(address _to, uint256 _value)public returns(bool);
    function balanceOf(address tokenOwner)public view returns(uint balance);
    function transferFrom(address from, address to, uint tokens)public returns(bool success);
}

contract SimpleAirdrop {

    ERC20 public token;
    uint256 amount = 1000 * 10**18 //18 decimals token

    function SimpleAirdrop(address _tokenAddr) public {
        token = ERC20(_tokenAddr);
    }

    function getAirdrop() public {
        token.transfer(msg.sender, amount); 
    }
}

I can deploy the contract with a given ERC20 address so I should be able to send token to this contract and users can call the getAirdrop() method and receive 1000 tokens

How can I prevent a user from being airdrop more than 1 time ? I could check the user balance but that doesn't proove he didn't received the airdrop already. Can I store a list of address that called the airdrop method ? What would be the gaz impact ?

Thank for your help !

1

The best you can do is create a mapping between the getAirdrop function and the user's address.

Then add a method into the getAirdrop fuction that if the airdrop is successful, the Boolean goes to true.

  1. Check if the address have done it before

//has the user gotten airdrop before?

mapping(address => bool) public hasClaimed;
  1. Incorporate the logic into the airdrop function

Then into the getAirdrop function, add

 function getAirdrop() public {
 require(hasClaimed[msg.sender] == false) ;
 token.transfer(msg.sender, amount);
 hasClaimed[msg.sender] == true;
}

Unfortunately a user can just create another address and claim more, but this is something you cannot stop solely with the Smart Contract.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.