I have a problem trying to put my sellPrice to 0.01 and my buyPrice equals to 0.02. My contract is deployed and later I use setPrices function to set token price. I put with doble quotes "10000000000000000" and "20000000000000000" because if I put without that throw an exception.
Buy function:
/// @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
}
On my web3 code:
$('#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);
}
});
});
When buy() is success add to my wallet 0.000000000000000001 of my tokens and I want 1 token on my wallet. I mean 0.02 = 1 mytokens.
Someone can help me please? I am very stuck here.
Thanks.
It is due to the fact you are using decimals. If you use the standardized 18 decimal format, you need to multiply the buy price by 180 (or 10**decimals) as mentioned in the comments above.
As @smarx said above, you can change the code to
msg.value / buyPrice * 10**decimals