1

I'm new to hardhat and tried to run some tests. Everything works fine until I try to execute payable functions. Addresses generated with ethers.getSigners(); have no eth on them. Which made me realise that when running npx hardhat test it's actually set on mainnet for some reason. Logging network gives me:

network is: { name: 'homestead', chainId: 1, ensAddress: '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e', _defaultProvider: [Function: func] { renetwork: [Function (anonymous)] } }

How do I go on about switching back to testing locally with hardhat network? I've added this to my hardhat.config.js

networks: { hardhat: { chainId: 31337 } } But it stills run tests on chainId 1. Even when trying npx hardhat test --network localhost

Any ideas how I could fix that pls ?

1 Answer 1

2

For anyone looking for the answer, actually for some reason even tho it IS indeed the hardhat local that is running the test in the background, .getDefaultProvider().getNetwork() shows "homestead" chainId 1, and getBalance of an address will show 0 eth...

So in order to access provider when testing locally without a network specified, you should use ethers.provider.getBalance(xxx) and not ethers.getDefaultProvider().getBalance(xxx)

1
  • You just saved me so much time, this is 100% the issue if you are testing locally. Commented May 27, 2023 at 15:11

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.