6

I am attempting to deploy a test contract on javascriptVM using Remix IDE in chrome. However when I try to execute the contract I receive this message in the status section.

"transact to Notes.addNote errored: VM error: invalid opcode. invalid opcode The execution might have thrown. Debug the transaction to get more information. "

pl let me know if anything wrong in mycode..

3
  • 4
    can you provide full code here in question? Commented Jan 31, 2019 at 6:04
  • Are you adding the first note in the contract? Also adding the error string in require statements can be helpful to debug.
    – Aniket
    Commented Jan 31, 2019 at 6:08
  • I think you are getting mixed up in note_id and index of array, if note at a particular index is not even created, how can you assign value directly by =.
    – sp4c3
    Commented Jan 31, 2019 at 7:55

2 Answers 2

6

Invalid opcode can be the result of either one of the following runtime operations:

  1. assert of an expression which evaluates to false
  2. array[i] where i >= array.length

In your code, only the second option appears viable.

It is viable in the expression userNotes[msg.sender][_noteId].

So the error must be the result of _noteId >= userNotes[msg.sender].length.

2
  • 1
    Two more operations which can lead to invalid opcode: 1. Dividing by zero. 2. Calling a function which receives an enum parameter as input, and passing it a value larger than that enum's max value. This is a rather poor decision of the Solidity compiler development team, since this scenario can be the result of user input (i.e., calling the function from the off-chain), which means that the correct opcode should be Revert (and the user would subsequently receive the remaining gas back). I hope that they change this decision in the future. Commented Aug 14, 2020 at 7:15
  • As a follow up, in 0.8.0 all of these were changed to revert with an error code.
    – hrkrshnn
    Commented Oct 1, 2021 at 9:23
0

It is probably because there are some array iterables which are going out of range. Invalid opcode error comes only in this case (For me though).

New to blockchain and smart contracts!!! Correct me if I'm wrong.

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.