Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@nomicfoundation/hardhat-ethers

NomicFoundation603.4kMIT4.0.2TypeScript support: included

Hardhat plugin for ethers

ethereum, smart-contracts, hardhat, ethers.js

readme

hardhat-ethers

This plugin integrates ethers.js into Hardhat, adding an ethers object to each network connection.

Installation

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],
};

Usage

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.

Provider

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.

Deploying contracts

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());

Library linking

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.

API

provider

An 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");

deployContract

Deploys 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);

getContractFactory

Returns 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();

getContractAt

Returns 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);

getSigners

Returns 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();

getSigner

Returns a specific ethers.js signer by its address.

function getSigner(address: string): Promise<ethers.Signer>;

For example:

const signer = await ethers.getSigner("0x1234...abcd");

getImpersonatedSigner

Like 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");

getContractFactoryFromArtifact

Like 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>;

getContractAtFromArtifact

Like getContractAt, but receives a Hardhat artifact.

function getContractAtFromArtifact(
  artifact: Artifact,
  address: string,
  signer?: ethers.Signer,
): Promise<ethers.Contract>;

changelog

@nomicfoundation/hardhat-ethers

4.0.2

Patch Changes

  • 138d673: Added network.createServer(...) to spawn a Hardhat node programmatically (#6472)

4.0.1

Patch Changes

  • 27d52b7: Fixed index resolution in clearEventListeners (#7359)

4.0.0

Major Changes

  • 29cc141: First release of Hardhat 3!

3.1.0

Minor Changes

  • 14b3042: Updated the minimal supported version of Node to v20 (#6982)

3.0.9

Patch Changes

  • d77ecab: Update ethers to v6.14.0 with Pectra support

3.0.8

Patch Changes

  • efa905d: Fix for corrupted Hardhat peer dependency version from pnpm.

3.0.7

Patch Changes

  • 93b30d5: Fix for getSigners against networks where eth_accounts is deprecated.

3.0.6

Patch Changes

  • 55924a7: Fixed a race condition in our ethers provider
  • 1d43aba: Updated the max fee per gas calculation to use eth_maxPriorityFeePerGas when available

3.0.5

Patch Changes

  • ebe5a5fe3: Added support for passing bigints as block tags

3.0.4

Patch Changes

  • 487cd4a81: Reduced the load time of the plugin
  • 84283d119: Fixed two issues related to contract.on (https://github.com/NomicFoundation/hardhat/issues/4098). The first one was about events with indexed arguments not being handled correctly. The second one was related to transactions that emitted the same event twice or more.

3.0.3

Patch Changes

  • a1e37a40b: Added support for listening for events with contract.on

3.0.2

Patch Changes

  • eb1ae069b: Fixed a problem when waitForDeployment was used in live networks.

3.0.1

Patch Changes

  • a9c159f96: The helper.deployContract now accepts transaction overrides

2.2.3

Patch Changes

  • 6dccd2915: Make getContractFactory's params validation more flexible.

2.2.2

Patch Changes

  • 7e013fa19: Upgrade undici

2.2.1

Patch Changes

  • 136f25a9e: getContractAt doesn't throw anymore if the given address is not a contract.

2.2.0

Minor Changes

  • f0310ec91: Add a deployContract helper

2.1.1

Patch Changes

  • fa2a98c8a: getContractAt() now throws an error if the address is not of a contract.

2.1.0

Minor Changes

  • 0d4a68043: Added new helper getImpersonatedSigner(), a shorthand for invoking the hardhat_impersonateAccount JSON-RPC method followed immediately by ethers.getSigner().

2.0.6

Patch Changes

  • 7403ec1d: Stop publishing tsconfig.json files

2.0.5

Patch Changes

  • 1de2a228: Fix an issue that was causing typescript projects to also compile Hardhat's source (#2260).

2.0.4

Patch Changes

  • 6afeeffe: Add equivalents in hardhat-ethers for getContractFactory and getContractAt that support passing Artifact, specifically getContractFactoryFromArtifact and getContractAtFromArtifact (issue #1716)

2.0.3

Patch Changes

  • def9cbb2: Reset the hardhat-ethers provider when a snapshot is reverted (issue #1247)
  • 571ef80d: Adds a custom formatter to better display BigNumber's in Hardhat console (issue #2109).