I am trying to run the following solidity function:

function test() payable {

Where this.balance is 0.1 Ether and balances[msg.sender] is 200, so the msg.sender should be sent 0.02 Ether. However, this never works. I have tried executing the function in the following ways (sending ether or not, it doesn't execute the transfer):

contract.test({from: web3.eth.accounts[0], gas:1000000, value: web3.toWei(0.01, "ether")})
contract.test({from: web3.eth.accounts[0], gas:1000000})

I have verified the values of balances[msg.sender] and this.balance as follows (I'm using the same msg.sender account in all cases):

function balance() constant returns (uint256) {
           return balances[msg.sender];

 function contractBalance() returns (uint256) {
           return this.balance;
  • 1
    Is balances[msg.sender] 200 wei or 200 * ETH_TO_WEI? If it's the former your contract will be rounding it down to 0. It's better to so the multiplication first, then divide the bigger number by 1000. – Edmund Edgar Oct 8 '17 at 12:05
  • balances[msg.sender] is generated when the user submits ether to the contract, and is basically balances[msg.sender] = (msg.value)*(1000)/10**18 so I think that's the issue I overlooked. The function where it does msg.sender.transfer also has other code in it which runs, so I think msg.sender.transfer is going through but transferring nothing (I believe it msg.sender.transfer in a function fails, none of the code in that function will go through). – ZhouW Oct 8 '17 at 12:14
  • OK, so as well as multiplying first, don't try to convert to ETH in the contract. Do everything in Wei, then convert to and from ETH in the UI. – Edmund Edgar Oct 8 '17 at 12:20

Your Answer

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

Browse other questions tagged or ask your own question.