Skip to content

Commit a6ea606

Browse files
authored
Merge pull request #4570 from NomicFoundation/web3-v4
Add hardhat-web3-v4 package
2 parents 2f76ab6 + 76bf45b commit a6ea606

21 files changed

+819
-15
lines changed
+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
name: hardhat-web3-v4 CI
2+
3+
on:
4+
push:
5+
branches: [$default-branch]
6+
paths:
7+
- "packages/hardhat-web3-v4/**"
8+
- "packages/hardhat-core/**"
9+
- "packages/hardhat-common/**"
10+
- "config/**"
11+
pull_request:
12+
branches:
13+
- "**"
14+
paths:
15+
- "packages/hardhat-web3-v4/**"
16+
- "packages/hardhat-core/**"
17+
- "packages/hardhat-common/**"
18+
- "config/**"
19+
workflow_dispatch:
20+
21+
defaults:
22+
run:
23+
working-directory: packages/hardhat-web3-v4
24+
25+
concurrency:
26+
group: ${{github.workflow}}-${{github.ref}}
27+
cancel-in-progress: true
28+
29+
jobs:
30+
test_on_windows:
31+
name: Test hardhat-web3-v4 on Windows with Node 16
32+
runs-on: windows-latest
33+
steps:
34+
- uses: actions/checkout@v2
35+
- uses: actions/setup-node@v2
36+
with:
37+
node-version: 16
38+
cache: yarn
39+
- name: Install
40+
run: yarn --frozen-lockfile
41+
- name: Build
42+
run: yarn build
43+
- name: Run tests
44+
run: yarn test
45+
46+
test_on_macos:
47+
name: Test hardhat-web3-v4 on MacOS with Node 16
48+
runs-on: macos-latest
49+
# disable until actions/virtual-environments#4896 is fixed
50+
if: ${{ false }}
51+
steps:
52+
- uses: actions/checkout@v2
53+
- uses: actions/setup-node@v2
54+
with:
55+
node-version: 16
56+
cache: yarn
57+
- name: Install
58+
run: yarn --frozen-lockfile
59+
- name: Build
60+
run: yarn build
61+
- name: Run tests
62+
run: yarn test
63+
64+
test_on_linux:
65+
name: Test hardhat-web3-v4 on Ubuntu with Node ${{ matrix.node }}
66+
runs-on: ubuntu-latest
67+
strategy:
68+
matrix:
69+
node: [16, 18, 20]
70+
steps:
71+
- uses: actions/checkout@v2
72+
- uses: actions/setup-node@v2
73+
with:
74+
node-version: ${{ matrix.node }}
75+
cache: yarn
76+
- name: Install
77+
run: yarn --frozen-lockfile
78+
- name: Build
79+
run: yarn build
80+
- name: Run tests
81+
run: yarn test

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
"wsrun": "^5.2.2"
2424
},
2525
"scripts": {
26-
"build": "tsc --build packages/hardhat-core packages/hardhat-ethers packages/hardhat-verify packages/hardhat-solhint packages/hardhat-solpp packages/hardhat-truffle4 packages/hardhat-truffle5 packages/hardhat-vyper packages/hardhat-web3 packages/hardhat-web3-legacy packages/hardhat-chai-matchers packages/hardhat-network-helpers packages/hardhat-toolbox packages/hardhat-foundry packages/hardhat-ledger packages/hardhat-viem packages/hardhat-toolbox-viem",
27-
"watch": "tsc --build --watch packages/hardhat-core/src packages/hardhat-ethers packages/hardhat-verify packages/hardhat-solhint packages/hardhat-solpp packages/hardhat-truffle4 packages/hardhat-truffle5 packages/hardhat-vyper packages/hardhat-web3 packages/hardhat-web3-legacy packages/hardhat-chai-matchers packages/hardhat-network-helpers packages/hardhat-toolbox packages/hardhat-foundry packages/hardhat-ledger packages/hardhat-viem packages/hardhat-toolbox-viem",
26+
"build": "tsc --build packages/hardhat-core packages/hardhat-ethers packages/hardhat-verify packages/hardhat-solhint packages/hardhat-solpp packages/hardhat-truffle4 packages/hardhat-truffle5 packages/hardhat-vyper packages/hardhat-web3 packages/hardhat-web3-v4 packages/hardhat-web3-legacy packages/hardhat-chai-matchers packages/hardhat-network-helpers packages/hardhat-toolbox packages/hardhat-foundry packages/hardhat-ledger packages/hardhat-viem packages/hardhat-toolbox-viem",
27+
"watch": "tsc --build --watch packages/hardhat-core/src packages/hardhat-ethers packages/hardhat-verify packages/hardhat-solhint packages/hardhat-solpp packages/hardhat-truffle4 packages/hardhat-truffle5 packages/hardhat-vyper packages/hardhat-web3 packages/hardhat-web3-v4 packages/hardhat-web3-legacy packages/hardhat-chai-matchers packages/hardhat-network-helpers packages/hardhat-toolbox packages/hardhat-foundry packages/hardhat-ledger packages/hardhat-viem packages/hardhat-toolbox-viem",
2828
"clean": "wsrun --exclude-missing clean",
2929
"test": "node scripts/run-tests.js",
3030
"lint": "wsrun --exclude-missing --stages lint && yarn prettier --check",

packages/hardhat-web3-v4/.eslintrc.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
extends: [`${__dirname}/../../config/eslint/eslintrc.js`],
3+
parserOptions: {
4+
project: `${__dirname}/tsconfig.json`,
5+
sourceType: "module",
6+
},
7+
};

