How to set token price in solidity?

0 votes

I've tried

contract OToken {

using SafeMath for uint256;

uint public _totalSupply = 0;
uint public constant _cap = 100000000;
string public constant symbol = "OXN";
string public constant name = "OToken";
uint public constant decimals = 18;

uint public oneTokenInWei = 183.602;

If I want the token price to be $0.02 each and 1 eth is trading at $167 then 1 wei = 183.602 tokens

I can call this function if I want to change the price per token to .03

 function setOneTokenInWei(uint w) onlyOwner {
    oneTokenInWei = w;
    changed(msg.sender);
}

then this function to create the token

function createTokens() payable{

    require(
        msg.value > 0
        && _totalSupply < _cap
        && CROWDSALE_PAUSED <1
        );

        uint multiplier = 10 ** decimals;
        uint256 tokens = msg.value.mul(multiplier) / oneTokenInWei;

        balances[msg.sender] = balances[msg.sender].add(tokens);
        _totalSupply = _totalSupply.add(tokens);
        owner.transfer(msg.value);
}

this is not adding the current value to the senders wallet

Sep 25, 2018 in Blockchain by digger
• 27,620 points
57 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

We know that 1 Ether = 1018 wei, if we take that 1 Ether = $ 167

Then for $ 0.02 we should get $ 0.02 / $ 167 = 0.00011976047904191617 Ethers.

This equals to 1018 x 0.00011976047904191617 = 119760479041916.17 wei.

So if you put 1 Ether you will get 1018 / 119760479041916.17 = 8350 tokens.

At the price of $ 167 each token value is 167 / 8350 = 0.02 as desired.

I'd define a function so you can set the price of one ether and it derive the price of one token at two cents.

function setEthPrice(uint _etherPrice) onlyOwner {
    oneTokenInWei = 1 ether * 2 / _etherPrice / 100;
    changed(msg.sender);
}
answered Sep 25, 2018 by slayer
• 29,040 points

Related Questions In Blockchain

+1 vote
2 answers

How to convert INT to STRING in Solidity?

Look at the following code : function uintToString(uint ...READ MORE

answered Jun 27, 2018 in Blockchain by Christine
• 15,790 points
662 views
0 votes
1 answer

How to set web3 solidity variable value?

The setLog transaction has not yet been mined by ...READ MORE

answered Jul 6, 2018 in Blockchain by Christine
• 15,790 points
86 views
0 votes
1 answer

How to set chaincode path in Hyperledger Fabric?

For chaincode to properly run on your ...READ MORE

answered Jul 27, 2018 in Blockchain by digger
• 27,620 points
369 views
0 votes
1 answer

How to get all address and send ethers in solidity using a loop?

I found a similar code somewhere: contract  Holders{ uint ...READ MORE

answered Jul 31, 2018 in Blockchain by digger
• 27,620 points
102 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
77 views
0 votes
1 answer
0 votes
1 answer

Solidity geth: Error encountered during contract execution [Bad instruction]

recipes is a dynamic storage array. You need ...READ MORE

answered Oct 15, 2018 in Blockchain by Omkar
• 66,880 points
40 views
0 votes
1 answer

How to achieve simple OOP in Solidity?

contract A { uint public ...READ MORE

answered Sep 27, 2018 in Blockchain by slayer
• 29,040 points
20 views
0 votes
1 answer

How to set up Hyperledger Fabric v0.6 network without docker?

Hyperledger Fabric v0.6 does not provide any ...READ MORE

answered Jul 9, 2018 in Blockchain by slayer
• 29,040 points
258 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.