0

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:

    contract.methods.createNewPlayer(
      '0x35000f8407c2358Fbb089C2aE4e70fE005f4F6cD',
      100
    ).send({ from: '0xc7edb363e01f7964f9fac9bf883afeee64fb2d93' })
     .then((receipt) => {
       console.log(receipt)
    })
    .catch((error) => {
      console.log(error);
    });     

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

    inst.createPlayer(
    '0x35000f8407c2358Fbb089C2aE4e70fE005f4F6cD',
    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 Jan 16 '18 at 12:13
  • @Chim No, but it's not supposed to be as it doesn't receive any ether. – Bastien Jan 16 '18 at 12:29
  • If you change the state, then function should mark as payable. And this does not check in truffle / testrpc – Chim Jan 16 '18 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 Jan 16 '18 at 13:06
  • Just mark the function as payable and try. Or share the function. – Chim Jan 16 '18 at 13:50
0

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:

    contract.methods.createNewPlayer(
      '0x0728f10c5a118883ecfb113a7b43bc627849fb45',
      100
    ).send({
      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, privacy policy and cookie policy

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