packages/hardhat-web3-v4/.gitignore

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Node modules
2+
/node_modules
3+
4+
# Compilation output
5+
/build-test/
6+
/dist
7+
8+
# Code coverage artifacts
9+
/coverage
10+
/.nyc_output
11+
12+
# Below is Github's node gitignore template,
13+
# ignoring the node_modules part, as it'd ignore every node_modules, and we have some for testing
14+
15+
# Logs
16+
logs
17+
*.log
18+
npm-debug.log*
19+
yarn-debug.log*
20+
yarn-error.log*
21+
lerna-debug.log*
22+
23+
# Diagnostic reports (https://nodejs.org/api/report.html)
24+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
25+
26+
# Runtime data
27+
pids
28+
*.pid
29+
*.seed
30+
*.pid.lock
31+
32+
# Directory for instrumented libs generated by jscoverage/JSCover
33+
lib-cov
34+
35+
# Coverage directory used by tools like istanbul
36+
coverage
37+
38+
# nyc test coverage
39+
.nyc_output
40+
41+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
42+
.grunt
43+
44+
# Bower dependency directory (https://bower.io/)
45+
bower_components
46+
47+
# node-waf configuration
48+
.lock-wscript
49+
50+
# Compiled binary addons (https://nodejs.org/api/addons.html)
51+
build/Release
52+
53+
# Dependency directories
54+
#node_modules/
55+
jspm_packages/
56+
57+
# TypeScript v1 declaration files
58+
typings/
59+
60+
# Optional npm cache directory
61+
.npm
62+
63+
# Optional eslint cache
64+
.eslintcache
65+
66+
# Optional REPL history
67+
.node_repl_history
68+
69+
# Output of 'npm pack'
70+
*.tgz
71+
72+
# Yarn Integrity file
73+
.yarn-integrity
74+
75+
# parcel-bundler cache (https://parceljs.org/)
76+
.cache
77+
78+
# next.js build output
79+
.next
80+
81+
# nuxt.js build output
82+
.nuxt
83+
84+
# vuepress build output
85+
.vuepress/dist
86+
87+
# Serverless directories
88+
.serverless/
89+
90+
# FuseBox cache
91+
.fusebox/
92+
93+
# DynamoDB Local files
94+
.dynamodb/
95+
96+
artifacts
97+
cache
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"require": "ts-node/register/files",
3+
"file": "../common/run-with-ganache.js",
4+
"ignore": ["test/fixture-projects/**/*"],
5+
"timeout": 10000
6+
}
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/node_modules
2+
/dist
3+
/test/fixture-projects/**/artifacts
4+
/test/fixture-projects/**/cache
5+
CHANGELOG.md

packages/hardhat-web3-v4/LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Nomic Foundation
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

