【发布时间】: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