0

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.

Francisco
  • 163
  • 1
  • 5
  • 18
  • 1
    Sounds like you probably want something like `msg.value / buyPrice * 10**decimals`, but it's hard to know without seeing the rest of the code. – user94559 Mar 26 '18 at 23:04
  • @smarx Thanks for your reply. Here is the full code: https://pastebin.com/eBYC77GV. – Francisco Mar 26 '18 at 23:11
  • 1
    `10**decimals` looks right to me. Basically, you're treating `buyPrice` as a rational number where you specify the numerator and the denominator is hard-coded to be 10**decimals. You might want to do a similar thing with `sellPrice`. – user94559 Mar 26 '18 at 23:21
  • @smarx Ok so I have to create a new contract with that function right? Or I can edit and work? – Francisco Mar 26 '18 at 23:27
  • 1
    Smart contract's can't be edited. You would have to deploy a new contract. – user94559 Mar 26 '18 at 23:28
  • You are the man! Now is working! Thanks. – Francisco Mar 26 '18 at 23:54

1 Answers1

0

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
James
  • 1,928
  • 3
  • 13
  • 30