2

I am trying to fork polygon testnet (mumbai) for local development and run my forked node with a custom chainId. This is so that my JS scripts can identify the chain that is running, and therefore execute the appropriate code (eg deploy contracts, use correct addresses etc).

In my hardhat.config.js i have this:

networks: {
   mumbai_fork: {
      url: "http://127.0.0.1:7545",
      chainId: 7545,
    },
}

As you can see, I am trying to set the chainId to my custom value, 7545. When I fork the network using this:

npx hardhat node --fork https://polygon-mumbai.g.alchemy.com/v2/<my-key> --port 7545 --verbose

I want the resulting node to run with the chainId of 7545.

However, when I run my JS scripts, I get the error:

Error HH101: Hardhat was set to use chain id 7545, but connected to a chain with id 31337.

How do I run my forked node with a chainId of 7545 and then connect hardhat to my custom 7545 chain on localhost:7545? It would be great if there was a flag I could append to the CLI command when starting the node, something like --chainid 7545?

1 Answer 1

2

By default, hardhat will search for the hardhat configuration in networks. If it is not defined, it will use the default parameters to run the node.

networks: {
   hardhat: {
      chainId: 7545,
    },
}

and run it with:

npx hardhat node --fork https://polygon-mumbai.g.alchemy.com/v2/<my-key>
2
  • This doesn't solve the problem though, because if I run hardhat without forking and using local network, then my script will check the chainId and see that it is 7545, and then run the conditional code for the mumbai fork... Ideally I would like to only set the chainId to 7545 if i am running the fork. Any ideas?
    – pho_pho
    Oct 28, 2022 at 16:36
  • In the answer provided you will fork the mumbai network and have the chainId set to 7545. Even though the network is named hardhat it is still a fork of mumbai. to run the script on the forked mumbai just run: npx hardhat run PATH_TO_SCRIPT --network hardhat Oct 28, 2022 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.