Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"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:

  • Verifying that your constructor params satisfy all assert conditions.
  • Verifying your constructor code doesn't access an array out of bounds.
  • Adding reason strings to your assert statements.

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

Error: *** Deployment Failed ***

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

  • Verifying that your constructor params satisfy all assert conditions.
  • Verifying your constructor code doesn't access an array out of bounds.
  • Adding reason strings to your assert statements.
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);
};
like image 392
Joe Thompson Avatar asked Jul 11 '26 23:07

Joe Thompson


2 Answers

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

like image 138
Wellington Zaneze Avatar answered Jul 14 '26 11:07

Wellington Zaneze


Change solc version in the truffle-config.js file as below.

compilers: {
    solc: {
      version: "0.8.13"
    }
  }
like image 35
Himanshu Avatar answered Jul 14 '26 12:07

Himanshu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!