fairdev610
fairdev610

Reputation: 148

Metamask not connecting to localhost 8545

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

Upvotes: 10

Views: 18801

Answers (6)

Yilmaz
Yilmaz

Reputation: 49341

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"

Upvotes: 2

confused_
confused_

Reputation: 1681

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

Upvotes: 3

Aqdas
Aqdas

Reputation: 940

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.

Upvotes: 1

jhonny
jhonny

Reputation: 858

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

Upvotes: 0

gcb
gcb

Reputation: 14558

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

Upvotes: 0

wiziwiz
wiziwiz

Reputation: 56

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/

Upvotes: 2

Related Questions