failed to deploy a contract to the local blockchain of Ganache using Ethers

40 Views Asked by At

I wrote a contract demo using vscode on Windows 10, mainly deploying a contract to the local blockchain of Ganache using Ethers. However, when I executed node test.js, it reported an error as follows: Error: missing revert data (action="estimateGas", data=null, reason=null, transaction={ "data": "Binary string of contract", "from": "0x58b5b7c1674f7174082BE1912869f64630F4145f", "to": null }, invocation=null, revert=null, code=CALL_EXCEPTION, version=6.11.1),how should I fix it?

By the way,My contract code is as follows:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
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;
    }
}

my js code is as follows:

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

async function main() {
  const provider = new ethers.JsonRpcProvider("https://rpc.sepolia.dev")
  const wallet = new ethers.Wallet("f2a0b5a745aa68a7c2343314f0376152329175ec2382fcbc32dc0fb87e6738e7", provider);
  const abi = fs.readFileSync("./SimpleStorage_sol_SimpleStorage.abi","utf8")
  const bin = fs.readFileSync("./SimpleStorage_sol_SimpleStorage.bin","utf8")
  const contractFactory = new ethers.ContractFactory(abi, bin, wallet)
  const contract = await contractFactory.deploy()
  console.log(contract)
}

main()
.then(()=>process.exit(0))
.catch((error)=>{
    console.log("----:"+error)
    process.exit(1)
})

my nodejs version is v20.11.1 my ethers version is ^6.11.1

Compile and execute code commands: yarn solcjs --bin --abi --include-path node_modules/ --base-path . -o . SimpleStorage.sol node test.js

I hope everyone can help me identify the possible causes of code errors and successfully execute the release contract code

1

There are 1 best solutions below

1
Petr Hejda On

The code snippet in your question contains a private key. Never share your private key on the internet. You should consider this key compromised - anyone can steal any funds on any network from the address derived from this key - so you might want to use a different key from now on.


const provider = new ethers.JsonRpcProvider("https://rpc.sepolia.dev")
const wallet = new ethers.Wallet("<privatekey>", provider);
// ...
const contractFactory = new ethers.ContractFactory(abi, bin, wallet)
const contract = await contractFactory.deploy()

This snippet tries to deploy the contract to the Sepolia testnet - not to your local emulator.

The deployer address doesn't have any Sepolia ETH to pay for the deployment transaction.

Instead of the rpc.sepolia.dev RPC URL, use your local emulator's URL. I believe Ganache uses http://127.0.0.1:7545 (UI) or http://127.0.0.1:8545 (CLI) by default. If you want, you can override the default URL in settings (Ganache UI) or with the --server.host and --server.port options (Ganache CLI, see the docs).

Example fix:

const provider = new ethers.JsonRpcProvider("http://127.0.0.1:8545")