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