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:
- 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;
}
}
- 2_deploy_infinity_canvas.js:
const InfinityCanvas = artifacts.require("InfinityCanvas");
module.exports = function (deployer) {
deployer.deploy(InfinityCanvas, { gas: 5000000 });
};
- 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:
- Checking Constructor: Verified that the
InfinityCanvas
contract does not have any parameters in its constructor that might not be provided during migration. - 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. - Checking Dependencies: Made sure all dependencies and imported contracts (like those from OpenZeppelin) are of compatible versions.
- Manual Compilation: Tried manually compiling the contract using
solc
to see if there were any issues not caught by Truffle. - Migration Script: Ensured that the migration script
2_deploy_infinity_canvas.js
doesn't have any inconsistencies or errors. - Restarting Ganache: Sometimes resetting Ganache can resolve nonce or other blockchain-related inconsistencies.
- Updating Truffle: Ensured Truffle is up-to-date. Sometimes using the latest version of Truffle resolves certain deployment issues.
- Checking the Solidity Version: Verified that the specified Solidity version in
truffle-config.js
matches the version in the contract file. - Other Contracts: Checked if there are any other contracts in the
contracts
directory that might be causing conflicts during migration. - 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.