1

When you're connected to a hardhat network using something like

npx hardhat run ... --network localhost

how do you tell if the network you're talking to is forked or not?

Right now I'm using this hack:

    const isHardhatNetwork = hre.network.name === "hardhat" || hre.network.name === "localhost"
    const isForked = isHardhatNetwork && ("forking" in hre.config.networks.hardhat) && hre.config.networks.hardhat.forking?.enabled

That's wrong though; it assumes that you're talking to a hardhat network that's using your same configuration, and that's not necessarily true. (The easy way to get a bug here is to start npx hardhat node, then change the configuration and don't restart npx hardhat node.)

1 Answer 1

-1

This will work

import { ethers, deployments, network } from 'hardhat';

console.log(network.config.forking.enabled);

and you will get true as a log if you used forking: true in your hardhat config.

also check this for possible solution in tests Hardhat chain fork on the test rather than config file

3
  • I don't think your answer is related to the question I'm asking. You're assuming that the hardhat configuration is related to the running instance; it's not. Or am I missing something in your answer? Commented Sep 10, 2023 at 1:41
  • Yes. I am assuming you are running forked network through HH config. If you are not and just connecting to some other RPC defined elsewhere that is forked then yes its not proper answer but I dont think you stated that
    – Blissful
    Commented Sep 13, 2023 at 15:35
  • "I am assuming you are running forked network through HH config. " - the question explicitly says that "it assumes that you're talking to a hardhat network that's using your same configuration, and that's not necessarily true." I suppose there's a related question of "how do you ask a running hardhat instance for its configuration" - but you cannot assume that an on-disk config matches the running instance. Commented Sep 14, 2023 at 16:42

Your Answer

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

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