1

I've written a smart contract that should return the balance of the current user using it and for some reason when I call the function it's returning the incorrect amount. I've used Remix and the Ropsten network for development/testing this.

  function getBalance() public view returns (uint){
    return msg.sender.balance;
  }

I am seeing 115792089237316195423570985008687907853269984665640564039457581007913129639935 as the result even though I have 0.35 ETH in my Ropsten wallet

Anybody know why this is happening?

2
  • 1
    Attach all of the code so we can inspect. Sep 25, 2020 at 4:57
  • 1
    Side note: you don't need a contract for that. For example, using web3.js, you can get the balance of an account via web3.eth.getBalance(account). Sep 25, 2020 at 6:10

1 Answer 1

1

This is a bug in geth that was resolved in v1.9.13.

The original issue can be found in Issue #16999 and the fix can be found in PR #20783.

The issue was basically that geth was subtracting 0 by (gasLimit * gasPrice), which underflowed and resulted in the large number you saw. In your case, you were probably using the default Remix values of gasPrice = 1000000000 and gasLimit = 3000000. If you take 0 - (1000000000 * 3000000), the resulting value will be the number you posted.

The reason you are seeing it on Remix is because you are probably using MetaMask. MetaMask uses Infura and Infura's nodes are on Geth v1.9.9. You can verify it with the following curl command:

curl -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":67}' https://mainnet.infura.io/v3/<project_id>

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.