-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Bind all network helpers #7174
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
Bind all network helpers #7174
Conversation
🦋 Changeset detectedLatest commit: 76ab773 The changes in this PR will be included in the next version bump. This PR includes changesets to release 21 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Luis Schaab <schaable@gmail.com>
Co-authored-by: Luis Schaab <schaable@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR introduces a bindAllMethods utility function to solve a limitation where network helper methods would fail when destructured or assigned to independent variables. The fix ensures that methods from NetworkHelpers and Time classes maintain their this context when called independently.
- Adds a new
bindAllMethodsutility function that binds all methods of an object to preservethiscontext - Updates NetworkHelpers and Time classes to use the binding utility in their constructors
- Adds comprehensive test coverage for the new utility function and verifies the fix works for destructured methods
Reviewed Changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| v-next/hardhat-utils/src/lang.ts | Implements the bindAllMethods utility function with proper method filtering |
| v-next/hardhat-utils/test/lang.ts | Adds comprehensive test coverage for the new utility function |
| v-next/hardhat-network-helpers/src/internal/network-helpers/network-helpers.ts | Applies method binding to NetworkHelpers class |
| v-next/hardhat-network-helpers/src/internal/network-helpers/time/time.ts | Applies method binding to Time class |
| v-next/hardhat-network-helpers/test/index.ts | Adds integration test verifying destructured methods work correctly |
| .changeset/wild-rivers-float.md | Documents the addition of bindAllMethods utility |
| .changeset/gentle-planets-travel.md | Documents the network helpers enhancement |
v-next/hardhat-utils/src/lang.ts
Outdated
|
|
||
| for (const key of keys) { | ||
| const val = objAsAny[key]; | ||
| if ( |
Copilot
AI
Aug 11, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The condition should also check if the property is writable to avoid attempting to bind read-only methods. Consider adding Object.getOwnPropertyDescriptor(obj, key)?.writable !== false to the condition.
v-next/hardhat-utils/src/lang.ts
Outdated
| const keys = [ | ||
| ...Object.getOwnPropertyNames(Object.getPrototypeOf(obj)), |
Copilot
AI
Aug 11, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code will fail with a TypeError if obj is null or has a null prototype (like objects created with Object.create(null)). Consider adding a null check: Object.getPrototypeOf(obj) && Object.getOwnPropertyNames(Object.getPrototypeOf(obj)).
| const keys = [ | |
| ...Object.getOwnPropertyNames(Object.getPrototypeOf(obj)), | |
| const proto = Object.getPrototypeOf(obj); | |
| const keys = [ | |
| ...(proto ? Object.getOwnPropertyNames(proto) : []), |
002851b to
76ab773
Compare
Bind all network helpers
Previously this would fail