I have a contract which offers two functions:

1) A state modifying function which takes two parameters

2) A constant function

I have a web frontend in React calling these functions. When I call the constant function from the React frontend, everything works. But when I call the state modifying function from the frontend using web3, I get an invalid opcode error. However, the function does work when I call it from Truffle console, so the Solidity function itself is correct.

Here's how I'm calling from web3 1.0:

    ).send({ from: '0xc7edb363e01f7964f9fac9bf883afeee64fb2d93' })
     .then((receipt) => {
    .catch((error) => {

This results in an invalid opcode. And when I call from Truffle console like so:

    100, { from: '0xc7edb363e01f7964f9fac9bf883afeee64fb2d93'})

Everything works.

What reasons could there be for this? The contract is deployed on TestRPC.

  • Is your solidity function mark as payable?
    – Chim
    Commented Jan 16, 2018 at 12:13
  • @Chim No, but it's not supposed to be as it doesn't receive any ether.
    – Bastien
    Commented Jan 16, 2018 at 12:29
  • If you change the state, then function should mark as payable. And this does not check in truffle / testrpc
    – Chim
    Commented Jan 16, 2018 at 12:31
  • @Chim, nope that's not it. When I try to send value from truffle like so: i.createNewPlayer('0x35000f8407c2358Fbb089C2aE4e70fE005f4F6cD', 100, { from: '0x3909591500d5037689c7794f501986de7f9e764c', value: 100 }); I get: Error: Cannot send value to non-payable function. So Truffle does check for it.
    – Bastien
    Commented Jan 16, 2018 at 13:06
  • Just mark the function as payable and try. Or share the function.
    – Chim
    Commented Jan 16, 2018 at 13:50

1 Answer 1


Found the issue.

So I tried to call another state-modifying function from my frontend and that worked.

At this point, I know one state modifying function works from the frontend, and the other doesn't. While both work from truffle.

On a hunch, I added a gas: value to the one which wasn't working like so:

      from: '0xf3b22a49ad7f5ad0ffe9693e4a22121ed3738f73',
      gas: 3000000 

And it worked. I had a faulty assumption that web3 was handling the required gas amount in the background but that's false. Also, there's no actual default gas value which web3 uses.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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