Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fast-bees-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"hardhat": minor
---

Add `getOrCreate` to the network API
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
Add `getOrCreate` to the network API
Add `create` and `getOrCreate` to the network API

maybe?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I split out the create changeset into .changeset/purple-forks-write.md, so it could get a more detailed explanation.

19 changes: 19 additions & 0 deletions .changeset/purple-forks-write.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
"@nomicfoundation/hardhat-ethers-chai-matchers": patch
"@nomicfoundation/hardhat-toolbox-mocha-ethers": patch
"@nomicfoundation/hardhat-ignition-ethers": patch
"@nomicfoundation/hardhat-network-helpers": patch
"@nomicfoundation/hardhat-viem-assertions": patch
"@nomicfoundation/hardhat-ignition-viem": patch
"@nomicfoundation/hardhat-toolbox-viem": patch
"@nomicfoundation/hardhat-ignition": patch
"@nomicfoundation/hardhat-errors": patch
"@nomicfoundation/hardhat-ethers": patch
"@nomicfoundation/hardhat-ledger": patch
"@nomicfoundation/hardhat-verify": patch
"@nomicfoundation/ignition-core": patch
"@nomicfoundation/hardhat-viem": patch
"hardhat": patch
---

Deprecate the `hre.network.connect()` method in favour of `hre.network.create()`, exactly the same method but more clearly indicating that it will create a new connection.
60 changes: 60 additions & 0 deletions .peer-bumps.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,66 @@
"package": "@nomicfoundation/hardhat-ignition-ethers",
"peer": "@nomicfoundation/hardhat-ignition",
"reason": "The usage of the new `getUserInterruptionsHandlers` helper from `@nomicfoundation/hardhat-ignition/helpers`"
},
{
"package": "@nomicfoundation/hardhat-ignition",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this needs all the packages that have hardhat as a peer, no just the top-level ones.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I mean, all the packages in this PR

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I see. I didn't notice that. I think it still makes sense to play safe and bump it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I have added in the extra bumps.

"peer": "hardhat",
"reason": "Switched from deprecated `hre.network.connect()` to new `hre.network.create()` in deploy, track-tx, transactions, and verify tasks"
},
{
"package": "@nomicfoundation/hardhat-verify",
"peer": "hardhat",
"reason": "Switched from deprecated `network.connect()` to new `network.create()` in verification logic and updated API docs"
},
{
"package": "@nomicfoundation/hardhat-ethers-chai-matchers",
"peer": "hardhat",
"reason": "Conservative bump for `connect()` deprecation and `create()`/`getOrCreate()` network API changes"
},
{
"package": "@nomicfoundation/hardhat-toolbox-mocha-ethers",
"peer": "hardhat",
"reason": "Conservative bump for `connect()` deprecation and `create()`/`getOrCreate()` network API changes"
},
{
"package": "@nomicfoundation/hardhat-ignition-ethers",
"peer": "hardhat",
"reason": "Conservative bump for `connect()` deprecation and `create()`/`getOrCreate()` network API changes"
},
{
"package": "@nomicfoundation/hardhat-network-helpers",
"peer": "hardhat",
"reason": "Conservative bump for `connect()` deprecation and `create()`/`getOrCreate()` network API changes"
},
{
"package": "@nomicfoundation/hardhat-viem-assertions",
"peer": "hardhat",
"reason": "Conservative bump for `connect()` deprecation and `create()`/`getOrCreate()` network API changes"
},
{
"package": "@nomicfoundation/hardhat-ignition-viem",
"peer": "hardhat",
"reason": "Conservative bump for `connect()` deprecation and `create()`/`getOrCreate()` network API changes"
},
{
"package": "@nomicfoundation/hardhat-toolbox-viem",
"peer": "hardhat",
"reason": "Conservative bump for `connect()` deprecation and `create()`/`getOrCreate()` network API changes"
},
{
"package": "@nomicfoundation/hardhat-ethers",
"peer": "hardhat",
"reason": "Conservative bump for `connect()` deprecation and `create()`/`getOrCreate()` network API changes"
},
{
"package": "@nomicfoundation/hardhat-ledger",
"peer": "hardhat",
"reason": "Conservative bump for `connect()` deprecation and `create()`/`getOrCreate()` network API changes"
},
{
"package": "@nomicfoundation/hardhat-viem",
"peer": "hardhat",
"reason": "Conservative bump for `connect()` deprecation and `create()`/`getOrCreate()` network API changes"
}
]
}
2 changes: 1 addition & 1 deletion packages/example-project/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ const printConfig = task("config", "Print the config")
const printAccounts = task("accounts", "Print the accounts")
.setAction(async () => ({
default: async ({}, hre) => {
const { provider } = await hre.network.connect();
const { provider } = await hre.network.create();
console.log(await provider.request({ method: "eth_accounts" }));
},
}))
Expand Down
4 changes: 2 additions & 2 deletions packages/example-project/scripts/demo-trace-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import hre from "hardhat";
// ═══════════════════════════════════════════════════════════════════════
// Setup: two connections to two independent EDR-simulated networks
// ═══════════════════════════════════════════════════════════════════════
const connA = await hre.network.connect("default");
const connB = await hre.network.connect("node");
const connA = await hre.network.create("default");
const connB = await hre.network.create("node");

