/// @notice Constructor of the contract
function startToken() {
    IcoIsRunning = true;
    minimalGoalReached = false;
    icoIsClosed = false;
    tokenBalanceOf[this] += _totalSupply;
    allowed[this][owner] = _totalSupply;
    currentTokenPrice = 1;  // CURRENT price of 1 Token
    ICOprice = 1;               // ICO price

How do I set this if I want an exchange rate of 1 token for 0.001 ETH? On this code, it's 1 ETH per 1 token(ICOprice) I tried many different ways with no success.


2 Answers 2


All the calculations in solidity are done using wei. When you write 1 ether, it gets used as 1e18, which is 1 followed by 18 zeros.

So your variables would have following effect:

IcoPrice = 1000 * 1e18;
buyBackPrice = 1000 * 9e17;

Its not clear where you want to set this, I hope having an idea of what it means will enable you to set this as per your requirement.

  • It's a token sale contract where the user can buy 1 token at 0.001 ether, and he can send the tocken back but the price will be reduced by 10%. I am not able to make this work.
    – Zetal
    May 2, 2018 at 2:56

If you want to set 1 token per 0.001, meaning it's 1000 token per 1 ETH, set it this way:

uint256 public rate = 1000;

Then calculating the rate based on msg.value:

amountToGive = rate.mul(msg.value); // if user send 1 ETH it will give 1000 tokens.

Understand that here, msg.value is in WEI (so if I send 1 ETH, msg.value = 1000000000000000000 WEI, 1000000000000000000 * 1000 = 1×10²¹ OR 1000000000000000000000 WEI which is 1000 ETH/token

So this solution will work if your token have 18 decimals.

For 10% rate, it would be 900 tokens per ETH so:

uint256 public rate = 900;
amountToGive = rate.mul(msg.value); // if user send 1 ETH it will give 900 tokens.

Notice this whole code use SafeMath Library

  • I tried this and it didn't work. It returns an error even if I included SafeMath.
    – Zetal
    May 2, 2018 at 17:58
  • If you could tell me the error or share me somewhere as pastbin the full script so O can debug the issue, I will gladly do :)
    – btc4cash
    May 2, 2018 at 20:57
  • For some reason I am not able to set the amount of tokens x eth, but only the amount of eth x tokens. I need to set 0.001 eth, and I can't find the way to set this decimal number. I tried buyPrice= 1/1000; but it returns an error: TypeError: Type rational_const 1 / 1000 is not implicitly convertible to expected type uint256. Try converting to type ufixed8x3 or use an explicit conversion.
    – Zetal
    May 3, 2018 at 15:53
  • You cannot use decimals nor ufixed value for now. Please read doc and use google
    – btc4cash
    May 3, 2018 at 15:56
  • I told you I could understand better with code. Solution I propose fully working for me.
    – btc4cash
    May 3, 2018 at 15:57

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.