✅ Goal:

You want your existing MetaMask connect-wallet system to work with Anvil, the local Ethereum network from Foundry.

Prerequisite: 1. Foundry

🧠 What Happens by Default?

When you run anvil, it:

  • Spins up a local testnet at http://127.0.0.1:8545

  • Creates 10 funded accounts, each with a private key (these are local and safe to use in dev)

BUT — MetaMask doesn't know about Anvil unless you manually add it.

✅ Step-by-Step: Connect MetaMask to Anvil

  1. Start Anvil

bash: anvil

You'll see something like this:

Listening on 127.0.0.1:8545
Private Keys:
0x1234abcd... (address: 0xABC...)

Image description

2. Add Anvil Network to MetaMask

Open MetaMask → go to Settings > Networks > Add Network Manually and fill in:

Field Value

Network Name: Anvil (Localhost)
New RPC URL: http://127.0.0.1:8545
Chain ID: 31337
Currency Symbol: ETH(Anything)
Block Explorer URL: (leave blank)

Image description

✅ Chain ID must be 31337 (default for Anvil).

Image description

Image description

Image description

3. Import Anvil Account into MetaMask

Copy one of the private keys from Anvil terminal, then:

Image description

  • In MetaMask, click your avatar → "Import Account"

  • Paste the private key

Now you're using the same account that your Foundry scripts use — super helpful for testing both sides.

Image description

Image description

Image description

Now you will see the ETH and accounts.

Image description

[NB: If you don't see any ETH/GO/or value , then, refresh the network by selecting another network and then again come the Anvil. Now it should show the values]