2

I am trying to deploy a simple storage smart contract to Ganache using ethers. The contract is getting deployed but still I am getting this error and can not access the contract functions.

the error:

Error: could not coalesce error (error={ "code": -32000, "data": { "hash": "0x89c29f1b4ec14fc129f11f4569658729340ade53f4140e019d1d10407eb04fb1", "messa ge": "invalid opcode", "programCounter": 24, "reason": null, "result": "0x89c29f1b4ec14fc129f11f4569658729340ade53f4140e019d1d10407eb04fb1" }, "message ": "VM Exception while processing transaction: invalid opcode", "name": "RuntimeError", "stack": "RuntimeError: VM Exception while processing transacti on: invalid opcode\n

Here is the smart contract code:

> // SPDX-License-Identifier: MIT
> 
> pragma solidity ^0.8.20;
> 
> contract SimpleStorage {
>     uint256 favoriteNumber;
> 
>     struct People {
>         uint256 favoriteNumber;
>         string name;
>     }
>     // uint256[] public anArray;
>     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;
>     } }

Here is the code for deployment:

const ethers = require("ethers");
const fs = require("fs-extra");

async function main() {
  const provider = new ethers.JsonRpcProvider("HTTP://127.0.0.1:7545");

  const signer = await provider.getSigner();
  const balance = await provider.getBalance(
    "0xD4628Fb04acd0b168A9c4619f514A50dC88462bc"
  );

  const abi = JSON.parse(
    fs.readFileSync("./SimpleStorage_sol_SimpleStorage.abi", "utf-8")
  );
  const byteCode = fs.readFileSync(
    "./SimpleStorage_sol_SimpleStorage.bin",
    "utf-8"
  );

  const myContract = new ethers.ContractFactory(abi, byteCode, signer);
  myContract.deploy({ gasLimit: 6721975 });

}

main();

I am using ethers 6.6.4

1 Answer 1

3

This is because solidity 0.8.20 introduces the PUSH0(0x5f) opcode which is only supported on the ETH mainnet and not on any other chains. That's why other chains can't find the PUSH0(0x5f) opcode and throw this error.

Consider using 0.8.19 for other chains.

This should solve your problem.

If you want to learn more about PUSH0 opcode you can check this small article which explains it pretty well. https://www.zaryabs.com/push0-opcode/

1
  • 1
    This worked. Thank you so much. Commented Jul 17, 2023 at 7:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.