1

I have a pretty straight-forward contract that let's you buy ERC721 Tokens.

My issue is that my "buyToken" function works half the time - and fails the other half of the time with the following error:

MetaMask - RPC Error: Error: [ethjs-query] while formatting outputs from RPC 
'{"value" :
    {"code" :-32603, "data":
       {"message" : "VM Exception while processing transaction: invalid opcode",
        "code" : -32000,
        "data":
        {"0xfd7b08996eb28759737deb2d695baaee53aaa0cea819d9f27a79181ccba021ae":        
           {"error" : "invalid opcode",
            "program_counter" : 2687,
            "return" : "0x"},
            "stack" : "c: VM Exception while processing transaction: invalid opcode\n    
                 at Function.c.fromResults (/usr/local/lib/node_modules/ganache-
                 cli/build/ganache-core.node.cli.js:4:194812)\n    
                 at w.processBlock (/usr/local/lib/node_modules/ganache-cli/build/ganache-
                 core.node.cli.js:42:53376)\n    a
                 t processTicksAndRejections (internal/process/task_queues.js:93:5)",

            "name" : "c"
          }
       }
    }
}' 

What I'd really like to know is how we are supposed to decipher these invalid opcode errors. Like what exactly does "code" :-32603 and "code" :-32000 actually mean? What are the possible candidates of the issues I should be looking for?

To be clear: if my code failed 100% of the time and gave me this same error every single time, I would know with 100% certainly that it's my code that's bad. But it's working at least 50% of the time - and there's been absolutely no consistency in the failures.

I thought it might have to do with the PRICE of the Tokens - I have them ranging from 0.5 ETH to 15 ETH. But I could buy a Token for 1.5 ETH and have the Tx succeed, and then immediately try to buy another Token for the exact same 1.5 ETH amount - and that Tx would fail. So this makes no sense.

Any tips on the sort of things I should be looking into with these invalid opcode errors?

1

1 Answer 1

1

In some Solidity versions unhandled errors cause an invalid opcode. Such a case is for example when you divide by 0 - the flow is guided to an invalid opcode as the EVM doesn't have any idea what else to do. This is fixed in the latest Solidity version: https://docs.soliditylang.org/en/breaking/080-breaking-changes.html

Failing assertions and other internal checks like division by zero or arithmetic overflow do not use the invalid opcode but instead the revert opcode

So my guess is that there is some unhandled exception in the code.

The other, very unlikely, option is that the chain you use does not support some of the used latest opcodes. So it would be using an old version of the blockchain (of client nodes). Just make sure you use the latest node client version (or anything close to it).

4
  • OK, I'll look into Solidity V. 8. And for what it's worth, I'm currently on Node v.12.20.0 and Truffle v5.1.58. (...And I'm still so very hung up on this Error: while formatting outputs from RPC message... while formatting outputs??? What in the world could that MEAN? 🤨🤔😎)
    – Mark55
    Dec 28, 2020 at 0:06
  • My best guess is some version incompatibility problem, but no idea what and where Dec 28, 2020 at 11:27
  • @Mark55 Did you ever figure this out? I have the same error
    – Totem
    Feb 24, 2021 at 17:00
  • This was months ago - but I think it was a bad-index-Array type problem. Basically I had an error in my code.
    – Mark55
    Feb 24, 2021 at 17:03

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.