2

I am trying to create an erc827token contract that accepts varying amounts of ether (0 for airdrop,   0.001, 0.002   >>   for donations.

code::

contract Airdrop coin is ERC827Token, Ownable { 
  string public constant name = "SimpleToken"; // solium-disable-line uppercase
  string public constant symbol = "SIM"; // solium-disable-line uppercase
  uint8 public constant decimals = 18; // solium-disable-line uppercase

  uint256 public constant INITIAL_SUPPLY = 10000 * (10 ** uint256(decimals));



  /**
   * @dev Constructor that gives msg.sender all of existing tokens.
   */
  constructor() public {
    totalSupply_ = INITIAL_SUPPLY;

  }


  function getAirdrop() external payable {
    if (msg.value < 0.005 ether) {
       balances[msg.sender] += 4000;
      } else if ( (msg.value >= 0.005 ether) && (msg.value < 0.008)) {
          balances[msg.sender] += 9000;
       } else if ( (msg.value >= 0.008 ether) && (msg.value < 0.016)) {
          balances[msg.sender] += 13000;
       } else balances[msg.sender] += 25000;
}


problem::

Deploying this contract works, but tokens don't get credited to addresses that send ether to the token contract.


How do I amend the contract to produce expected behavior and how do I forward recieved eth to an external wallet ( i.e implementing something like function withdraw() public onlyowner{})?

1 Answer 1

2

Your contract has errors:

  1. People sending nothing will still get tokens because in getAirdrop the first conditional ask for anything between 0 and 0.005 ether, so people can get as many tokens as they want just calling this many times sending 0 ether. Instead, you can ask for instance:

if((msg.value <= 0.003 ether) && (msg.value < 0.005 ether))

  1. (msg.value < 0.008) should be (msg.value < 0.008 ether) same for (msg.value < 0.016) which should be (msg.value < 0.016 ether)

Adding the getAirdrop function into the fallback will solve the problem of getting tokens in exchange for ether and then transfer to the owner will get the ether to the owner of the contract.

Here is an example using mostly the code that you provided.

Note that this will fail if the sender of ether is another contract because the transfer function only provides 2300 gas and the getAirdrop function consumes more than that.

pragma solidity ^0.4.24;

contract Airdrop { 
    mapping(address => uint256) public balances;
    address public admin;


  constructor() public {
      owner = msg.sender;
  }

  function getAirdrop() public payable {
    if (msg.value < 0.005 ether) {
       balances[msg.sender] += 4000;
      } else if ( (msg.value >= 0.005 ether) && (msg.value < 0.008 ether)) {
          balances[msg.sender] += 9000;
       } else if ( (msg.value >= 0.008 ether) && (msg.value < 0.016 ether)) {
          balances[msg.sender] += 13000;
       } else balances[msg.sender] += 25000;
    }

    function () payable public {
        getAirdrop();
        admin.transfer(msg.value);
    }


}

Hope this helps

1
  • Thanks, with little modifications, that worked to solve both problems. if (msg.value < 0.005 ether) was however intentional as I am looking to implement some sort of blacklist within the block to limit access (like, require msg.sender is not blacklisted then add to blacklist after that claim). Jun 15, 2018 at 21:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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