sellprice and buyprice fixing in smart contract

0 votes

I'facing problem when trying to put my sellPrice to 0.01 and my buyPrice equals to 0.02 in my smart contract after deployment. I intend to use setPrices function to set token price. So, I put within double quotes "10000000000000000" and "20000000000000000" because if I put without them, then it throws 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 successfull, I want to 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 stuck here.

Oct 17, 2018 in Blockchain by sabby
• 4,370 points
602 views

1 answer to this question.

0 votes

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
answered Oct 17, 2018 by Christine
• 15,790 points

Related Questions In Blockchain

0 votes
1 answer
0 votes
1 answer

How to prevent the smart contract from being modified and deployed in the blockchain network?

To expand on Matthew's answer, each state ...READ MORE

answered Jul 6, 2018 in Blockchain by aryya
• 7,460 points
808 views
+1 vote
2 answers
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,923 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,451 views
+3 votes
2 answers

How to run ethereumjs using Node.JS

You need to install testrpc globally on ...READ MORE

answered Mar 27, 2018 in Blockchain by ned_crew
• 1,610 points
1,415 views
0 votes
1 answer

Smart contract limitations in Ethereum and Hyperledger Fabric

No, the contract cannot sign on behave of B. A ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
675 views
+1 vote
1 answer

How is a smart contract stored on a Blockchain?

Smart contract template and transaction events are ...READ MORE

answered Apr 4, 2018 in Blockchain by Christine
• 15,790 points
1,094 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP