>
>
Complete the 2023 Solidity Survey here
>
>
npm
hardhat

#hardhat-web3-v4

This plugin is a collaboration between the Nomic Foundation and ChainSafe

Integrate Web3.js 4.x into Hardhat.

# What

This plugin brings to Hardhat the Web3 module and an initialized instance of Web3.

#Installation

npm install --save-dev @nomicfoundation/hardhat-web3-v4 'web3@4'

And add the following statement to your hardhat.config.js:

require("@nomicfoundation/hardhat-web3-v4");

Or, if you are using TypeScript, add this to your hardhat.config.ts:

import "@nomifoundation/hardhat-web3-v4";

By default, contract invocations will not be typesafe. Consider installing @chainsafe/hardhat-ts-artifact-plugin to obtain available contract methods and events. Read more about inferring types here.

# Tasks

This plugin creates no additional tasks.

# Environment extensions

This plugin adds the following elements to the HardhatRuntimeEnvironment:

  • Web3: The Web3.js module.
  • web3: An instantiated Web3.js object connected to the selected network.

# Usage

Install it and access Web3.js through the Hardhat Runtime Environment anywhere you need it (tasks, scripts, tests, etc). For example, in your hardhat.config.js:

require("@nomicfoundation/hardhat-web3-v4");

// task action function receives the Hardhat Runtime Environment as second argument
task("accounts", "Prints accounts", async (_, { web3 }) => {
  console.log(await web3.eth.getAccounts());
});

And then run npx hardhat accounts to try it.

Read the documentation on the Hardhat Runtime Environment to learn how to access the HRE in different ways to use Web3.js from anywhere the HRE is accessible.