Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port 'hardhat-ethers' plugin from V2 to V3 #5787

Merged
merged 24 commits into from
Oct 3, 2024

Conversation

ChristopherDedominici
Copy link
Contributor

@ChristopherDedominici ChristopherDedominici commented Sep 27, 2024

Short overview on how to use the package

const { ethers } = await hre.network.connect();

// ethers functionalities
ethers.isAddress("0x1234567890123456789012345678901234567890");

// ethers.Provider functionalities
await ethers.provider.getBlockNumber();

// Hardhat helper methods
await ethers.getSigners();

Issue to track TODOs as soon as V3 node is ready

Link to issue

Questions: input needed

  • In the old v2 code there is a property called blockGasLimit that is assigned to gasLimit under specific conditions. But currently the new NetworkConfig does not have this property. Is it gonna be supported in the future? Or this logic can be removed?

  • in the code I kept the utils functions from ethers (e.g.: isHexString). The reason behind this choice is to have the code logic as close as possible to the original ethers library. Do we want to keep these ethers utils'functions or do we want to use the hardhat ones from hardhat-utils?

  • the old code was usingsinon, specifically in this file, to ensure that the listener functions were called correctly. I replaced sinon with the builtin nodeJS test runner mocks. Is this ok?

  • the old V2 code had a few TODOs. I kept them, since this PR is only about porting the code from V2 to V3. Is this ok?

  • As mentioned in the design doc, the current implementation is using the mocked ArtifactsManager. For the tests, I copied the code of the mocked artifact manager from this test example and I modified it a bit, see here. Is this ok?
    Also, as mentioned in one of the team syncing calls, the artifacts used in the tests are currently stored in files but they just include the artifact properties and not the code that originated them. See the artifacts folder here. This is because we currently do not know if we're going to use this approach (in this case the artifacts should have additional information like the source code, etc.) or if we're going to use the old approach of compiling the code during the tests. Is it ok to keep the artifact in this format right now?

  • in the old V2 code the hardhat helpers were separated in functions, see here. In V3 I grouped them into a class (see here), to simplify the usage of the dependencies that before were imported from the HardhatRuntimeEnvironment. For example, before the functionalities available in the hre (like hre.artifacts ) had to be passed several times across functions, now they can be directly accessed via this. This approach does not affect how the methods are exposed to the end user. Is this ok?

