I've been running this code on remix ide on injected web3 with my ganache server in on state, then also i'm getting this error of invalid opcode. The code runs perfectly in rinkeby test network.

pragma solidity ^0.5.7;
contract cust{

    struct a{
        uint id;
        string name;

    a[] arr;

    function set(uint x, string memory y ) public
        a memory temp;

    function get(uint x) public view returns(uint,string memory){
        uint i;
  • Can't see any issue in your code, but from how you're using that array, you may as well change it from a[] arr to mapping (uint => string) arr. Then you won't need that for loop. Commented May 10, 2020 at 6:05
  • On what hard fork is ganache running?
    – JBrouwer
    Commented May 10, 2020 at 9:29
  • Try setting your compiler setting to "byzantium" to see if that works. I suspect that ganache is running a non-Istanbul hard fork. Your solidity code compiles probably to opcodes introduced in Istanbul, which are thus invalid on your non-Istanbul chain.
    – JBrouwer
    Commented May 10, 2020 at 9:30
  • can you please provide with the steps to change the compiler settings as i'm pretty new to this :)
    – Yash
    Commented May 10, 2020 at 10:13

7 Answers 7


Every time I have ever gotten an invalid opcode error the byte code of the compile contracts got corrupted or it was an error in ganache. If it works on a testnet, I would say your error is probably ganache.

Things to try:

  • Delete your build directory and recompile (as you are using remix, change something and then recompile). This is to ensure your byte code did not get corrupted (can happen with lots of recompiles).
  • Restart ganache. Good old fashion turning it off and on again. Sometimes fixes it.
  • Update ganache and try it (if restarting it didn't work).

If the issue persists but your code works on a testnet, then assume it is ganache and there is nothing much you can do.

Hope this helps :)

NOTE: My problem was solved by disabling 'Error on transaction'. If using ganache GUI go to Settings >> Server >> disable Error on transaction.

Hope that to be helpful 😉

  • 3
    An invalid opcode is also returned as a result of: 1. An illegal array access (i.e., index >= length). 2. An assert statement with a falsified condition. Commented May 10, 2020 at 11:04
  • very wired, I switched to hardhat and problem gone.
    – Siwei
    Commented Mar 27 at 6:09

I was facing same issue once dealing with ganache. Make sure to keep same gas limit on ganache-cli and remix IDE.

Things to do:

  1. If you haven't installed ganache-cli, please try following command. npm install ganache-cli
  2. Then navigate to your account by using following command. ganache-cli -a
  3. Then you could get gas limit and make sure keep same gas limit to your remix IDE.

I resolved the issue by updating from package "ganache-cli" to "ganache" (version 7.8.0).


I'm using hardhat instead of Ganache but I got the same issue. I fixed the same issue by upgrading the hardhat package.

  • Thank you so much! saved my hours!
    – Siwei
    Commented Mar 27 at 6:09

I ran into this error after updating my version of the solc compiler. The solution was to empty my build/ directory and re-compile my contracts.


I faced a similar issue, I was using ethers.js and solc with JavaScript and Ganache. Issue was fixed by changing my solc version to "0.8.7-fixed"

from what I found out, this issue occurs due to corrupted contract binary data. issue CODE-32000


This arises due to accessing an out of bound index value

  • 1
    This answer needs more information to be considered good. How did you come to this conclusions ? How to you propose this should be fixed ? Commented Aug 23, 2023 at 6: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.