5

I want to connect to ganache and I get an error that I don't know how to solve it. here is my deploy.js code which is script node.js

    const ethers = require("ethers")
    // const solc = require("solc")
    const fs = require("fs-extra")
    
    async function main(){
        // const provider = new ethers.providers.JsonRpcProvider("http://127.0.0.1:7545");
        // const wallet = new ethers.Wallet(
        //     "1ef159aa4f7ee5116a0f22652da709ccd8d35827b12a03eb24f22dd023591987",
        //     provider
        // )
        let provider = new ethers.providers.JsonRpcProvider("http://0.0.0.0:7545")
        let wallet = new ethers.Wallet("34db8b76c03356b47da8e1dcf427a35f9c013ce95a671ce23fe96d86fe2ba44b", 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(); // Stop here! wait for contract to be deploy
        console.log(contract)
    
    }
    
    main()
        .then(() => process.exit(0))
        .catch((error)=>{
            console.error(error);
            process.exit(1);
        })

and it produces the following error:

     Deploying, please wait..
     Error: could not detect network (event="noNetwork", code=NETWORK_ERROR, version=providers/5.6.8)
         at Logger.makeError (/home/stars/forder/blockchain/leson5/ethers-simple-storage/node_modules/@ethersproject/logger/lib/index.js:233:21)
         at Logger.throwError (/home/stars/forder/blockchain/leson5/ethers-simple-storage/node_modules/@ethersproject/logger/lib/index.js:242:20)
         at JsonRpcProvider.<anonymous> (/home/stars/forder/blockchain/leson5/ethers-simple-storage/node_modules/@ethersproject/providers/lib/json-rpc-provider.js:561:54)
         at step (/home/stars/forder/blockchain/leson5/ethers-simple-storage/node_modules/@ethersproject/providers/lib/json-rpc-provider.js:48:23)
         at Object.throw (/home/stars/forder/blockchain/leson5/ethers-simple-storage/node_modules/@ethersproject/providers/lib/json-rpc-provider.js:29:53)
         at rejected (/home/stars/forder/blockchain/leson5/ethers-simple-storage/node_modules/@ethersproject/providers/lib/json-rpc-provider.js:21:65)
         at processTicksAndRejections (node:internal/process/task_queues:96:5) {
       reason: 'could not detect network',
       code: 'NETWORK_ERROR',
       event: 'noNetwork'
     }

What am I doing wrong?

Paweł Stach
  • 83
  • 1
  • 1
  • 5

7 Answers7

9

You are following the Freecodecamp pattric collins tutorial. You are working in the WSL ubuntu terminal and installed the Ganache locally and must be located in the WSL server. So, Ganache is not connected to different environment. Go to the Ganache and click on Settings, then go the server and choose WSL as server. After this save and restart the Ganache. In your code in the connection code replace this

const provider = new ethers.providers.JsonRpcProvider(
    "HTTP://172.27.224.1:7545"
  );

or with your shown JSON-RPC. It will work for you. Thanks

3

https://github.com/smartcontractkit/full-blockchain-solidity-course-js/blob/main/chronological-updates.md#using-the-wsl-endpoint

Please follow along one of the 4 steps that suit you on the weblink above.

Let me know if it helped.

Thanks Manu

  • 1
    A link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](https://meta.stackexchange.com/a/8259) so your fellow users will have some idea what it is and why it is there, then quote the most relevant part of the page you are linking to in case the target page is unavailable. – cursorrux Jul 11 '22 at 10:45
1

If you are on windows and using WSL-UBUNTU just go to the server setting in Ganache and reset the server to:

vETHERNET - WSL

Example

enter image description here

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
1

If you are following the Freecodecamp pattric collins tutorial and you are working in the WSL ubuntu.

This is what worked for me OPTION 1: If you want to install & run Ganache on your WSL itself

  1. Install Ganache on WSL yarn add ganache

  2. Run Ganache yarn run ganache

  3. Copy the RPC URL (127.0.0.1:8545) from the bottom of the terminal screen that says RPC listening on 127.0.0.1:8545 & paste it in the ethers.js JsonRpcProvider( ) function parameter.

  4. Also copy any Private key on scrolling up the Terminal Screen & paste it in the ethers.js Wallet( ) function parameter.

  5. Now let this terminal run in the background, since we want our Ganache server to be running. So open a new terminal & run the deploy.js file by running the below command: node deploy.js

OPTION 2: If you want to run Ganache on your Windows ( if you have already installed Ganache on Windows )

  1. Go to Settings ( Gear Icon ) on the Top Right hand Corner of Ganache Windows GUI. Move over to the Server tab and Change the HOSTNAME to WSL ( Mine worked for VirtualBox & WiFi too ) Port Number & Network ID can be LEFT UNHARMED.

  2. Click on Save & Restart at the top right hand corner of the UI.

  3. Now Copy the RPC URL from the RPC SERVER and paste it in the ethers.js JsonRpcProvider( ) function parameter.

  4. Also Copy any Private Key and paste it in the ethers.js Wallet( ) function parameter.

  5. IMPORTANT STEP ( Firewall Settings ) : Go to Windows Defender Firewall with Advanced Security on your Windows. Click on Inbound Rules & Search for Ganache Right click on Ganache & Go to Properties. Then Check the CheckBox that says Allow the connection & Click on Apply. And Close this Firewall window.

  6. Run the deploy.js file by running the command in the WSL terminal window: node deploy.js

Paul
  • 21
  • 2
0

For me, I installed ganache-cli in the wsl and it was able to connect to ganache-cli - running directly on WSL2.

Just in case this helps.

Sagar Darekar
  • 982
  • 9
  • 14
user3069970
  • 71
  • 1
  • 3
0

This happens if you run the code in WSL but Ganache is opened in Windows. Follow along the URL given in another answer here. For me changing the server on ganache UI helped

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 22 '22 at 23:30
0

For MacOS users that encounter this error, just get Ganache up and running and try to run again with node.

Simeon Udoh
  • 23
  • 1
  • 4
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 08 '22 at 13:14