Skip to content

Commit

Permalink
Merge pull request #4570 from NomicFoundation/web3-v4
Browse files Browse the repository at this point in the history
Add hardhat-web3-v4 package
  • Loading branch information
fvictorio authored Nov 13, 2023
2 parents 2f76ab6 + 76bf45b commit a6ea606
Show file tree
Hide file tree
Showing 21 changed files with 819 additions and 15 deletions.
81 changes: 81 additions & 0 deletions .github/workflows/hardhat-web3-v4-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
name: hardhat-web3-v4 CI

on:
push:
branches: [$default-branch]
paths:
- "packages/hardhat-web3-v4/**"
- "packages/hardhat-core/**"
- "packages/hardhat-common/**"
- "config/**"
pull_request:
branches:
- "**"
paths:
- "packages/hardhat-web3-v4/**"
- "packages/hardhat-core/**"
- "packages/hardhat-common/**"
- "config/**"
workflow_dispatch:

defaults:
run:
working-directory: packages/hardhat-web3-v4

concurrency:
group: ${{github.workflow}}-${{github.ref}}
cancel-in-progress: true

jobs:
test_on_windows:
name: Test hardhat-web3-v4 on Windows with Node 16
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 16
cache: yarn
- name: Install
run: yarn --frozen-lockfile
- name: Build
run: yarn build
- name: Run tests
run: yarn test

test_on_macos:
name: Test hardhat-web3-v4 on MacOS with Node 16
runs-on: macos-latest
# disable until actions/virtual-environments#4896 is fixed
if: ${{ false }}
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 16
cache: yarn
- name: Install
run: yarn --frozen-lockfile
- name: Build
run: yarn build
- name: Run tests
run: yarn test

test_on_linux:
name: Test hardhat-web3-v4 on Ubuntu with Node ${{ matrix.node }}
runs-on: ubuntu-latest
strategy:
matrix:
node: [16, 18, 20]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node }}
cache: yarn
- name: Install
run: yarn --frozen-lockfile
- name: Build
run: yarn build
- name: Run tests
run: yarn test
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
"wsrun": "^5.2.2"
},
"scripts": {
"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",
"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",
"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",
"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",
"clean": "wsrun --exclude-missing clean",
"test": "node scripts/run-tests.js",
"lint": "wsrun --exclude-missing --stages lint && yarn prettier --check",
Expand Down
7 changes: 7 additions & 0 deletions packages/hardhat-web3-v4/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
extends: [`${__dirname}/../../config/eslint/eslintrc.js`],
parserOptions: {
project: `${__dirname}/tsconfig.json`,
sourceType: "module",
},
};
97 changes: 97 additions & 0 deletions packages/hardhat-web3-v4/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Node modules
/node_modules

# Compilation output
/build-test/
/dist

# Code coverage artifacts
/coverage
/.nyc_output

# Below is Github's node gitignore template,
# ignoring the node_modules part, as it'd ignore every node_modules, and we have some for testing

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
#node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# parcel-bundler cache (https://parceljs.org/)
.cache

# next.js build output
.next

# nuxt.js build output
.nuxt

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

artifacts
cache
6 changes: 6 additions & 0 deletions packages/hardhat-web3-v4/.mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"require": "ts-node/register/files",
"file": "../common/run-with-ganache.js",
"ignore": ["test/fixture-projects/**/*"],
"timeout": 10000
}
5 changes: 5 additions & 0 deletions packages/hardhat-web3-v4/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/node_modules
/dist
/test/fixture-projects/**/artifacts
/test/fixture-projects/**/cache
CHANGELOG.md
21 changes: 21 additions & 0 deletions packages/hardhat-web3-v4/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Nomic Foundation

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
59 changes: 59 additions & 0 deletions packages/hardhat-web3-v4/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
[![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)

# hardhat-web3-v4

_This plugin is a collaboration between the Nomic Foundation and [ChainSafe](https://chainsafe.io/)_

Integrate [Web3.js](https://github.com/ethereum/web3.js) `4.x` into [Hardhat](https://hardhat.org).

## What

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

# Installation

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

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

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

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

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

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/).

## 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`:

```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](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.
60 changes: 60 additions & 0 deletions packages/hardhat-web3-v4/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"name": "@nomicfoundation/hardhat-web3-v4",
"version": "1.0.0",
"author": "Nomic Foundation",
"license": "MIT",
"homepage": "https://github.com/nomicfoundation/hardhat/tree/main/packages/hardhat-web3-v4",
"repository": "github:nomicfoundation/hardhat",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
"description": "Hardhat plugin for using Web3 4.x",
"keywords": [
"ethereum",
"smart-contracts",
"hardhat",
"hardhat-plugin",
"web3",
"web3.js"
],
"scripts": {
"lint": "yarn prettier --check && yarn eslint",
"lint:fix": "yarn prettier --write && yarn eslint --fix",
"eslint": "eslint 'src/**/*.ts' 'test/**/*.ts'",
"prettier": "prettier \"**/*.{js,md,json}\"",
"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",
"build": "tsc --build .",
"prepublishOnly": "yarn build",
"clean": "rimraf dist"
},
"files": [
"dist/src/",
"src/",
"LICENSE",
"README.md"
],
"devDependencies": {
"@types/chai": "^4.2.0",
"@types/mocha": ">=9.1.0",
"@types/node": "^16.0.0",
"@typescript-eslint/eslint-plugin": "5.61.0",
"@typescript-eslint/parser": "5.61.0",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"eslint": "^8.44.0",
"eslint-config-prettier": "8.3.0",
"eslint-plugin-import": "2.27.5",
"eslint-plugin-no-only-tests": "3.0.0",
"eslint-plugin-prettier": "3.4.0",
"hardhat": "^2.0.0",
"mocha": "^10.0.0",
"prettier": "2.4.1",
"rimraf": "^3.0.2",
"ts-node": "^10.8.0",
"typescript": "~5.0.0",
"web3": "^4.0.1"
},
"peerDependencies": {
"hardhat": "^2.0.0",
"web3": "^4.0.1"
}
}
12 changes: 12 additions & 0 deletions packages/hardhat-web3-v4/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { extendEnvironment } from "hardhat/config";
import { lazyFunction, lazyObject } from "hardhat/plugins";

import "./type-extensions";

extendEnvironment((env) => {
env.Web3 = lazyFunction(() => require("web3").Web3);
env.web3 = lazyObject(() => {
const Web3 = require("web3").Web3;
return new Web3(env.network.provider);
});
});
10 changes: 10 additions & 0 deletions packages/hardhat-web3-v4/src/type-extensions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type * as Web3 from "web3";

import "hardhat/types/runtime";

declare module "hardhat/types/runtime" {
interface HardhatRuntimeEnvironment {
Web3: typeof Web3;
web3: Web3.Web3;
}
}
11 changes: 11 additions & 0 deletions packages/hardhat-web3-v4/test/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
extends: [`${__dirname}/../.eslintrc.js`],
rules: {
"import/no-extraneous-dependencies": [
"error",
{
devDependencies: true,
},
],
},
};
Loading

0 comments on commit a6ea606

Please sign in to comment.