search

Home  >  Q&A  >  body text

How to fix "Migration encountered an invalid opcode while deploying" error in Solidity?

'migrates' encountered an invalid opcode while deploying. try:

My migration.sol code

// SPDX-License-Identifier: UNLICENSED

    //the version of solidity that is compatible

    pragma solidity ^0.8.0;
    contract Migrations {
      address public owner = msg.sender;
      uint public last_completed_migration;

      modifier restricted() {
        require(
          msg.sender == owner,
          "This function is restricted to the contract's owner"
        );
        _;
      }

      function setCompleted(uint completed) public restricted {
        last_completed_migration = completed;
      }
    }

My truffle config.js file

    const path = require("path");

    module.exports = {
      // See <http://truffleframework.com/docs/advanced/configuration>
      // to customize your Truffle configuration!
      contracts_build_directory: path.join(__dirname, "/build"),
  
   
      networks: {
        development: {
        host: "127.0.0.1",
      port: 8545,
      network_id: "*" //Match any network id
     }
    },
    
    plugins: ["truffle-contract-size"],
    compilers: {
    solc: {
      version: "^0.8.0"
    }
    },
    solidity: {
    version: "0.8.3",
    settings: {
      optimizer: {
        enabled: true,
        runs: 1000,
      },
    },
  },
   };

P粉523625080P粉523625080275 days ago413

reply all(1)I'll reply

  • P粉924915787

    P粉9249157872024-03-27 20:10:04

    This may be due to the recent introduction of the new opcode PUSH0 starting with solc version 0.8.20.

    For a complete list, see "When was each opcode added to the EVM?" . p>

    Essentially, your version of the Solidity compiler is "ahead" of the network you are trying to deploy to. In other words, solc outputs bytecode that contains the opcode, but the network doesn't have it yet.

    You have 3 potential solutions:

    • Wait for your target network to support the new opcode, or use a different network.
      • Since your truffle configuration indicates that you are connecting to 127.0.0.1:8545, this means that you can upgrade to the latest version of the network you are running locally (e.g. Ganache), maybe this will solve the problem
      • Downgrade to an earlier version of solc.
      • Change the solc version in your Solidity files: pragma Solidity 0.8.19;
    • Change the solc version in the truffle configuration file: version: "0.8.19"
      • If the root cause of the error is indeed the PUSH0 opcode, this will resolve your issue, as solc version 0.8.19 does not output this.
    • Continue to use the latest solc version, but specify a non-latest target EVM version
      • Update the solc section in the truffle configuration file to add the new property: settings: { evmVersion: 'london' }
      • Note that 0.8.20 targets evmVersion: 'shanghai' by default, which means it can output PUSH0
      • However, if you override it to target evmVersion: 'london', which is the second latest target EVM version (as of June 2023), then you are actually telling solc Avoid output PUSH0.
      • If the root cause of the error is indeed the PUSH0 opcode, this will solve your problem since solc has been told not to output this.

    references:

    reply
    0
  • Cancelreply