hardhat
hardhat copied to clipboard
Make hardhat-ethers compatible with ENS when Hardhat forks a remote network
Hey @thegostep. Can you explain what this means? I don't get it.
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
This would be really helpful! Happy to spec out more.
Created a minimal reproducible example cc @alcuadrado @fvictorio https://github.com/tmm/hardhat-fork-with-ens
Thanks, @tmm! Now I understand what you meant.
We should override this method in our ethers provider so that it uses the forked network.
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
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 .
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
Sure, feel free to send a PR!