Notes

  • As mentioned in the design doc, there are a few parts of the code that can only be implemented when V3 has all the features that V2 has. This is the list of those parts:
    • tests
    • property blockGasLimit (if it's still gonna be supported - see the question asked in the previous section)

To find these portions of code that require the missing V3 features you can search in the code for TODO: enable when V3 is ready.

  • In the old V2 code there were a lot of disabled eslint rules. I removed most of them, but a few are required. In the code, they can be found by searching for eslint-disable-next-line. In the ethers-utils.ts file all of them have been kept, because almost all the methods in the file are a verbatim copy of the ethers methods. There are also a few disabled eslint rules in the tests, because there are tests that check errors that are not thrown by Hardhat, so there is a warning when using the assert.rejects method provided by the nodeJS test runner.

  • The old V2 files where quite long, but as agreed in one of the team syncing calls, in V3 we will keep the same file structure as in V2.

Tests

For an explanation on why we currently need a workaround to run the tests, please read the design doc here.

How to run the tests:

  • start a V2 hardhat node with npx hardhat node
  • in the hardhat-ethers package run the command pnpm test:tmp

If you want to run the tests using an INFURA_URL, you can set the env variable INFURA_URL to the desired URL. If the env variable is not set, the test will be skipped.

@ChristopherDedominici ChristopherDedominici added the v-next A Hardhat v3 development task label Sep 27, 2024
@ChristopherDedominici ChristopherDedominici self-assigned this Sep 27, 2024
Copy link

changeset-bot bot commented Sep 27, 2024

⚠️ No Changeset found

Latest commit: 5a170ac

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

vercel bot commented Sep 27, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
hardhat ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 2, 2024 2:16pm

Copy link
Contributor

github-actions bot commented Sep 27, 2024

hardhat

Total size of the bundle: 80M
Total number of dependencies (including transitive): 120

List of dependencies (sorted by size)
78M	total
23M	solc
20M	esbuild
4.9M	lodash
2.9M	fp-ts
2.8M	@sentry/tracing
2.1M	secp256k1
1.9M	@noble/curves
1.6M	undici
1.2M	@sentry/types
1.2M	@noble/hashes
1.1M	@ignored/hardhat-vnext-build-system
996K	@nomicfoundation/ethereumjs-util
952K	node-addon-api
932K	@sentry/node
920K	@sentry/utils
892K	keccak
824K	zod
712K	@ignored/hardhat-vnext-utils
576K	tsx
556K	resolve
548K	@sentry/core
504K	fast-equals
492K	@scure/bip39
464K	@ignored/edr
368K	ethereum-cryptography
344K	@sentry/hub
320K	enquirer
284K	semver
268K	fs-extra
248K	scrypt-js
244K	io-ts
204K	@ignored/hardhat-vnext-errors
200K	undici-types
196K	readable-stream
192K	@nomicfoundation/ethereumjs-rlp
180K	blakejs
176K	elliptic
168K	@scure/base
136K	adm-zip
128K	get-tsconfig
112K	bn.js
104K	hash.js
96K	commander
96K	browserify-aes
96K	@scure/bip32
92K	chalk
88K	tslib
88K	@sentry/minimal
84K	js-sha3
76K	agent-base
72K	sha.js
72K	@nomicfoundation/solidity-analyzer
68K	glob
68K	debug
64K	lru_map
64K	https-proxy-agent
60K	@ignored/hardhat-vnext-zod-utils
56K	rfdc
56K	graceful-fs
56K	follow-redirects
52K	pbkdf2
52K	hmac-drbg
48K	safe-buffer
48K	minimatch
48K	memorystream
48K	command-exists
48K	ansi-colors
44K	tmp
44K	node-gyp-build
40K	resolve-pkg-maps
40K	buffer-xor
36K	klaw
36K	concat-map
32K	string_decoder
32K	jsonfile
32K	fs.realpath
32K	create-hash
32K	cookie
28K	util-deprecate
28K	ripemd160
28K	randombytes
28K	minimalistic-crypto-utils
28K	create-hmac
28K	base-x
24K	strip-ansi
24K	p-map
24K	md5.js
24K	inherits
24K	indent-string
24K	env-paths
24K	clean-stack
24K	cipher-base
24K	bs58check
24K	brorand
24K	brace-expansion
24K	ansi-regex
24K	aggregate-error
24K	@types/secp256k1
20K	wrappy
20K	universalify
20K	setimmediate
20K	require-from-string
20K	path-parse
20K	path-is-absolute
20K	path-exists
20K	p-try
20K	p-locate
20K	p-limit
20K	os-tmpdir
20K	once
20K	ms
20K	minimalistic-assert
20K	locate-path
20K	inflight
20K	hash-base
20K	find-up
20K	evp_bytestokey
20K	bs58
20K	balanced-match
20K	@types/pbkdf2

@ChristopherDedominici ChristopherDedominici linked an issue Sep 27, 2024 that may be closed by this pull request
2 tasks
@@ -0,0 +1,14 @@
import type { Artifact } from "@ignored/hardhat-vnext/types/artifacts";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are the mocked artifacts mentioned in the PR description that currently do not have the source code that generates them, as discussed during the team meeting sync

this.#artifacts = new Map();
this.#artifactsPaths = new Map();

// An array of elements, where every element has an artifact name and artifact file name, is passed as argument during initialization and stored in the map.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the code I copied from the mocked ArtifactsManager PR. As mentioned in the PR description, I modified the logic slightly to suit my specific test scenarios

@@ -21,6 +21,7 @@
"./hre": "./dist/src/hre.js",
"./plugins": "./dist/src/plugins.js",
"./types/arguments": "./dist/src/types/arguments.js",
"./types/artifacts": "./dist/src/types/artifacts.js",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was a missing export for the artifacts

@@ -0,0 +1,73 @@
import type { HardhatEthersProvider } from "./internal/hardhat-ethers-provider/hardhat-ethers-provider.js";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need a more careful review of the types here, to be sure that they properly align with the V3 artifact implementation

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I double checked this, I think it looks good.

// WARNING: this assumes that the hardhat node is being run in the
// same project which might be wrong
// TODO: enable when V3 is ready: blockGasLimit currently missing in networkConfig
// gasLimit = networkConfig.blockGasLimit;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in the PR description, this is the property (blockGasLimit) that is currently not supported in networkConfig


export async function deepCopy<T = any>(value: T): Promise<T> {
// The function 'deepClone' from 'hardhat-utils' cannot be used to replace this function, it won't properly clone
// the value.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd add an explanation about the "why" to this comment

Copy link
Member

@alcuadrado alcuadrado Oct 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw that dis code comes from v2, so keeping it makes sense. It's just not obvious why

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a comment to better explain why deepClone cannot be used to replace deepCopy: 8348448

// depending on the config, we set a fixed gas limit for all transactions
let gasLimit: bigint | undefined;

if (networkName === "hardhat") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changed. Now this check would be netorkConfig.type !== "http".

I think we should create an issue to address this entire section in a follow up pr.

Copy link
Contributor Author

@ChristopherDedominici ChristopherDedominici Oct 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created an issue to track it and I added the link to it in the issue at the beginning of the PR description

"license": "MIT",
"type": "module",
"exports": {
".": "./dist/src/index.js"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should also have an export for the types

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, good catch. Done here

@ChristopherDedominici ChristopherDedominici added this pull request to the merge queue Oct 3, 2024
Merged via the queue into v-next with commit 929ccb5 Oct 3, 2024
136 checks passed
@ChristopherDedominici ChristopherDedominici deleted the feature/port-hh-ethers branch October 3, 2024 08:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
v-next A Hardhat v3 development task
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Port hardhat-ethers to the v3 API
3 participants