Hey everyone I am launching an ICO. I need to create a smart contract that holds a certain amount of tokens that I created. Then when ether is sent to the smart contract address for my token. It will send back my created tokens ether equivalent value to the sending address? I also need to set the value of the tokens to .5 usd or whatever the equivalent is in ether? The contract then should send the ether to my private address. Can anyone help me or point me to some info. Thanks!

2 Answers 2


I guess this is what you are looking for

uint256 public totalSupply;// total no of tokens in supply
uint remaining;
uint price;
mapping (address => uint) investors //it maps no of tokens given to each address

function token(uint _totalSupply) //constructor
    totalSupply = _totalSupply;//set initial no of tokens in supply
    remaining = 0;

function ()  payable// called when ether is send
    assert(remaining < totalSupply);
    uint noOfTokens = div(msg.value,price); // calculate no of tokens to be issued depending on the price and ether send
    assert(noOfTokens < sub(totalSupply,remaining)); //no of tokens available should be greater than the one to be issued
    remaining = add(remaining,noOfTokens);
    transfer(msg.sender, noOfTokens);


function setPrice(uint _price)
{ //  price need to be set maually as it cannot be done via ethereum network
    price = _price;

I have included open-zepplin safe math for sub and div. While creating an ICO, remember to include all the secrurity checks, give the description about the token so that you can monitor the token easily using block explorer. You need to dynamically set price as .5 USD price is an approximate one and ethereum does not support decimal values.

  • Thanks! When you say dynamically what do you mean? How would you go about receiving a large amount of ether utilizing this smart contract? Also I don't see much about the ethereum addresses. How would you keep track of all the addresses that send ether and how many of thetokens they received? Security checks? Commented Aug 22, 2017 at 22:35
  • How do I set the value to .5 USD and make sure that the changing value of ether corresponds? Commented Aug 22, 2017 at 23:24
  • The value of USD/ETH keeps on fluctuating. If you want to set the price to 0.5 USD, then you need to keep on sending the value from outside. function setPrice(uint _price) does that job. Just send the value. Commented Aug 24, 2017 at 9:17
  • For example, if value of 1 ETH= 100$ and you want a token to cost 0.5 USD, then a token should cost (0.5*1/100 = 0.005)[priceOfOneToken/valueOfEtherInDollar] ether. so send the value 0.05 to setPrice. Hope this ans your query. Commented Aug 24, 2017 at 9:21
  • Also i just wrote a code snippet and not the complete code... you need to set the address. best practice i found is inside constructor set the address(dont give any function to change receiving address). Checks as in owner check, i.e., these functions can be performed only by owner. Commented Aug 24, 2017 at 9:26

I do not want to frustrate you, but it's harder than you expect. The first thing you should learn about Virtual Machine(EVM)

Secend thing read this post Intro to Programming Smart Contracts on Ethereum

The last thing read this article from the spiritual father of ICO he talking about How To Create Token and Initial Coin

  • Thanks! I appreciate your advice. Though time is of the essence mate. Commented Aug 22, 2017 at 22:36
  • This i suggest is to be done by everyone before jumping the wagon. Know the basics... Commented Aug 24, 2017 at 9:27

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.