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
• 26,740 points
8,729 views

1 answer to this question.

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,370 points
is the _etherPrice agrument's price in the ether or the dollar ??

Hi, @There,

Ethereum is an alternative digital currency to bitcoin and had been trading as high as $352 on Wednesday. It has since rebounded from its flash-crash lows to trade to about $325 on the GDAX exchange. According to industry and price tracking website Coinmarketcap, which takes into account the price on several exchanges, ethereum was trading around $338.

Related Questions In Blockchain

0 votes
1 answer

How to set the ERC20 token price in respect to ether in solidity?

Hi, @Jas, I would suggest you go through ...READ MORE

answered Nov 9, 2020 in Blockchain by anonymous
• 10,480 points
5,159 views
+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
12,446 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
2,119 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
• 26,740 points
1,861 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
1,922 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,451 views
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
• 69,220 points
1,464 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,370 points
893 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,370 points
1,364 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP