0

While deploying my smart contract, I keep getting this error: Warning! Error encountered during contract execution [invalid opcode: PUSH0

import solcx
solcx.install_solc('0.6.0')
from solcx import compile_standard
import json
from web3 import Web3
import os
from dotenv import load_dotenv

load_dotenv()

with open ("./SimpleStorage.sol", "r") as file: simple_storage_file = file.read() print(simple_storage_file)

compiled_sol = compile_standard( { "language": "Solidity", "sources": {"SimpleStorage.sol": {"content": simple_storage_file}}, "settings": { "outputSelection": { "": { "": ["abi", "metadata", "evm.bytecode", "evm.bytecode.sourceMap"] } } }, }, #solc_version="0.6.0", ) #print(compiled_sol) with open("compiled_code.json", "w") as file: json.dump(compiled_sol, file)

#get bytecode bytecode = compiled_sol["contracts"]["SimpleStorage.sol"]["SimpleStorage"]["evm"]["bytecode"]["object"]

#get abi abi = compiled_sol["contracts"]["SimpleStorage.sol"]["SimpleStorage"]["abi"] print(abi)

w3 = Web3(Web3.HTTPProvider("https://goerli-rollup.arbitrum.io/rpc")) chain_id = 421613 my_address = "0xc80e5761af132995BecefD637************" private_key = "0x1d70b76bc2********************"

#Create contract in Ganache SimpleStorage = w3.eth.contract(abi=abi, bytecode=bytecode)

#Get the latest transaction nonce = w3.eth.get_transaction_count(my_address) print(nonce)

#Build a transaction #Sign a transaction #Send a transaction

#transaction = SimpleStorage.constructor().build_transaction({"chainId" : chain_id, "gasPrice": w3.eth.gas_price,"from" : my_address, "nonce": nonce}) print(w3.eth.gas_price) transaction = SimpleStorage.constructor().build_transaction( { "chainId": chain_id, "gasPrice": 900000000, "from": my_address, "gas": 100000, "nonce": nonce, } ) print(transaction) signed_txn = w3.eth.account.sign_transaction(transaction, private_key=private_key) private_key = os.getenv("PRIVATE_KEY") print(private_key) print(signed_txn)

tx_hash = w3.eth.send_raw_transaction(signed_txn.rawTransaction)

I am following this tutorial https://www.youtube.com/watch?v=M576WGiDBdQ Lesson 4

This is my solidity file:

// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0;

contract SimpleStorage {

uint256 favoriteNumber;

// This is a comment!
struct People {
    uint256 favoriteNumber;
    string name;
}

People[] public people;
mapping(string => uint256) public nameToFavoriteNumber;

function store(uint256 _favoriteNumber) public {
    favoriteNumber = _favoriteNumber;
}

function retrieve() public view returns (uint256){
    return favoriteNumber;
}

function addPerson(string memory _name, uint256 _favoriteNumber) public {
    people.push(People(_favoriteNumber, _name));
    nameToFavoriteNumber[_name] = _favoriteNumber;
}

}

sutti
  • 1
  • 2

2 Answers2

3

Instead of going back to an older version of Solidity compiler, in hardhat to prevent use of PUSH0 you can try setting evmVersion to a previous version (e.g. paris, which is the one before shanghai) whilst still using the latest compiler version (0.8.21 currently):

solidity: {
    compilers: [
      {
        version: `0.8.21`,
        settings: {
          optimizer: {
            enabled: true,
            runs: 15000
          },
          evmVersion: `paris`
        }
      },
    ],
  },

Polygon announced last week that they’ve just implemented PUSH0 on their zkEVM blockchain. I’ve tested the testnet and it works. zkEVM mainnet will work from 2023-09-10: Polygon zkEVM: Dragon Fruit Upgrade (with New Opcode) Coming to Mainnet Beta

I also tested 140 blockchains today and only found these ones (16%) that support PUSH0 currently:

  • auroraTestnet
  • edgeware
  • edgewareTestnet
  • gnosis
  • chiado
  • goerli
  • mainnet
  • moonbaseAlpha
  • moonbeam
  • moonriver
  • polygonZkEvmTestnet
  • pulsechain
  • pulsechainV4
  • sapphire
  • sapphireTestnet
  • scrollSepolia
  • sepolia
  • syscoin
  • syscoinTestnet
  • taikoTestnetSepolia

Hopefully the many other blockchains will follow, as developers want to be able to use the latest version.

SKYBITDEV3
  • 143
  • 1
  • 6
1

PUSH0 is a new op code that was added with the Shanghai EVM update: https://www.evm.codes/#5f?fork=shanghai

Some chains have not yet included the new op code.

The tutorial and and solidity version you are using are quite old. I suggest you target an earlier version of the EVM by updating your compiler settings with the following:

compiled_sol = compile_standard(
    {
        "language": "Solidity",
        "sources": {"SimpleStorage.sol": {"content": simple_storage_file}},
        "settings": {
            "evmVersion": "paris", # Add this line
            "outputSelection": {
                "*": {
                    "*": ["abi", "metadata", "evm.bytecode", "evm.bytecode.sourceMap"]
                }
            }
        },
    },
    #solc_version="0.6.0",
)

You might also want to look at increase the solidity version for your contracts.

You can read more about compiler settings here: https://docs.soliditylang.org/en/v0.8.21/using-the-compiler.html

Milk
  • 421
  • 2
  • 11