设置令牌价格稳固性

use*_*607 2 ethereum solidity

如何才能确定每个令牌的价格?

我试过了

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;
Run Code Online (Sandbox Code Playgroud)

如果我希望令牌价格为每个0.02美元,1个eth的交易价格为167美元,则1 wei = 183.602令牌

如果我想将每个令牌的价格更改为.03,我可以调用此函数

 function setOneTokenInWei(uint w) onlyOwner {
    oneTokenInWei = w;
    changed(msg.sender);
}
Run Code Online (Sandbox Code Playgroud)

然后这个函数来创建令牌

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);
}
Run Code Online (Sandbox Code Playgroud)

这不是将当前值添加到发件人钱包

Ism*_*ael 6

我们知道1 Ether = 10 18 wei,如果我们取1 Ether = $ 167

那么0.02美元我们应该得到$ 0.02/$ 167 = 0.00011976047904191617 Ethers.

这相当于10 18 x 0.00011976047904191617 = 119760479041916.17 wei.

因此,如果你输入1以太,你将获得10 18/119760479041916.17 = 8350令牌.

以167美元的价格,每个标记值根据需要为167/8350 = 0.02.

我定义了一个函数,这样你就可以设置一个以太的价格,并将一个令牌的价格推算为两美分.

function setEthPrice(uint _etherPrice) onlyOwner {
    oneTokenInWei = 1 ether * 2 / _etherPrice / 100;
    changed(msg.sender);
}
Run Code Online (Sandbox Code Playgroud)