JsonRpcProvider failed to detect network and cannot start up to deploy Smart contract using EthersJs to Ganache

103 Views Asked by At

SimpleStorage.sol

// I'm a comment!
// SPDX-License-Identifier: MIT

pragma solidity 0.8.24;

// pragma solidity ^0.8.0;
// pragma solidity >=0.8.0 <0.9.0;

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;
    }
}

deploy.js

const ethers = require("ethers");
const fs = require("fs-extra");
const { JsonRpcProvider } = require("@ethersproject/providers");

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

  
  const wallet = new ethers.Wallet(
    "0x9ef2baed5cd5e299d69990906f9cbb20c280c67a4034c4b3920de497074c539b",
    provider
  );
 
  const abi = fs.readFileSync("./simpleStorage_sol_SimpleStorage.abi", "utf-8");
  const binary = fs.readFileSync(
    "./simpleStorage_sol_SimpleStorage.bin",
    "utf-8"
  );
  
  const contractFactory = new ethers.ContractFactory(abi, binary, wallet);
  console.log("Deploying contract, please wait...");
  const contract = await contractFactory.deploy(); 
  console.log(contract);
}

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

package.json

{
  "name": "ethers-simple-storage-fcc",
  "version": "1.0.0",
  "description": "",
  "dependencies": {
    "@ethersproject/providers": "^5.7.2",
    "dotenv": "^14.2.0",
    "ethers": "^6.10.0",
    "fs-extra": "^11.2.0",
    "prettier": "^2.5.1",
    "solc": "0.8.24"
  },
  "scripts": {
    "compile": "solcjs --bin --abi --include-path node_modules/ --base-path . -o . SimpleStorage.sol"
  }
}

Error

Deploying contract, please wait... JsonRpcProvider failed to detect network and cannot start up; retry in 1s (perhaps the URL is wrong or the node is not started) Error: connect ECONNREFUSED 127.0.0.1:7545 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1532:16) { errno: -111, code: 'ECONNREFUSED', syscall: 'connect',
address: '127.0.0.1', port: 7545 }

0

There are 0 best solutions below