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
- Some helper methods are also added, like
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
With respect to the difference between
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.