Add getOrCreate to network API#8116
Conversation
In the network API we rename `hre.network.connect()` to `hre.network.create()`. This commit is a mechanical rename using refactoring and search and replace.
🦋 Changeset detectedLatest commit: bb81d75 The changes in this PR will be included in the next version bump. This PR includes changesets to release 15 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 |
There was a problem hiding this comment.
Pull request overview
This PR updates Hardhat’s network API to make connection lifetimes clearer by introducing hre.network.create(...), deprecating (but retaining) hre.network.connect(...), and adding hre.network.getOrCreate(...) for singleton-style cached connections. It also mechanically updates internal code, tests, templates, and package READMEs to prefer create().
Changes:
- Add
NetworkManager.create(...)andNetworkManager.getOrCreate(...)and markconnect(...)as deprecated in types. - Implement
create/getOrCreateinNetworkManagerImplementation, with caching keyed by network name + chain type. - Replace most usages of
connect()withcreate()across tests, templates, examples, and documentation.
Reviewed changes
Copilot reviewed 73 out of 73 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/ignition-core/test-integrations/helpers/create-hre.ts | Update helper to use hre.network.create() |
| packages/hardhat/test/internal/builtin-plugins/node/json-rpc/server.ts | Update test to use hre.network.create() |
| packages/hardhat/test/internal/builtin-plugins/network-manager/request-handlers/e2e.ts | Update e2e tests to use hre.network.create() |
| packages/hardhat/test/internal/builtin-plugins/network-manager/network-manager.ts | Update tests for create() and add coverage for connect/getOrCreate |
| packages/hardhat/test/internal/builtin-plugins/network-manager/edr/edr-provider.ts | Update tests to use hre.network.create() |
| packages/hardhat/templates/hardhat-3/02-mocha-ethers/test/Counter.ts | Template: switch to network.create() |
| packages/hardhat/templates/hardhat-3/02-mocha-ethers/scripts/send-op-tx.ts | Template: switch to network.create() |
| packages/hardhat/templates/hardhat-3/01-node-test-runner-viem/test/Counter.ts | Template: switch to network.create() |
| packages/hardhat/templates/hardhat-3/01-node-test-runner-viem/scripts/send-op-tx.ts | Template: switch to network.create() |
| packages/hardhat/src/types/network.ts | Add create/getOrCreate typings + deprecate connect |
| packages/hardhat/src/internal/builtin-plugins/node/task-action.ts | Switch node task initialization to hre.network.create() |
| packages/hardhat/src/internal/builtin-plugins/network-manager/network-manager.ts | Implement create, connect alias, and getOrCreate cache |
| packages/hardhat/src/internal/builtin-plugins/network-manager/hook-handlers/hre.ts | Expose create/getOrCreate on hre.network and rewire connect |
| packages/hardhat-viem/test/hook-handlers/network.ts | Update to use hre.network.create() |
| packages/hardhat-viem/test/contracts.ts | Update to use hre.network.create() |
| packages/hardhat-viem/test/clients.ts | Update to use hre.network.create() |
| packages/hardhat-viem/README.md | Docs: switch examples to network.create() |
| packages/hardhat-viem-assertions/test/internal/assertions/revert/revert.ts | Update to use hre.network.create() |
| packages/hardhat-viem-assertions/test/internal/assertions/revert/revert-with.ts | Update to use hre.network.create() |
| packages/hardhat-viem-assertions/test/internal/assertions/revert/revert-with-custom-error.ts | Update to use hre.network.create() |
| packages/hardhat-viem-assertions/test/internal/assertions/revert/revert-with-custom-error-with-args.ts | Update to use hre.network.create() |
| packages/hardhat-viem-assertions/test/internal/assertions/emit/emit.ts | Update to use hre.network.create() |
| packages/hardhat-viem-assertions/test/internal/assertions/emit/emit-with-args.ts | Update to use hre.network.create() |
| packages/hardhat-viem-assertions/test/internal/assertions/balances-have-changed.ts | Update to use hre.network.create() |
| packages/hardhat-viem-assertions/README.md | Docs: switch examples to hre.network.create() |
| packages/hardhat-verify/test/verification.ts | Update tests to use hre.network.create() |
| packages/hardhat-verify/test/hook-handlers/network.ts | Update tests to use hre.network.create() |
| packages/hardhat-verify/src/internal/verification.ts | Switch verification logic to network.create() |
| packages/hardhat-verify/src/internal/etherscan.types.ts | Update doc comments to reference network.create() |
| packages/hardhat-verify/README.md | Docs: switch examples to hre.network.create() |
| packages/hardhat-toolbox-viem/test/fixture-projects/toolbox/scripts/script.ts | Fixture: switch to network.create() |
| packages/hardhat-toolbox-mocha-ethers/test/fixture-projects/toolbox/scripts/script.ts | Fixture: switch to network.create() |
| packages/hardhat-network-helpers/test/index.ts | Update tests to use hre.network.create() |
| packages/hardhat-network-helpers/test/helpers/helpers.ts | Update helper to use hre.network.create() |
| packages/hardhat-network-helpers/src/types.ts | Update API docs/examples to use hre.network.create() |
| packages/hardhat-network-helpers/README.md | Docs: switch examples to network.create() |
| packages/hardhat-ledger/test/integration.ts | Update tests to use hre.network.create() (and fix var typo) |
| packages/hardhat-ledger/README.md | Docs: switch examples to hre.network.create() |
| packages/hardhat-ignition/test/test-helpers/use-ignition-project.ts | Update tests/helpers to use hre.network.create() |
| packages/hardhat-ignition/test/test-helpers/create-hre.ts | Update helper to use hre.network.create() |
| packages/hardhat-ignition/src/internal/tasks/verify.ts | Switch ignition task to hre.network.create() |
| packages/hardhat-ignition/src/internal/tasks/transactions.ts | Switch ignition task to hre.network.create() |
| packages/hardhat-ignition/src/internal/tasks/track-tx.ts | Switch ignition task to hre.network.create() |
| packages/hardhat-ignition/src/internal/tasks/deploy.ts | Switch ignition task to hre.network.create() |
| packages/hardhat-ignition-viem/test/test-helpers/create-hre.ts | Update helper to use hre.network.create() |
| packages/hardhat-ignition-viem/test/ignition-helper-exclusivity.ts | Update test to use hre.network.create() |
| packages/hardhat-ignition-viem/README.md | Docs: switch examples to network.create() |
| packages/hardhat-ignition-ethers/test/test-helpers/create-hre.ts | Update helper to use hre.network.create() |
| packages/hardhat-ignition-ethers/test/ignition-helper-exclusivity.ts | Update test to use hre.network.create() |
| packages/hardhat-ignition-ethers/README.md | Docs: switch examples to network.create() |
| packages/hardhat-ethers/test/index.ts | Update tests to use hre.network.create() |
| packages/hardhat-ethers/test/helpers/helpers.ts | Update helpers to use hre.network.create() |
| packages/hardhat-ethers/README.md | Docs: switch examples to network.create() |
| packages/hardhat-ethers-chai-matchers/test/multi-network-connections.ts | Update tests to use hre.network.create() |
| packages/hardhat-ethers-chai-matchers/test/index.ts | Update tests to use hre.network.create() |
| packages/hardhat-ethers-chai-matchers/test/helpers/helpers.ts | Update helpers to use hre.network.create() |
| packages/hardhat-ethers-chai-matchers/README.md | Docs: switch examples to network.create() |
| packages/hardhat-errors/src/descriptors.ts | Fix typo + update guidance to hre.network.create() |
| packages/example-project/test/node/example-test.ts | Example tests: switch to hre.network.create() |
| packages/example-project/test/node/example-test-of-assertions.ts | Example tests: switch to hre.network.create() |
| packages/example-project/test/mocha/mocha-test.ts | Example tests: switch to hre.network.create() |
| packages/example-project/scripts/viem-plugin-example.ts | Example script: switch to hre.network.create() |
| packages/example-project/scripts/send-op-tx.ts | Example script: switch to network.create() |
| packages/example-project/scripts/send-op-tx-viem.ts | Example script: switch to network.create() |
| packages/example-project/scripts/network-helpers.ts | Example script: switch to hre.network.create() |
| packages/example-project/scripts/ledger.ts | Example script: switch to hre.network.create() |
| packages/example-project/scripts/hardhat-ethers.ts | Example script: switch to hre.network.create() |
| packages/example-project/scripts/deploy-rocket-from-script.ts | Example script: switch to hre.network.create() |
| packages/example-project/scripts/demo-trace-output.ts | Example script: switch to hre.network.create() |
| packages/example-project/hardhat.config.ts | Example task: switch to hre.network.create() |
| .peer-bumps.json | Record peer bump reasons due to API usage changes |
| .changeset/purple-forks-write.md | Changeset: deprecate connect() in favor of create() |
| .changeset/fast-bees-smash.md | Changeset: add getOrCreate as a minor change |
9b81e61 to
447969c
Compare
Add the `connect` method back the the network manager API, exactly matching the old connect (that has now been renamed to `create`). Add a deprecation warning to the method. The method is a pass through alias to `create`. A test has been added as a simple regression to check that connect is passing through to create.
This is a caching api providing singleton semantics for the same network name and chain type. The API matches `create` except that overrides are banned (as this would break the caching). The signature types enforce this along with a runtime check.
Most updates are to docs or readmes, requiring a changeset for release but not a peer depenency bump. The two exceptions are Ignition, which uses the new create API during deployment (among other things), and `hardhat-verify` which creates a connection to get a network name.
447969c to
88787e1
Compare
| export interface CachedNetworkConnectionParams< | ||
| ChainTypeT extends ChainType | string = DefaultChainType, | ||
| > extends NetworkConnectionParams<ChainTypeT> { | ||
| override?: never; | ||
| } |
There was a problem hiding this comment.
CachedNetworkConnectionParams extends NetworkConnectionParams and sets override?: never, but without exactOptionalPropertyTypes this still permits override: undefined (and in general doesn’t strongly prevent an override key from being passed), which can lead to a surprising runtime error in getOrCreate when callers accidentally include the key. Consider defining CachedNetworkConnectionParams without inheriting override (e.g. omit the field entirely) so excess-property checks reliably flag override in object literals.
There was a problem hiding this comment.
I am happy either way on this one. The inheritance is to avoid the two types drifting, but if there is a practical advantage to duplicating the type we can take that approach.
| "hardhat": minor | ||
| --- | ||
|
|
||
| Add `getOrCreate` to the network API |
There was a problem hiding this comment.
| Add `getOrCreate` to the network API | |
| Add `create` and `getOrCreate` to the network API |
maybe?
There was a problem hiding this comment.
I split out the create changeset into .changeset/purple-forks-write.md, so it could get a more detailed explanation.
| "reason": "The usage of the new `getUserInterruptionsHandlers` helper from `@nomicfoundation/hardhat-ignition/helpers`" | ||
| }, | ||
| { | ||
| "package": "@nomicfoundation/hardhat-ignition", |
There was a problem hiding this comment.
I think this needs all the packages that have hardhat as a peer, no just the top-level ones.
There was a problem hiding this comment.
I mean, all the packages in this PR
There was a problem hiding this comment.
Most of the packages that are touched here are because of updates to documentation e.g. their README.md had a reference to hre.network.connect.
I am happy to add them as the least risky approach.
There was a problem hiding this comment.
I see. I didn't notice that. I think it still makes sense to play safe and bump it.
There was a problem hiding this comment.
I have added in the extra bumps.
|
@alcuadrado I switched to a deprecation console warning from It will print the warning once at the end, but only if you used |
To improve clarity around the lifetime of connections we are altering the Network API by:
hre.network.connect(...)tohre.network.create(..)for clarityhre.network.connect(...)but marking it as deprecatedhre.network.getOrCreate(...)to support singleton use casesA deprecation warning will be shown once per hre instance via Node's
process.emitWarningon usage ofconnect.New API
Deprecation warning
Calls to
connectare recorded and a deprecation warning printed at the end:Note
The warning is only shown if the default
hreinstance is used.Review
Note
Review a commit at a time
This PR affects many files as we have switched the default usage in docs and examples from
hre.network.connect()tohre.network.create(). This change is mainly a mechanical refactor, and is captured in the first commit.Docs PR
createandcreateOrGethardhat-website#249TODO
createOrGet, can it be brought into line withcreateconnectused