3

Whenever I try to execute a function that uses address(this).balance I get an invalid opcode error. It compiles fine using 0.6.0.

Using truffle debug I get the following error:

Transaction halted with a RUNTIME ERROR.

There was no revert message.  This may be due to an in intentional halting expression, such as assert(), revert(), or require(), or could be due to an unintentional exception such as out-of-gas exceptions.
Please inspect your transaction parameters and contract code to determine the meaning of this error.

I checked the docs but there's no breaking change for using address(this).balance. Am i missing something?

Edit: I just tested this code in remix and it throws as well:


    pragma solidity 0.6.0;

    contract A {
        uint public total;

        receive() external payable {
            total = address(this).balance;
        }
    }

  • Please reduce compiler version to 0.5.x, and then check whether or not the problem persists. Also, please share your code in order to convince us that you don't get this error for some other instruction. – goodvibration Dec 29 '19 at 9:15
  • 1
    Please try v0.5.13. The change-log for that version mentions a balance-related fix. – goodvibration Dec 29 '19 at 10:16
  • 1
    That note says "Use SELFBALANCE opcode for address(this).balance if using Istanbul EVM", so your issue could actually be a subsequent result of the recent hard-fork. – goodvibration Dec 29 '19 at 10:19
  • 1
    I came to the same conclusion, solidity 0.6.0 uses the SELFBALANCE opcode. This opcode is supported by the main Ethereum network, but not by many emulators yet. – Jesbus Dec 29 '19 at 10:27
  • 1
    @JesseBusman yes that was the problem. I fixed it by explicitly defining the EVM version for the compiler. – Chris Dec 29 '19 at 10:54
5

I managed to fix it by explicitly defining the EVM version in my truffle config file. It's still weird because Remix supports this (as seen in the attached image) as well but still fails.

  compilers: {
    solc: {
      version: "0.6.0",
      settings: {
        optimizer: {
          enabled: true
        },
        evmVersion: "petersburg"
      }
    }
  }

Edit: I created an issue for the Remix team here.

Remix Configuration

| improve this answer | |
2

Summarizing the comments above, the change-log for compiler version 0.5.13 mentions a related fix:

Code Generator: Use SELFBALANCE opcode for address(this).balance if using Istanbul EVM.

So I'm taking the liberty to guess that your issue:

  1. Is actually a result of the recent hard-fork
  2. Actually dates back to compiler version 0.5.13

I'm wondering (and worried about) what happens in already compiled and deployed contracts which make use of address(this).balance...

| improve this answer | |

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.