Define the HookContext
as a partial view of the HRE
#5776
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR addresses a limitation in Hardhat's core architecture discovered by @ChristopherDedominici.
Explanation of the problem
Chris is working on the first few hook handlers that use the builtin plugins, and in particular, the extensions to the
HRE
that they make.This was problematic, as
HookHandler
s don't receive theHRE
, but aHookContext
instead. The problem was that any extension to theHRE
wasn't reflected in theHookContext
.The' HookContext' intends to prevent
HookHandler
's from accessing theTaskManager
and running tasks, as these are separate concerns in v3, and not to prevent them from accessing the builtin plugins.Proposed solution
This solution has a type-level and runtime-level component.
Types
Instead of defining the
HookContext
as a separate interface, define it as a derived type of theHRE
:This way, every extension to the
HardhatRuntimeEnvironment
is also available in theHookContext
.Runtime
To make the values (as in runtime values, not types) of the extensions available through the
HookContext
, without creating a new hook nor restructuring the system, I usedObject.create
to create theHookContext
, and overwrotetasks
withundefined
.Contrary to making a shallow copy at initialization, defining the HRE as its prototype means that any future modification to the HRE will also be visible.