DEV Community

Luis Lucena
Luis Lucena

Posted on • Edited on

Hardhat bug fix with MetaMask due to chainId

Fix

  • This is a Hardhat error that originates because MetaMask takes the default network and hardhat works with another network. Here is a sample of the error and how to fix it.

Image description

Config your hardhat.config.js file

  • Manually add the chaindId:1337 to avoid the conflict with MetaMask

e.g

require("@nomicfoundation/hardhat-toolbox");
// The next line is part of the sample project, you don't need it in your
// project. It imports a Hardhat task definition, that can be used for
// testing the frontend.
require("./tasks/faucet");
/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
  solidity: "0.8.9",
  networks: {
    hardhat: {
      chainId: 1337 // We set 1337 to make interacting with MetaMask simpler
    }
  }
};
Enter fullscreen mode Exit fullscreen mode

Boot up local development blockchain

$ npx hardhat node
Enter fullscreen mode Exit fullscreen mode

Connect development blockchain accounts to MetaMask

  • Copy private key of the addresses and import to MetaMask
    Connect your MetaMask to hardhat blockchain, network 127.0.0.1:8545.

  • If you have not added hardhat to the list of networks on your MetaMask, open up a browser, click the fox icon, then click the top center drop down button that lists all the available networks then click add networks. A form should pop up. For the "Network Name" field enter "Hardhat". For the "New RPC URL" field enter "http://127.0.0.1:8545". For the chain ID enter "1337". Then click save.

Migrate Smart Contracts

$ npx hardhat run src/contracts/scripts/deploy.js --network localhost
Enter fullscreen mode Exit fullscreen mode

Finally

  • Once you have your test account on the hardhat node you will have 10000 ETH to deploy your contracts.

Happy coding!❤️
💫 contributions are appreciate..
ETH address: luislucena.eth
tw: _luisald

Top comments (0)