packages/hardhat-web3-v4/README.md

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
[![npm](https://img.shields.io/npm/v/@nomicfoundation/hardhat-web3-v4.svg)](https://www.npmjs.com/package/@nomicfoundation/hardhat-web3-v4) [![hardhat](https://hardhat.org/buidler-plugin-badge.svg?1)](https://hardhat.org)
2+
3+
# hardhat-web3-v4
4+
5+
_This plugin is a collaboration between the Nomic Foundation and [ChainSafe](https://chainsafe.io/)_
6+
7+
Integrate [Web3.js](https://github.com/ethereum/web3.js) `4.x` into [Hardhat](https://hardhat.org).
8+
9+
## What
10+
11+
This plugin brings to Hardhat the Web3 module and an initialized instance of Web3.
12+
13+
# Installation
14+
15+
```bash
16+
npm install --save-dev @nomicfoundation/hardhat-web3-v4 'web3@4'
17+
```
18+
19+
And add the following statement to your `hardhat.config.js`:
20+
21+
```js
22+
require("@nomicfoundation/hardhat-web3-v4");
23+
```
24+
25+
Or, if you are using TypeScript, add this to your `hardhat.config.ts`:
26+
27+
```js
28+
import "@nomifoundation/hardhat-web3-v4";
29+
```
30+
31+
By default, contract invocations will not be typesafe. Consider installing [@chainsafe/hardhat-ts-artifact-plugin](https://www.npmjs.com/package/@chainsafe/hardhat-ts-artifact-plugin) to obtain available contract methods and events. Read more about inferring types [here](https://docs.web3js.org/guides/smart_contracts/infer_contract_types_guide/).
32+
33+
## Tasks
34+
35+
This plugin creates no additional tasks.
36+
37+
## Environment extensions
38+
39+
This plugin adds the following elements to the `HardhatRuntimeEnvironment`:
40+
41+
- `Web3`: The Web3.js module.
42+
- `web3`: An instantiated Web3.js object connected to the selected network.
43+
44+
## Usage
45+
46+
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`:
47+
48+
```js
49+
require("@nomicfoundation/hardhat-web3-v4");
50+
51+
// task action function receives the Hardhat Runtime Environment as second argument
52+
task("accounts", "Prints accounts", async (_, { web3 }) => {
53+
console.log(await web3.eth.getAccounts());
54+
});
55+
```
56+
57+
And then run `npx hardhat accounts` to try it.
58+
59+
Read the documentation on the [Hardhat Runtime Environment](https://hardhat.org/hardhat-runner/docs/advanced/hardhat-runtime-environment) to learn how to access the HRE in different ways to use Web3.js from anywhere the HRE is accessible.

packages/hardhat-web3-v4/package.json

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
{
2+
"name": "@nomicfoundation/hardhat-web3-v4",
3+
"version": "1.0.0",
4+
"author": "Nomic Foundation",
5+
"license": "MIT",
6+
"homepage": "https://github.com/nomicfoundation/hardhat/tree/main/packages/hardhat-web3-v4",
7+
"repository": "github:nomicfoundation/hardhat",
8+
"main": "dist/src/index.js",
9+
"types": "dist/src/index.d.ts",
10+
"description": "Hardhat plugin for using Web3 4.x",
11+
"keywords": [
12+
"ethereum",
13+
"smart-contracts",
14+
"hardhat",
15+
"hardhat-plugin",
16+
"web3",
17+
"web3.js"
18+
],
19+
"scripts": {
20+
"lint": "yarn prettier --check && yarn eslint",
21+
"lint:fix": "yarn prettier --write && yarn eslint --fix",
22+
"eslint": "eslint 'src/**/*.ts' 'test/**/*.ts'",
23+
"prettier": "prettier \"**/*.{js,md,json}\"",
24+
"test": "mocha --recursive \"test/**/*.ts\" --exit && node web3-lazy-object-tests/when-accessing-web3-class.js && node web3-lazy-object-tests/when-accessing-web3-object.js && node web3-lazy-object-tests/when-requiring-web3-module.js",
25+
"build": "tsc --build .",
26+
"prepublishOnly": "yarn build",
27+
"clean": "rimraf dist"
28+
},
29+
"files": [
30+
"dist/src/",
31+
"src/",
32+
"LICENSE",
33+
"README.md"
34+
],
35+
"devDependencies": {
36+
"@types/chai": "^4.2.0",
37+
"@types/mocha": ">=9.1.0",
38+
"@types/node": "^16.0.0",
39+
"@typescript-eslint/eslint-plugin": "5.61.0",
40+
"@typescript-eslint/parser": "5.61.0",
41+
"chai": "^4.2.0",
42+
"chai-as-promised": "^7.1.1",
43+
"eslint": "^8.44.0",
44+
"eslint-config-prettier": "8.3.0",
45+
"eslint-plugin-import": "2.27.5",
46+
"eslint-plugin-no-only-tests": "3.0.0",
47+
"eslint-plugin-prettier": "3.4.0",
48+
"hardhat": "^2.0.0",
49+
"mocha": "^10.0.0",
50+
"prettier": "2.4.1",
51+
"rimraf": "^3.0.2",
52+
"ts-node": "^10.8.0",
53+
"typescript": "~5.0.0",
54+
"web3": "^4.0.1"
55+
},
56+
"peerDependencies": {
57+
"hardhat": "^2.0.0",
58+
"web3": "^4.0.1"
59+
}
60+
}

packages/hardhat-web3-v4/src/index.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { extendEnvironment } from "hardhat/config";
2+
import { lazyFunction, lazyObject } from "hardhat/plugins";
3+
4+
import "./type-extensions";
5+
6+
extendEnvironment((env) => {
7+
env.Web3 = lazyFunction(() => require("web3").Web3);
8+
env.web3 = lazyObject(() => {
9+
const Web3 = require("web3").Web3;
10+
return new Web3(env.network.provider);
11+
});
12+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type * as Web3 from "web3";
2+
3+
import "hardhat/types/runtime";
4+
5+
declare module "hardhat/types/runtime" {
6+
interface HardhatRuntimeEnvironment {
7+
Web3: typeof Web3;
8+
web3: Web3.Web3;
9+
}
10+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module.exports = {
2+
extends: [`${__dirname}/../.eslintrc.js`],
3+
rules: {
4+
"import/no-extraneous-dependencies": [
5+
"error",
6+
{
7+
devDependencies: true,
8+
},
9+
],
10+
},
11+
};

0 commit comments

Comments
 (0)