'migrates' encountered an invalid opcode while deploying. try:
My migration.sol code
// SPDX-License-Identifier: UNLICENSED //the version of solidity that is compatible pragma solidity ^0.8.0; contract Migrations { address public owner = msg.sender; uint public last_completed_migration; modifier restricted() { require( msg.sender == owner, "This function is restricted to the contract's owner" ); _; } function setCompleted(uint completed) public restricted { last_completed_migration = completed; } }
My truffle config.js file
const path = require("path"); module.exports = { // See <http://truffleframework.com/docs/advanced/configuration> // to customize your Truffle configuration! contracts_build_directory: path.join(__dirname, "/build"), networks: { development: { host: "127.0.0.1", port: 8545, network_id: "*" //Match any network id } }, plugins: ["truffle-contract-size"], compilers: { solc: { version: "^0.8.0" } }, solidity: { version: "0.8.3", settings: { optimizer: { enabled: true, runs: 1000, }, }, }, };
This may be due to the recent introduction of the new opcode
PUSH0
starting withsolc
version 0.8.20.For a complete list, see "When was each opcode added to the EVM?" . p>
Essentially, your version of the Solidity compiler is "ahead" of the network you are trying to deploy to. In other words,
solc
outputs bytecode that contains the opcode, but the network doesn't have it yet.You have 3 potential solutions:
127.0.0.1:8545
, this means that you can upgrade to the latest version of the network you are running locally (e.g. Ganache), maybe this will solve the problemsolc
.solc
version in your Solidity files:pragma Solidity 0.8.19;
solc
version in the truffle configuration file:version: "0.8.19"
PUSH0
opcode, this will resolve your issue, assolc
version 0.8.19 does not output this.solc
version, but specify a non-latest target EVM versionsolc
section in the truffle configuration file to add the new property:settings: { evmVersion: 'london' }
evmVersion: 'shanghai'
by default, which means it can outputPUSH0
evmVersion: 'london'
, which is the second latest target EVM version (as of June 2023), then you are actually tellingsolc
Avoid outputPUSH0
.PUSH0
opcode, this will solve your problem sincesolc
has been told not to output this.references: