0

A simple contract for addition, an error is reported when calling the function add in Web3 provider mode:

"Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending?

VM Exception while processing transaction: invalid opcode."

But it works fine in JavaScript mode. The contract is as following:

pragma solidity >=0.4.0;

contract Calc {
     function add(uint a, uint b) public payable returns(uint) {
         return a + b;
     }  
}

I found out that if I use the pure modifier and compile it using the version 0.4.24, it would work.:

pragma solidity >=0.4.0;  

contract Calc {
     function add(uint a, uint b) public view returns(uint) {  
         return a + b;
     }  
}

choose the compiler version 0.4.24, then I can call the function add successfully. Why is that?

  • This question really confuses me, does anybody know why the error happens? Please help.. – XHQ Jun 15 at 5:41
  • What is your target blockchain? ropsten, rinkeby or mainnet. – Ismael Jun 16 at 6:43
  • in your first function have you send any eth to call that function because with payable you need to send eth. And btw if you only want to return a value not doing something that change the state of the blockchain then you should use view or pure not payable – haxerl Jun 16 at 11:57

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.