【问题标题】:sellPrice and buyPrice SoliditysellPrice 和 buyPrice Solidity
【发布时间】:2018-09-05 05:06:15
【问题描述】:

我在尝试将我的 sellPrice 设置为 0.01 而我的 buyPrice 等于 0.02 时遇到问题。 我的合约已部署,后来我使用 setPrices 函数设置代币价格。我加上双引号“10000000000000000”和“20000000000000000”,因为如果我不加引号就会抛出异常。

购买功能:

/// @notice Buy tokens from contract by sending ether
function buy() payable public {
    uint amount = msg.value / buyPrice;               // calculates the amount
    _transfer(this, msg.sender, amount);              // makes the transfers
}

在我的 web3 代码上:

$('#buy').click(function(){
Compra.buy({
  gas: 300000,
  from: web3.eth.coinbase,
  value: 20000000000000000
},function(error,res){
console.log(res);
if(!error){
    if(res.blockHash != $("#insTrans").html())
        $("#loader").hide();

    $("#insTrans").html("Block hash: " + res.blockHash)
}else{
    $("#loader").hide();
    console.log(error);
}
});
});

当 buy() 成功时,将 0.000000000000000001 的代币添加到我的钱包中,我想要在我的钱包中添加 1 个代币。我的意思是 0.02 = 1 个我的代币。

有人可以帮助我吗?我被困在这里了。

谢谢。

【问题讨论】:

  • 听起来你可能想要msg.value / buyPrice * 10**decimals 之类的东西,但如果不查看其余代码就很难知道。
  • @smarx 感谢您的回复。以下是完整代码:pastebin.com/eBYC77GV.
  • 10**decimals 在我看来是正确的。基本上,您将buyPrice 视为一个有理数,您可以在其中指定分子,而分母被硬编码为 10** 小数。您可能想对sellPrice 做类似的事情。
  • @smarx 好的,所以我必须创建一个具有该功能的新合同,对吧?或者我可以编辑和工作?
  • 智能合约无法编辑。你必须部署一个新合约。

标签: ethereum solidity smartcontracts web3js erc20


【解决方案1】:

这是因为您使用的是小数。如果您使用标准化的 18 位十进制格式,则需要将买入价格乘以 180(或 10**十进制),如上述 cmets 所述。

正如@smarx上面所说,您可以将代码更改为

msg.value / buyPrice * 10**decimals

【讨论】:

    猜你喜欢
    • 2021-11-10
    • 2022-11-27
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 2019-06-14
    • 1970-01-01
    • 1970-01-01
    • 2022-08-06
    相关资源
    最近更新 更多