npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@nomicfoundation/hardhat-ethers

v4.0.3

Published

Hardhat plugin for ethers

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 { defineConfig } from "hardhat/config";
import hardhatEthers from "@nomicfoundation/hardhat-ethers";

export default defineConfig({
  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>;