PatrickAlphaC / ethers-simple-storage-fcc

90 stars 128 forks source link

[RuntimeError] Local ganache deployment error-invalid opcode #90

Closed menarayanzshrestha closed 1 year ago

menarayanzshrestha commented 1 year ago

I am following the same as tutorials but facing problem while deploying into Ganache locally

Code to deploy I am using is:

const ethers = require("ethers")
const fs = require("fs-extra")
require("dotenv").config()

async function main() {

    let provider = new ethers.JsonRpcProvider(process.env.RPC_URL)
    let wallet = new ethers.Wallet(process.env.PRIVATE_KEY, provider)

    const abi = fs.readFileSync("./SimpleStorage_sol_SimpleStorage.abi", "utf8")
    const binary = fs.readFileSync("./SimpleStorage_sol_SimpleStorage.bin", "utf8")

    const contractFactory = new ethers.ContractFactory(abi, binary, wallet)
    console.log("Deploying, please wait...")
    const contract = await contractFactory.deploy()

    console.log(contract)

}

main()
    .then(() => process.exit(0))
    .catch((error) => {
        console.error(error)
        process.exit(1)
    })

when I try to deploy this file with command "node deploy.js"

It says

Error: missing revert data (action="estimateGas", data=null, reason=null, transaction={ "data": "0xlonghexhere", "from": "0x27D4DCED3A264971AB584bEaAc8b8594EAB5d688", "to": null }, invocation=null, revert=null, code=CALL_EXCEPTION, version=6.6.1)
    at makeError (/Users/narayanzshrestha/other projects/hardhat-fcc/ethers-simple-storage/node_modules/ethers/lib.commonjs/utils/errors.js:125:21)
    at getBuiltinCallException (/Users/narayanzshrestha/other projects/hardhat-fcc/ethers-simple-storage/node_modules/ethers/lib.commonjs/abi/abi-coder.js:104:37)
    at Function.getBuiltinCallException (/Users/narayanzshrestha/other projects/hardhat-fcc/ethers-simple-storage/node_modules/ethers/lib.commonjs/abi/abi-coder.js:201:16)
    at JsonRpcProvider.getRpcError (/Users/narayanzshrestha/other projects/hardhat-fcc/ethers-simple-storage/node_modules/ethers/lib.commonjs/providers/provider-jsonrpc.js:599:43)
    at /Users/narayanzshrestha/other projects/hardhat-fcc/ethers-simple-storage/node_modules/ethers/lib.commonjs/providers/provider-jsonrpc.js:267:45
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  code: 'CALL_EXCEPTION',
  action: 'estimateGas',
  data: null,
  reason: null,
  transaction: {
    to: null,
    data: 'longhexdata',
    from: '0x27D4DCED3A264971AB584bEaAc8b8594EAB5d688'
  },
  invocation: null,
  revert: null,
  info: {
    error: {
      message: 'VM Exception while processing transaction: invalid opcode',
      stack: 'RuntimeError: VM Exception while processing transaction: invalid opcode\n' +
        '    at exactimate (/Applications/Ganache.app/Contents/Resources/static/node/node_modules/ganache/dist/node/1.js:2:182136)',
      code: -32000,
      name: 'RuntimeError',
      data: [Object]
    },
    payload: {
      method: 'eth_estimateGas',
      params: [Array],
      id: 4,
      jsonrpc: '2.0'
    }
  }
}

Version I am using is

{
  "dependencies": {
    "dotenv": "^16.3.1",
    "ethers": "^6.6.1",
    "fs-extra": "^11.1.1",
    "solc": "^0.8.20"
  },
  "scripts": {
    "compile": "yarn solcjs --bin --abi --include-path node_modules/ --base-path . -o . SimpleStorage.sol"
  }
}
menarayanzshrestha commented 1 year ago

I solved by following steps:

packages used:

{
  "dependencies": {
    "dotenv": "^16.3.1",
    "ethers": "5.7.2",
    "fs-extra": "^11.1.1",
    "solc": "^0.8.20"
  },
  "scripts": {
    "compile": "yarn solcjs --bin --abi --include-path node_modules/ --base-path . -o . SimpleStorage.sol"
  }
}

final code converted to

const { getDefaultProvider, Wallet, ContractFactory } = require("ethers")
const fs = require("fs-extra")
require("dotenv").config()

async function main() {

    let provider = getDefaultProvider(process.env.RPC_URL)
    let wallet = new Wallet(process.env.PRIVATE_KEY, provider)

    const abi = fs.readFileSync("./SimpleStorage_sol_SimpleStorage.abi", "utf8")
    const binary = fs.readFileSync("./SimpleStorage_sol_SimpleStorage.bin", "utf8")

    const contractFactory = new ContractFactory(abi, binary, wallet)
    console.log("Deploying, please wait...")
    // const contract = await contractFactory.deploy({ gasPrice: 110000, gasLimit: 200000000 })
    const contract = await contractFactory.deploy({ gasPrice: 100000000000, gasLimit: 1000000 })

    console.log(contract)
    const deploymentReceipt = await contract.deployTransaction.wait(1);
    console.log(deploymentReceipt);

}

main()
    .then(() => process.exit(0))
    .catch((error) => {
        console.error(error)
        process.exit(1)
    })