javascriptethereumsoliditytruffleganache

"Migrations" hit an invalid opcode while deploying


I have suddenly started getting '"Migrations" hit an invalid opcode while deploying' error when I do 'truffle deploy' command.

My migrations file has not changed so I'm not sure why I am suddenly getting this error. Everyone who's posted for the same error just suggests downloading latest versions of ganache/truffle but I have updated my truffle and ganache to the most up to date versions and still getting the error.

This is the full error: ⠋ Fetching solc version list from solc-bin. Attempt #1 Starting migrations...

Network name: 'development' Network id: 5777 Block gas limit: 6721975 (0x6691b7)

1_initial_migration.js

⠙ Fetching solc version list from solc-bin. Attempt #1 Deploying 'Migrations'

*** Deployment Failed ***st from solc-bin. Attempt #1

"Migrations" hit an invalid opcode while deploying. Try:

Exiting: Review successful transactions manually by checking the transaction hashes above on Etherscan.

Error: *** Deployment Failed ***

"Migrations" hit an invalid opcode while deploying. Try:

at /usr/local/lib/node_modules/truffle/build/webpack:/packages/deployer/src/deployment.js:330:1

Truffle v5.9.2 (core: 5.9.2) Node v18.7.0

Migrations.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

contract Migrations {
    address public owner;
    uint256 public lastCompletedMigration;

    constructor() {
        owner = msg.sender;
    }

    modifier restricted() {
        require(msg.sender == owner, "Restricted to contract owner");
        _;
    }

    function setCompleted(uint256 completed) public restricted {
        lastCompletedMigration = completed;
    }

    function upgrade(address new_address) public restricted {
        Migrations upgraded = Migrations(new_address);
        upgraded.setCompleted(lastCompletedMigration);
    }
}

1_initial_migration.js

const Migrations = artifacts.require("Migrations");

module.exports = function(deployer) {
    deployer.deploy(Migrations);
};

Solution

  • Try change your pragma and solc version

    Migrations.sol

    // SPDX-License-Identifier: MIT
    pragma solidity >=0.4.22 <0.9.0;
    

    truffle-config.js

    compilers: {
        solc: {
          version: "0.5.1"
    }
    

    Sometimes, solc works for me with 0.8.20 or 0.7.3