2

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;
        temp.id=x;
        temp.name=y;

        arr.push(temp);
    }
    function get(uint x) public view returns(uint,string memory){
        uint i;
        for(i=0;i<arr.length;i=i+1)
        {
            if(arr[i].id==x)
            {
                return(arr[i].id,arr[i].name);
            }
        }
    }
}
4
  • 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

8 Answers 8

6

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
  • 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
  • 1
    @Siwei from the date of your comment I may deduct that you were using compiler version 0.8.25. According to the solc changelog, they started using MCOPY, which is only from Cancun hardfork. And Ganache supports hardforks only up to Shanghai, and is deprecated.
    – Do-do-new
    Commented Oct 2 at 15:03
1

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.
1

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

1

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

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

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.

0

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

0

One possible reason is that your compiler is newer than the network you are deploying your code to.

This happens because e.g. in Cancan hardfork some newer opcodes were introduced, and Ganache (which is anyway deprecated) supports creating local networks only up to Shanghai hardfork.

For details see solc changelog and the list of opcodes.

So starting with compiler version 0.8.24 (support for Cancan) and for sure 0.8.25 (MCOPY, so even simple constructor will cause Invalid Opcode) do not deploy to Ganache.

Example of the simple constructor failing would be

    constructor(string memory initialMessage) {
        message = initialMessage;
    }

producing

Runtime error: invalid opcode
...
Web3ContractError: code couldn't be stored
-1

This arises due to accessing an out of bound index value

1
  • 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.