I am using hardhat and ethers.js for testing and when I call

WETHSigner = await ethers.provider.getSigner(WETHHolderAddress);

it fails but this works:

WETHSigner = await ethers.getSigner(WETHHolderAddress);

Can someone explain with an example between a provider and a network?


When you use the @nomiclabs/hardhat-ethers plugin, the ethers object in the Hardhat Runtime Environment has a couple of extra properties added:

  • ethers.provider is an ethers provider connected to the chosen network. So if you run a script with, for example, hh run script.js --network localhost, then ethers.provider will be connected to http://localhost:8545. You can use it as any ethers provider, and call methods like ethers.provider.getBlockNumber.
  • Some helper methods are also added, like ethers.getSigner or ethers.getContractFactory. You can check the full list in the plugin's docs.

Besides this, you also have the rest of the properties/methods exposed by ethers itself, like ethers.utils.

With respect to the difference between ethers.getSigner and ethers.provider.getSigner, they are pretty much equivalent, except for two things:

  • ethers.getSigner returns a promise, while ethers.provider.getSigner don't. That is, you need to use await in the first case.
  • The Signer returned by ethers.getSigner is a wrapper created by the plugin that behaves pretty much exactly like the one returned by ethers.provider.getSigner, except that it has a .address property, so you don't need to call signer.getAddress(), which is an async function.
  • Thanks for this answer! But What's the difference between ethers.provider.getSigner and ethers.getSigner ? Jul 14 at 17:12
  • Sorry, I missed that that was the main point of your question. I actually don't know why the first case fails for you, so you should expand on that. As I explained in the (edited) answer, they are pretty much the same thing, except for the two differences I explained. Jul 14 at 17:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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