Hardhat, ether.js - fetching balance of signers locally shows no ether

I am trying to fetch the balance of signers in a test locally. I didn't change the default value of eth for accounts in the hardhat configuration. Yet when I query the balance, I see that the accounts do not have any balance.

Here is my code:

        [owner, alice, bob, chuck, ...others] = await ethers.getSigners();
        
        provider = ethers.getDefaultProvider();

        balance = await provider.getBalance(owner.address);
        console.log(balance.toString()); // 0

What am I missing?

Answers 1

  • The issue was the provider returned by getDefaultProvider connected to the main net, while I was trying to test locally. I ended up at this conclusion by calling the getNetwork call on the provider, seeing that the network being used was in fact, the main net.

    To fix this, I looked up hardhat docs, and hardhat provides a custom provider in ethers object which can be accessed using ethers.provider.

    So the fix was using:

        provider = ethers.provider;
    

Related Questions