0
 /// @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
    updatePrices();
}

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.

Zetal
  • 91
  • 2
  • 6

2 Answers2

0

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.

Ayushya
  • 1,698
  • 9
  • 24
  • 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 02 '18 at 02:56
0

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

btc4cash
  • 538
  • 4
  • 14
  • I tried this and it didn't work. It returns an error even if I included SafeMath. – Zetal May 02 '18 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 02 '18 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 03 '18 at 15:53
  • You cannot use decimals nor ufixed value for now. Please read doc and use google – btc4cash May 03 '18 at 15:56
  • I told you I could understand better with code. Solution I propose fully working for me. – btc4cash May 03 '18 at 15:57
  • I updated the code. Thanks for your help and patience. – Zetal May 03 '18 at 16:07
  • Calculation I put is amount of token for ONE ETH :)Do the math! Rate value * by amount of eth send. – btc4cash May 04 '18 at 01:09