hardhat icon indicating copy to clipboard operation
hardhat copied to clipboard

Make hardhat-ethers compatible with ENS when Hardhat forks a remote network

Open thegostep opened this issue 4 years ago • 10 comments

thegostep avatar Mar 05 '21 13:03 thegostep

Hey @thegostep. Can you explain what this means? I don't get it.

alcuadrado avatar Mar 05 '21 13:03 alcuadrado

If I run a script with ethers and attempt to pass an ens name as a parameter, it will work on mainnet but not on hardhat mainnet fork

thegostep avatar Mar 05 '21 14:03 thegostep

This would be really helpful! Happy to spec out more.

tmm avatar Mar 01 '22 02:03 tmm

Created a minimal reproducible example cc @alcuadrado @fvictorio https://github.com/tmm/hardhat-fork-with-ens

tmm avatar Mar 03 '22 22:03 tmm

Thanks, @tmm! Now I understand what you meant.

We should override this method in our ethers provider so that it uses the forked network.

alcuadrado avatar Mar 03 '22 22:03 alcuadrado

Awesome!


In the meantime, a temporary, hacky workaround is to hardcode provider's network ensAddress:

const provider = ethers.provider
await provider.getNetwork()
// Mainnet, but you could set to whatever network you want
const ensAddress = '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e'
provider.network.ensAddress = ensAddress

tmm avatar Mar 03 '22 23:03 tmm

If I may add my 2 cents, I think that hardhat-ethers works as intended. The underlying issue seems to be coming from the forked network node itself.

I came to that conclusion because I'm currently developing a dapp and I cannot use .lookupAddress from my frontend on a forked network as well, it throws the common network does not support ENS. However, your hacky fix works @tmm .

So this seems to be coming from the node itself. I'll try to dig into the code but I'd appreciate some insights on where to find exactly what I'm looking for @alcuadrado .

0xbuidler avatar Feb 14 '23 14:02 0xbuidler

I found a solution for this, which is to add chainId of the network you forked in hardhat.config.ts.

To detect network, ethers.js asks for chainId here. Since hardhat's default chainId is 31337 and it's not defined in ethers.js, it returns unknown network.

If you specify a chainId in hardhat.config.ts, some defined networks have ensAddresss and support ens methods.

@fvictorio If you would like, I am happy to add an explanation to add chainId of the forked network in doc.

https://hardhat.org/hardhat-network/docs/guides/forking-other-networks

networks: {
  hardhat: {
    forking: {
      url: "https://mainnet.infura.io/v3/<key>",
    },
    chainId: <chainId of the forking network>
  }
}

(Note that you'll need to replace the component of the URL with your personal Infura or Alchemy API key. You may also want to specify chainId of the forking network to override hardhat default chainId 31337.)

hiroshitashir avatar May 21 '23 20:05 hiroshitashir

Sure, feel free to send a PR!

fvictorio avatar May 22 '23 14:05 fvictorio

@fvictorio

A PR was sent. Please let me know if there is anything I can do on my end:-)

hiroshitashir avatar May 22 '23 19:05 hiroshitashir