Truffle Migrate Error: "<ContractName>" hit an invalid opcode while deploying

136 views Asked by At

I'm trying to deploy a contract named InfinityCanvas on a local Ganache blockchain using Truffle. However, I'm encountering an "invalid opcode" error during the deployment process.

Issue:

When running truffle migrate --reset, I encounter the following error:

*** Deployment Failed ***

"InfinityCanvas" 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.

Code Details:

  1. InfinityCanvas.sol:
// SPDX-License-Identifier: MIT 
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";

contract InfinityCanvas is ERC721Enumerable {
    // tokenId counter
    uint256 private _currentTokenId = 0;

    struct Canvas {
        string description;
        // Add other metadata attributes if needed
    }

    mapping(uint256 => Canvas) public canvases;

    constructor() ERC721("InfinityCanvas", "ICNV") {}

    function _getNextTokenId() private returns (uint256) {
        _currentTokenId += 1;
        return _currentTokenId;
    }

    function mintCanvas(address owner, string memory description) public returns (uint256) {
        uint256 newCanvasId = _getNextTokenId();
        _mint(owner, newCanvasId);
        canvases[newCanvasId] = Canvas(description);
        return newCanvasId;
    }
    
    function setDescription(uint256 tokenId, string memory description) public {
        require(ownerOf(tokenId) == msg.sender, "Only the owner can set the description");
        canvases[tokenId].description = description;
    }
}
  1. 2_deploy_infinity_canvas.js:
const InfinityCanvas = artifacts.require("InfinityCanvas");

module.exports = function (deployer) {
    deployer.deploy(InfinityCanvas, { gas: 5000000 });
};
  1. truffle-config.js:
module.exports = {
  networks: {
    development: {
      host: "127.0.0.1",
      port: 7545,
      network_id: "*",
      gas: 5500000,
      gasPrice: 20000000000,
    },
  },

  mocha: {
    // timeout: 100000
  },

  compilers: {
    solc: {
      version: "0.8.21",
    }
  },

  db: {
    enabled: false
  }
};

Environment:

  • Truffle version: v5.11.5
  • Node version: v18.18.1
  • Using Ganache CLI with the default settings

Any help or insights into why this is happening and how to resolve it would be greatly appreciated!

Fixes Tried:

  1. Checking Constructor: Verified that the InfinityCanvas contract does not have any parameters in its constructor that might not be provided during migration.
  2. Gas Limit: Adjusted the gas limit in the migration script and truffle-config.js to ensure that the contract is not running out of gas during deployment.
  3. Checking Dependencies: Made sure all dependencies and imported contracts (like those from OpenZeppelin) are of compatible versions.
  4. Manual Compilation: Tried manually compiling the contract using solc to see if there were any issues not caught by Truffle.
  5. Migration Script: Ensured that the migration script 2_deploy_infinity_canvas.js doesn't have any inconsistencies or errors.
  6. Restarting Ganache: Sometimes resetting Ganache can resolve nonce or other blockchain-related inconsistencies.
  7. Updating Truffle: Ensured Truffle is up-to-date. Sometimes using the latest version of Truffle resolves certain deployment issues.
  8. Checking the Solidity Version: Verified that the specified Solidity version in truffle-config.js matches the version in the contract file.
  9. Other Contracts: Checked if there are any other contracts in the contracts directory that might be causing conflicts during migration.
  10. Following Question on StackOverflow: "Migrations" hit an invalid opcode while deploying

Unfortunately, none of the above steps resolved the issue. Its also possible though, that I did do something wrong when trying the above named fixes.

0

There are 0 answers