Truffle Migration – Resolving Invalid Opcode Error While Deploying “MyToken”

truffle-migration

I am trying to deploy my first smart contract on ganache blockchain.
The compilation of my NFT goes well but when I try the "truffle migrate" command it gives me the error in the pic.
The code of my contract is really simple, I paste it here:

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

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "hardhat/console.sol";

contract MyToken is ERC721 {

    constructor() ERC721("MyToken", "MTK") {}

}

enter image description here

I tried the way described in this issue: "Migrations" hit an invalid opcode while deploying on deploy Migrations.sol with Truffle but didn't solve my problem.

Best Answer

Downgrading solc from 0.8.20 to 0.8.13 resolved the problem for me.

I got to that version by downloading the metacoin truffle box, verifying that migration works and then getting the version of solc from the truffle-config.js of the project.

It is worth doing the same, rather than using the specific solc version that resolved this for me at this point in time, as Truffle will probably update the metacoin box eventually to keep up with the rest of their tools and this number will get out of date.

Downloading the metacoin truffle box is as easy as this:

truffle unbox metacoin [PATH/TO/DIRECTORY]