Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
@nomicfoundation/hardhat-ethers
Advanced tools
Hardhat plugin for integration with ethers.js.
This plugin brings to Hardhat the Ethereum library ethers.js
, which allows you to interact with the Ethereum blockchain in a simple way.
npm install --save-dev @nomicfoundation/hardhat-ethers ethers
And add the following statement to your hardhat.config.js
:
require("@nomicfoundation/hardhat-ethers");
Or, if you are using TypeScript, add this to your hardhat.config.ts
:
import "@nomicfoundation/hardhat-ethers";
This plugin creates no additional tasks.
This plugins adds an ethers
object to the Hardhat Runtime Environment.
This object has the same API as ethers.js
, with some extra Hardhat-specific functionality.
A provider
field is added to ethers
, which is an ethers.Provider
automatically connected to the selected network.
These helpers are added to the ethers
object:
interface Libraries {
[libraryName: string]: string;
}
interface FactoryOptions {
signer?: ethers.Signer;
libraries?: Libraries;
}
function deployContract(name: string, constructorArgs?: any[], signer?: ethers.Signer): Promise<ethers.Contract>;
function getContractFactory(name: string, signer?: ethers.Signer): Promise<ethers.ContractFactory>;
function getContractFactory(name: string, factoryOptions: FactoryOptions): Promise<ethers.ContractFactory>;
function getContractFactory(abi: any[], bytecode: ethers.utils.BytesLike, signer?: ethers.Signer): Promise<ethers.ContractFactory>;
function getContractAt(name: string, address: string, signer?: ethers.Signer): Promise<ethers.Contract>;
function getContractAt(abi: any[], address: string, signer?: ethers.Signer): Promise<ethers.Contract>;
function getSigners() => Promise<ethers.Signer[]>;
function getSigner(address: string) => Promise<ethers.Signer>;
function getImpersonatedSigner(address: string) => Promise<ethers.Signer>;
function getContractFactoryFromArtifact(artifact: Artifact, signer?: ethers.Signer): Promise<ethers.ContractFactory>;
function getContractFactoryFromArtifact(artifact: Artifact, factoryOptions: FactoryOptions): Promise<ethers.ContractFactory>;
function getContractAtFromArtifact(artifact: Artifact, address: string, signer?: ethers.Signer): Promise<ethers.Contract>;
The Contract
s and ContractFactory
s returned by these helpers are connected to the first signer returned by getSigners
by default.
There are no additional steps you need to take for this plugin to work.
Install it and access ethers through the Hardhat Runtime Environment anywhere you need it (tasks, scripts, tests, etc). For example, in your hardhat.config.js
:
require("@nomicfoundation/hardhat-ethers");
// task action function receives the Hardhat Runtime Environment as second argument
task(
"blockNumber",
"Prints the current block number",
async (_, { ethers }) => {
const blockNumber = await ethers.provider.getBlockNumber();
console.log("Current block number: " + blockNumber);
}
);
module.exports = {};
And then run npx hardhat blockNumber
to try it.
Read the documentation on the Hardhat Runtime Environment to learn how to access the HRE in different ways to use ethers.js from anywhere the HRE is accessible.
Some contracts need to be linked with libraries before they are deployed. You can pass the addresses of their libraries to the getContractFactory
function with an object like this:
const contractFactory = await this.env.ethers.getContractFactory("Example", {
libraries: {
ExampleLib: "0x...",
},
});
This allows you to create a contract factory for the Example
contract and link its ExampleLib
library references to the address "0x..."
.
To create a contract factory, all libraries must be linked. An error will be thrown informing you of any missing library.
Ethers.js polls the network to check if some event was emitted (except when a WebSocketProvider
is used; see below). This polling is done every 4 seconds. If you have a script or test that is not emitting an event, it's likely that the execution is finishing before the event is detected by the polling mechanism.
If you are connecting to a Hardhat node using a WebSocketProvider
, events should be emitted immediately. But keep in mind that you'll have to create this provider manually, since Hardhat only supports configuring networks via http. That is, you can't add a localhost
network with a URL like ws://127.0.0.1:8545
.
FAQs
Hardhat plugin for ethers
The npm package @nomicfoundation/hardhat-ethers receives a total of 46,783 weekly downloads. As such, @nomicfoundation/hardhat-ethers popularity was classified as popular.
We found that @nomicfoundation/hardhat-ethers demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 6 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.