尝试将我的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 mytokens。
有人可以帮我吗?我很困在这里。
谢谢。
这是由于你使用的是小数。如果你使用标准的18位十进制格式,则需要将购买价格乘以180(或10 **小数),如上面的注释中所述。
正如@smarx所说,你可以将代码更改为
msg.value / buyPrice * 10**decimals