10

I was trying to follow a tutorial from devto.io and to send transactions to the smart contract, I would need to connect my MetaMask wallet using one of the accounts created when I ran npx hardhat node command. To do so, I opened MetaMask and updated the network to be Localhost 8545, but Metamask didn't connect to it and showed errors like the following screenshot. enter image description here

Yilmaz
  • 35,338
  • 10
  • 157
  • 202
fairdev610
  • 148
  • 1
  • 1
  • 10

6 Answers6

3

What helped me was following these steps :

Step 1: Install Ganache and then launch the application.

Step 2: Check the RPC SERVER on the navigation bar and note it down.

enter image description here

Step 3: Open Metamask network settings. Delete the original localhost network.

Step 4: Add a new Ganache localhost network with the following configuration:

  • Network name - Whatever you want (Example: Ganache localhost)
  • New RPC URL - RPC server noted from Ganache.
  • Chain ID - 1337
  • Currency Symbol - ETH
  • Press Add network

enter image description here

confused_
  • 1,133
  • 8
  • 10
2

I think that you probably interrupted the node you launched when you typped :

npx hardhat node

You should keep it running, open another terminal for the rest of the tutorial. Also you could check the output of this command. Is it indicating something else than ? :

Started HTTP and WebSocket JSON-RPC server at http://127.0.0.1:8545/
wiziwiz
  • 56
  • 1
  • Thanks for your answer. But I kept it running as you said. – fairdev610 Nov 01 '21 at 11:51
  • Ok. Is there anything blocking port 8545 on your machine? – wiziwiz Nov 01 '21 at 13:37
  • Also what was displayed in the terminal were you run >npx hardhat node when you deployed the first contract ? (there should be something like " console.log: Deploying a Greeter with greeting: Hello, Hardhat! ") Can you see any error printed in that same terminal when you try to connect with metamask ? – wiziwiz Nov 01 '21 at 13:57
  • There were no errors and displayed as you wrote above. – fairdev610 Nov 01 '21 at 15:12
2

You most likely entered wrong URL into RPC-URL input: "http://localhost:8545". SInce in config file, hardhat has chainId:1337, it may be using ganache under the hood.

If you entered correct config but it still does not connect, you could use ganache-cli. http://localhost:8545 is for ganache-cli. You should be installing ganache-cli globally and then start the server on terminal if your hardhat node has an issue

   ganache-cli

When you run the command on the terminal, you will get the local public and private keys:

enter image description here

After setting the network you could import an account in one of those private keys:

enter image description here

if you click on the logo next to the network name, you will see the option import account. note that everytime you run a new command ganache-cli it will generate new private keys, so you have import a new account.

  • ganache gui listens to "7545" but ganache-cli listens to "8545"
Yilmaz
  • 35,338
  • 10
  • 157
  • 202
1

What worked for me, in this case, is This video

I downloaded Ganache from the following link

Installed it and just hit the Quick Start

It will launch the ganache as shared below in the screenshot. You can see the Network ID and RPC Server details.

enter image description here

As a Next step, delete the HTTP://localhost:8545 network and create a new network with Ganache RPC Server details and Network ID as shared below. I have just changed my port from default to 3000.

enter image description here

Here you Go!

enter image description here

Hope it helps. Also, feel free to share your thoughts if you have other approaches as I am also in the learning phase :-)

Thanks.

Aqdas
  • 868
  • 4
  • 16
  • 57
0

You probably have a IPv6 address that is not listening on that port.

e.g.

$ ping localhost
PING localhost.localdomain (::1)...

either remove that line from /etc/hosts (e.g. ::1 localhost.localdomain localhost) or point ethermask to 127.0.0.1 instead of localhost

gcb
  • 13,901
  • 7
  • 67
  • 92
0

The error is most simple that you probably think, the default chain id for test networks in metamask is not the same as the hardhat chain id, you should go to metamask, then config, networks, localhost and change chain id to 31337

jhonny
  • 805
  • 4
  • 10