0

I have a contract now distributing the tokens but I'm tired of sending individually. Can I get the solidity code to simply send to many addresses? Edited:::: In brief about the question 1.I want to send the same number of tokens to multiple addresses. Help me with the complete contract code not that single function :( .

This code seems working to me but i am bit confused 1.First do i need to create the contract and then send tokens to the created contract address and then give the values addresses and number of tokens and then click on sendTokenSingleValue area? please guide me

pragma solidity ^0.4.16;

contract Ownable {

  address public owner;

  function Ownable() {
    owner = msg.sender;
  }

  modifier onlyOwner() {
    require(msg.sender == owner);
    _;
  }

  function transferOwnership(address newOwner) onlyOwner {
    require(newOwner != address(0));
    owner = newOwner;
  }
}

interface Token {
  function transfer(address _to, uint256 _value) returns (bool);
  function balanceOf(address _owner) constant returns (uint256 balance);
}

contract AirDrop is Ownable {

  Token token;

  event TransferredToken(address indexed to, uint256 value);
  event FailedTransfer(address indexed to, uint256 value);

  modifier whenDropIsActive() {
    assert(isActive());

    _;
  }

  function AirDrop () {
      address _tokenAddr = 0x; //here pass address of your token
      token = Token(_tokenAddr);
  }

  function isActive() constant returns (bool) {
    return (
        tokensAvailable() > 0 // Tokens must be available to send
    );
  }
  //below function can be used when you want to send every recipeint with different number of tokens
  function sendTokens(address[] dests, uint256[] values) whenDropIsActive onlyOwner external {
    uint256 i = 0;
    while (i < dests.length) {
        uint256 toSend = values[i] * 10**18;
        sendInternally(dests[i] , toSend, values[i]);
        i++;
    }
  }

  // this function can be used when you want to send same number of tokens to all the recipients
  function sendTokensSingleValue(address[] dests, uint256 value) whenDropIsActive onlyOwner external {
    uint256 i = 0;
    uint256 toSend = value * 10**18;
    while (i < dests.length) {
        sendInternally(dests[i] , toSend, value);
        i++;
    }
  }  

  function sendInternally(address recipient, uint256 tokensToSend, uint256 valueToPresent) internal {
    if(recipient == address(0)) return;

    if(tokensAvailable() >= tokensToSend) {
      token.transfer(recipient, tokensToSend);
      TransferredToken(recipient, valueToPresent);
    } else {
      FailedTransfer(recipient, valueToPresent); 
    }
  }   


  function tokensAvailable() constant returns (uint256) {
    return token.balanceOf(this);
  }

  function destroy() onlyOwner {
    uint256 balance = tokensAvailable();
    require (balance > 0);
    token.transfer(owner, balance);
    selfdestruct(owner);
  }
}

when i tried with this code i got this error? what to do ? transact to browser/ballot.sol:AirDrop.sendTokensSingleValue errored: Gas required exceeds limit: 3000000. An important gas estimation might also be the sign of a problem in the contract code. Please check loops and be sure you did not sent value to a non payable function (that's also the reason of strong gas estimation).

Goutham
  • 11
  • 2
  • 6

1 Answers1

0

Check this and/or this as they already have the solutions. Either of this should work for you.

Rajesh
  • 706
  • 6
  • 23
  • 1
    Hi there. If you feel a previous answer already answers a new question, then the standard practice is to flag the new question as a duplicate. (Using "close" -> "duplicate of...") – Richard Horrocks Dec 07 '17 at 09:03
  • https://ethereum.stackexchange.com/questions/27778/distribute-token-to-multiple-address at this link the guy seems to be succeeded with that function but i want the complete contract code i am zero guy in coding if you can help me in this i will be very happy and thankful to you. :) – Goutham Dec 07 '17 at 21:23
  • rajesh sir ,could you please review that error please.i have tried with random gas limits but none worked. – Goutham Dec 07 '17 at 23:34
  • Try with gas 6000000. Better use remix and metamask that would make it more easy. – Rajesh Dec 08 '17 at 01:41
  • hi thank you for the response i have changed the gas and executed but transaction has done but the addresses not received the tokens? what wrong am doing? – Goutham Dec 08 '17 at 02:07
  • the program was written like the tokens will be sent from this contract (not from the owner), so have some tokens sent to this contract and then try again. Ensure you are the owner of the token contract as well as this contract – Rajesh Dec 08 '17 at 03:03
  • 1.i have sent tokens to the created airdrop contract 2.the creator of the smart contract and airdrop contract are same. could you please review again from your side on remix? or suggest another code? – Goutham Dec 08 '17 at 03:26
  • https://ibb.co/d3YyNw see the logs of the transaction here showing failed – Goutham Dec 08 '17 at 03:34
  • did you pass the args like this ["address1","Address1"],1000 ? – Rajesh Dec 08 '17 at 03:40
  • yes just like same as said ["A","B","C"],1000 – Goutham Dec 08 '17 at 03:43
  • see this line if(tokensAvailable() >= tokensToSend) you seems trying send more tokens that that is available with the contract, thats why Failed event was logged. Check you have sent enough tokens to this contract. and note that 1000 is not the wei amount that is being sent to it, its actual 1000 tokens, not 1000 * number of decimals – Rajesh Dec 08 '17 at 05:13
  • yes i have sent 100000 tokens to that contract and trying to send 1000 tokens to each address i put 6 addresses 6*1000=6000 tokens so the tokens i have sen to the contract are more than i am trying to send and i have not multiplied with the decimals though but still getting the failed transaction.could you please review what am doing wrong? :( – Goutham Dec 08 '17 at 15:39
  • which network is it? can you give the contact address? – Rajesh Dec 08 '17 at 16:28
  • i think there's something i am doing wrong in giving the values format but as u said its a correct format still not possible :( can u get me another if u can rajesh sir ? – Goutham Dec 08 '17 at 18:44
  • i got executed the code successfully with different code.anyways thank you rajesh sir :) – Goutham Dec 09 '17 at 04:14