Take look at my contract transaction history: https://ropsten.etherscan.io/address/0xe42d26e4a66e9562b40bde44e4ceff706fd67d20

I verified my contract so you can see everything using etherscan interface!!

You will notice I set sell/buy price in this transaction (I called it multiple times). https://ropsten.etherscan.io/tx/0xd03f3b07cb2ba66d3b9661ce584ff11873c517a830b0dda456863415c7954116

In this transaction I set buy price to 100. You can read that it is indeed so:

In these transactions I bought tokens for 0.01 ether

https://ropsten.etherscan.io/tx/0x41958bf1de73ece017be737eab94db1040d2a5614048011c95027be4e01e4782 https://ropsten.etherscan.io/tx/0xf57bbbf3ee155a9992c229c473d328e0e7ccbe8f3fd14325d474e22c450a7ec0

and some others.

What is evident from log is that I received 0.0001 tokens in return as if msg.value was 0.01 and not 10000000000000000 wei.
From official docs msg.value (uint): number of wei sent with the message

Why is result interpreted as if msg.value is in ether and not wei?

I am testing a contract on a Ropsten network using geth 1.8.8 with command line:

geth --testnet --datadir="x:\\a\\b\\GethWithRopsten\\"

In order to connect to geth I use web3js version 1, beta 34 running from node.js:

var web3 = new Web3(new web3.providers.IpcProvider('\\\\.\\pipe\\geth.ipc',net));

Compiler I use is solc 4.24 release.

Relevant code is (this code is almost all taken from example ERC20 token):

function div(uint256 a, uint256 b) internal pure returns (uint256) {
    return a / b;

function setPrices(uint256 newSellPrice, uint256 newBuyPrice) onlyOwner public {
    sellPrice = newSellPrice;
    buyPrice = newBuyPrice;

function buy() payable onlyIfEnabled public {
    uint256 amount = msg.value.div(buyPrice);   
    _transfer(this, msg.sender, amount);   


function _transfer(address _from, address _to, uint256 _value) internal returns (bool){
   require (_to != 0x0);
   require (balanceOf[_from] >= _value);
   require (balanceOf[_to].add(_value) > balanceOf[_to]);
   balanceOf[_from] = balanceOf[_from].sub(_value);
   balanceOf[_to] = balanceOf[_to].add(_value);
   emit Transfer(_from, _to, _value);
   return true;


  • 1
    Please split your question in multiple questions. Thanks. – Nicolas Massart May 29 '18 at 11:33

Trouble here is that I was not reading values correctly. Tokens are displayed with their decimal places as opposed to displaying values that hold them that are integers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.