const [counterA, counterB, revertContract] = await Promise.all([
connA.viem.deployContract("Counter", []),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import hre from "hardhat";

import apolloModule from "../ignition/modules/Apollo.js";

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

const { apollo } = await ignition.deploy(apolloModule);

Expand Down
2 changes: 1 addition & 1 deletion packages/example-project/scripts/hardhat-ethers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import hre from "hardhat";

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

// ethers functionalities
ethers.isAddress("0x1234567890123456789012345678901234567890");
Expand Down
2 changes: 1 addition & 1 deletion packages/example-project/scripts/ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import assert from "node:assert";

const BALANCE_TO_SEND = 1000000000000000000n;

const { provider, ethers } = await hre.network.connect("edrOp");
const { provider, ethers } = await hre.network.create("edrOp");

const signers = await ethers.getSigners();

Expand Down
2 changes: 1 addition & 1 deletion packages/example-project/scripts/network-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

import hre from "hardhat";

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

await networkHelpers.mine();
2 changes: 1 addition & 1 deletion packages/example-project/scripts/send-op-tx-viem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { network } from "hardhat";
async function sendL2Transaction(networkConfigName: string) {
console.log("Sending transaction using network", networkConfigName);

const { viem, networkConfig } = await network.connect({
const { viem, networkConfig } = await network.create({
network: networkConfigName,
chainType: "op",
});
Expand Down
2 changes: 1 addition & 1 deletion packages/example-project/scripts/send-op-tx.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { network } from "hardhat";

const { provider } = await network.connect({
const { provider } = await network.create({
network: "op",
chainType: "op",
});
Expand Down
8 changes: 4 additions & 4 deletions packages/example-project/scripts/viem-plugin-example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { parseEther } from "viem";

async function testL2Extensions() {
// This network connection has access to an optimism-specific viem api
const optimism = await hre.network.connect({
const optimism = await hre.network.create({
network: "localhost",
chainType: "op",
});
Expand All @@ -12,7 +12,7 @@ async function testL2Extensions() {
console.log("L1 base fee:", l1BaseFee);

// This one doesn't
const mainnet = await hre.network.connect({
const mainnet = await hre.network.create({
network: "localhost",
chainType: "l1",
});
Expand All @@ -26,7 +26,7 @@ async function testL2Extensions() {
}

async function testClients() {
const networkConnection = await hre.network.connect();
const networkConnection = await hre.network.create();

const publicClient = await networkConnection.viem.getPublicClient();
const [fromWalletClient, toWalletClient] =
Expand Down Expand Up @@ -72,7 +72,7 @@ async function testClients() {
}

async function testContracts() {
const networkConnection = await hre.network.connect();
const networkConnection = await hre.network.create();
const counter = await networkConnection.viem.deployContract("Counter");

console.log("Deployed contract at address", counter.address);
Expand Down
8 changes: 4 additions & 4 deletions packages/example-project/test/mocha/mocha-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe("Mocha test", () => {

describe("Mocha test with chai-matchers", () => {
before(async () => {
await hre.network.connect();
await hre.network.create();
});

it("should import variables from the chai-matchers package", () => {
Expand All @@ -30,7 +30,7 @@ describe("Mocha test with chai-matchers", () => {

describe("Rocket test", () => {
it("should launch the Apollo 11 rocket", async () => {
const connection = await hre.network.connect();
const connection = await hre.network.create();

const Rocket = await connection.ethers.getContractFactory("Rocket");
const rocket = await Rocket.deploy("Apollo 11");
Expand All @@ -43,7 +43,7 @@ describe("Rocket test", () => {

describe("Matchers without automining", () => {
it("emit should wait for the tx to be mined", async () => {
const { ethers, provider } = await hre.network.connect();
const { ethers, provider } = await hre.network.create();

const Rocket = await ethers.getContractFactory("Rocket");
const rocket = await Rocket.deploy("Apollo 11");
Expand All @@ -62,7 +62,7 @@ describe("Matchers without automining", () => {
});

it("revert should wait for the tx to be mined", async () => {
const { ethers, provider } = await hre.network.connect();
const { ethers, provider } = await hre.network.create();

const FailingContract = await ethers.getContractFactory("FailingContract");
const failing = await FailingContract.deploy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { describe, it, before } from "node:test";
import hre from "hardhat";
import { ContractReturnType } from "@nomicfoundation/hardhat-viem/types";

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

describe("Example EDR based test", () => {
describe("revert", () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/example-project/test/node/example-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import hre from "hardhat";

describe("Example EDR based test", () => {
it("should work get the block number from the EDR Network", async () => {
const connection = await hre.network.connect();
const connection = await hre.network.create();

const blockNumberAtStart = await connection.provider.request({
method: "eth_blockNumber",
Expand All @@ -24,7 +24,7 @@ describe("Example EDR based test", () => {
});

it("should show stack traces when a transaction reverts", async () => {
const connection = await hre.network.connect();
const connection = await hre.network.create();

const Revert = await connection.ethers.getContractFactory("Revert");
const revert = await Revert.deploy();
Expand Down
4 changes: 2 additions & 2 deletions packages/hardhat-errors/src/descriptors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1067,7 +1067,7 @@ Try using another mnemonic or deriving less keys.`,
messageTemplate: "The provider has been closed.",
websiteTitle: "Provider closed",
websiteDescription:
"The provider your are trying to use has been closed. Please create a new one using hre.network.connect() and try again.",
"The provider you are trying to use has been closed. Please create a new one using hre.network.create() and try again.",
},
INCOMPATIBLE_EIP7702_FIELDS: {
number: 723,
Expand Down Expand Up @@ -2232,7 +2232,7 @@ Please ensure you're using one of the supported networks.`,
To resolve this, make sure to add an account to the specified network in the Hardhat config. Alternatively, you can set a custom wallet client by passing it as a parameter in the relevant function:

\`\`\`ts
const networkConnection = await hre.network.connect(...);
const networkConnection = await hre.network.create(...);
const walletClient = await networkConnection.viem.getWalletClient(address);

await networkConnection.viem.deployContract(contractName, constructorArgs, { walletClient });
Expand Down
2 changes: 1 addition & 1 deletion packages/hardhat-ethers-chai-matchers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Here is an example of using the `emit` matcher:
import { expect } from "chai";
import { network } from "hardhat";

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

it("some test", async function () {
const contract = await ethers.deployContract("SomeContract");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export async function initEnvironment(_artifactsPath: string): Promise<{

await hre.tasks.getTask("build").run({ force: false });

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

return { provider, ethers };
}
Expand Down
2 changes: 1 addition & 1 deletion packages/hardhat-ethers-chai-matchers/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe("hardhat-ethers-chai-matchers plugin correctly initialized", () => {

const hre = await createHardhatRuntimeEnvironment(hardhatConfig.default);

await hre.network.connect();
await hre.network.create();

const result = await hre.tasks.getTask(["test", "mocha"]).run({
testFiles: ["./test/test.ts"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ describe("handle multiple connections", () => {
},
});

({ ethers } = await hre.network.connect({
({ ethers } = await hre.network.create({
network: "test1",
}));

({ ethers: ethers2 } = await hre.network.connect({
({ ethers: ethers2 } = await hre.network.create({
network: "test2",
}));
});
Expand Down
8 changes: 4 additions & 4 deletions packages/hardhat-ethers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ This plugin adds an `ethers` property to each network connection:
```ts
import { network } from "hardhat";

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

const counter = await ethers.deployContract("Counter");
await counter.inc();
Expand All @@ -41,7 +41,7 @@ This object has the same API as [ethers.js](https://docs.ethers.org/), with some

### Provider

The plugin adds a `provider` property to the `ethers` object: an [ethers.js provider](https://docs.ethers.org/v6/single-page/#api_providers__Provider) connected to the network selected by `network.connect()`.
The plugin adds a `provider` property to the `ethers` object: an [ethers.js provider](https://docs.ethers.org/v6/single-page/#api_providers__Provider) connected to the network selected by `network.create()`.

```ts
const blockNumber = await ethers.provider.getBlockNumber();
Expand Down Expand Up @@ -84,10 +84,10 @@ An [ethers.js provider](https://docs.ethers.org/v6/single-page/#api_providers__P

```ts
// the network selected with --network option if specified, or the default network otherwise
const { ethers } = await network.connect();
const { ethers } = await network.create();

// a specific network from the config
const { ethers } = await network.connect("mainnet");
const { ethers } = await network.create("mainnet");
```

### `deployContract`
Expand Down
2 changes: 1 addition & 1 deletion packages/hardhat-ethers/test/helpers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export async function initializeTestEthers(
const network =
config.networks?.localhost !== undefined ? "localhost" : "default";

const connection = await hre.network.connect(network);
const connection = await hre.network.create(network);

const provider = connection.provider;
const networkName = connection.networkName;
Expand Down
2 changes: 1 addition & 1 deletion packages/hardhat-ethers/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe("hardhat-ethers plugin initialization", () => {
plugins: [hardhatEthersPlugin],
});

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

it("should correctly set up ethers functionalities", async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/hardhat-ignition-ethers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ This plugin adds an `ignition` property to each network connection:
import { network } from "hardhat";
import Counter from "../ignition/modules/Counter.js";

const { ignition } = await network.connect();
const { ignition } = await network.create();
const { counter } = await ignition.deploy(Counter);

await counter.inc();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe("ignition helper mutual exclusivity", () => {
plugins: [fakeHardhatIgnitionViemPlugin, hardhatIgnitionEthersPlugin],
});

return hre.network.connect();
return hre.network.create();
},
HardhatError.ERRORS.IGNITION.INTERNAL
.ONLY_ONE_IGNITION_EXTENSION_PLUGIN_ALLOWED,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import hardhatIgnitionEthers from "../../src/index.js";

export async function createConnection(): Promise<NetworkConnection> {
const hre = await createHre();
const connection = await hre.network.connect();
const connection = await hre.network.create();

await connection.provider.request({
method: "evm_setAutomine",
Expand Down
Loading
Loading