
Product
Introducing GitHub Actions Scanning Support
Detect malware, unsafe data flows, and license issues in GitHub Actions with Socket’s new workflow scanning support.
@nomicfoundation/hardhat-ethers
Advanced tools
This plugin integrates ethers.js into Hardhat, adding an ethers object to each network connection.
This plugin is part of the Ethers+Mocha Hardhat Toolbox. If you are using that toolbox, there's nothing else you need to do.
To install this plugin, run the following command:
npm install --save-dev @nomicfoundation/hardhat-ethers
In your hardhat.config.ts file, import the plugin and add it to the plugins array:
import hardhatEthers from "@nomicfoundation/hardhat-ethers";
export default {
plugins: [hardhatEthers],
};
This plugin adds an ethers property to each network connection:
import { network } from "hardhat";
const { ethers } = await network.connect();
const counter = await ethers.deployContract("Counter");
await counter.inc();
console.log(await counter.x());
This object has the same API as ethers.js, with some extra Hardhat-specific functionality. The rest of this section describes the most important extra features. Check the API reference below for the complete list of extensions.
The plugin adds a provider property to the ethers object: an ethers.js provider connected to the network selected by network.connect().
const blockNumber = await ethers.provider.getBlockNumber();
const balance = await ethers.provider.getBalance(someAddress);
Use ethers.provider to access read-only blockchain data, such as accounts state, block data, or transactions objects.
The hardhat-ethers plugin also adds a deployContract method to the ethers object, which allows you to easily deploy contracts from your project:
const counter = await ethers.deployContract("Counter");
await counter.inc();
console.log(await counter.x());
Some contracts need to be linked with libraries before they are deployed. You can pass the addresses of their libraries to methods like deployContract with an object mapping library names to their addresses:
const counter = await ethers.deployContract("Counter", {
libraries: {
SafeMath: "0x...",
},
});
This allows you to deploy an instance of the Counter contract, linking its SafeMath library to the address "0x...". The plugin will throw an error if you try to deploy a contract or create a factory without linking the necessary libraries.
providerAn ethers.js provider connected to the network you selected when calling network.connect:
// the network selected with --network option if specified, or the default network otherwise
const { ethers } = await network.connect();
// a specific network from the config
const { ethers } = await network.connect("mainnet");
deployContractDeploys a contract from your project.
function deployContract(
name: string,
constructorArgs?: any[],
signer?: ethers.Signer,
): Promise<ethers.Contract>;
Receives the name of the contract to deploy. Most of the time this will be the name of the contract:
const counter = await ethers.deployContract("Counter");
If you have two contracts with the same name in different files, you'll need to use the fully qualified name of the contract, which includes its source name:
const counter = await ethers.deployContract("contracts/Counter.sol:Counter");
If your contract has constructor parameters, you can pass them as the second argument:
const counter = await ethers.deployContract("Counter", [42]);
By default, the contract will be deployed with the first available signer. If you want to use a different one, you can pass it as the third argument:
const [defaultSigner, deployer] = await ethers.getSigners();
const counter = await ethers.deployContract("Counter", [], deployer);
getContractFactoryReturns an ethers.js contract factory.
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>;
It can receive a contract name:
const Counter = await ethers.getContractFactory("Counter");
const counter = await Counter.deploy();
Or an ABI and a deployment bytecode:
const Counter = await ethers.getContractFactory(counterAbi, counterBytecode);
const counter = await Counter.deploy();
By default, the contracts deployed with the factory will use the first signer in the config. If you want to use a different signer, you can pass it as the second argument:
const [defaultSigner, deployer] = await ethers.getSigners();
const Counter = await ethers.getContractFactory("Counter", deployer);
const counter = await Counter.deploy();
getContractAtReturns an ethers.js contract instance connected to a specific address.
function getContractAt(
name: string,
address: string,
signer?: ethers.Signer,
): Promise<ethers.Contract>;
function getContractAt(
abi: any[],
address: string,
signer?: ethers.Signer,
): Promise<ethers.Contract>;
It can receive a contract name and an address:
const counter = await ethers.getContractAt("Counter", "0x1234...abcd");
Or an ABI and an address:
const counter = await ethers.getContractAt(counterAbi, "0x1234...abcd");
By default, the contract will be connected to the first signer in the config. If you want to use a different signer, you can pass it as the third argument:
const [defaultSigner, caller] = await ethers.getSigners();
const counter = await ethers.getContractAt("Counter", "0x1234...abcd", caller);
getSignersReturns an array of ethers.js signers that correspond to the accounts configured in your Hardhat project.
function getSigners(): Promise<ethers.Signer[]>;
For example:
const signers = await ethers.getSigners();
getSignerReturns a specific ethers.js signer by its address.
function getSigner(address: string): Promise<ethers.Signer>;
For example:
const signer = await ethers.getSigner("0x1234...abcd");
getImpersonatedSignerLike getSigner, but it impersonates the given address, allowing you to use it even if you don't have its private key.
function getImpersonatedSigner(address: string): Promise<ethers.Signer>;
For example:
const impersonatedSigner = await ethers.getImpersonatedSigner("0x1234...abcd");
getContractFactoryFromArtifactLike getContractFactory, but receives a Hardhat artifact.
function getContractFactoryFromArtifact(
artifact: Artifact,
signer?: ethers.Signer,
): Promise<ethers.ContractFactory>;
function getContractFactoryFromArtifact(
artifact: Artifact,
factoryOptions: FactoryOptions,
): Promise<ethers.ContractFactory>;
getContractAtFromArtifactLike getContractAt, but receives a Hardhat artifact.
function getContractAtFromArtifact(
artifact: Artifact,
address: string,
signer?: ethers.Signer,
): Promise<ethers.Contract>;
Truffle is a popular Ethereum development framework that provides similar functionalities to Hardhat, including contract deployment, testing, and interaction. While Truffle is more established and has a larger community, Hardhat offers a more modern and flexible approach with better support for TypeScript and advanced debugging features.
Web3.js is a JavaScript library for interacting with the Ethereum blockchain. It provides similar functionalities to Ethers.js, which is integrated into Hardhat via the @nomicfoundation/hardhat-ethers plugin. Ethers.js is often preferred for its smaller size, better documentation, and more modern API design.
FAQs
Hardhat plugin for ethers
The npm package @nomicfoundation/hardhat-ethers receives a total of 186,935 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 1 open source maintainer 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
Detect malware, unsafe data flows, and license issues in GitHub Actions with Socket’s new workflow scanning support.

Product
Add real-time Socket webhook events to your workflows to automatically receive pull request scan results and security alerts in real time.

Research
The Socket Threat Research Team uncovered malicious NuGet packages typosquatting the popular Nethereum project to steal wallet keys.