Skip to content

Commit 6dbd7f8

Browse files
committed
Replace connect() with create() and getOrCreate()
1 parent dd1e605 commit 6dbd7f8

31 files changed

Lines changed: 127 additions & 100 deletions

STYLE.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,12 @@ Examples:
9595

9696
Too cold/direct:
9797

98-
- First, you'll add the `myAccount` property to the `NetworkConnection` object returned by `network.connect()`.
98+
- First, you'll add the `myAccount` property to the `NetworkConnection` object returned by `network.create()`.
9999

100100
Warm and conversational:
101101

102-
- Let's start by adding a `myAccount` property to the `NetworkConnection` object returned by `network.connect()`.
103-
- The first step is adding the `myAccount` property to the `NetworkConnection` object returned by `network.connect()`.
102+
- Let's start by adding a `myAccount` property to the `NetworkConnection` object returned by `network.create()`.
103+
- The first step is adding the `myAccount` property to the `NetworkConnection` object returned by `network.create()`.
104104

105105
Note that this is really important, and key to our branding. We don't want our documentation to be perceived too cold/direct just to be slightly more concise.
106106

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"@astrojs/vercel": "^8.2.8",
2626
"@expressive-code/plugin-collapsible-sections": "^0.41.3",
2727
"@expressive-code/plugin-line-numbers": "^0.41.3",
28-
"@nomicfoundation/hardhat-errors": "^3.0.8",
28+
"@nomicfoundation/hardhat-errors": "^3.0.10",
2929
"@types/tar-stream": "^3.1.4",
3030
"astro": "^5.6.1",
3131
"cspell": "^9.2.2",

pnpm-lock.yaml

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/content/docs/docs/explanations/edr-simulated-networks.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Unlike connecting to a remote node through JSON-RPC, simulated networks give you
2020

2121
The only difference between a blockchain simulated by EDR and a production one is that EDR requires no consensus mechanism or peer-to-peer network, as it's run by a single process each time.
2222

23-
When you call `await network.connect()` with a Network Config of type `edr-simulated`, Hardhat creates a new, independent blockchain simulation. Each simulation is isolated, so you can create multiple simulations simultaneously without them interfering with each other.
23+
When you call `await network.create()` with a Network Config of type `edr-simulated`, Hardhat creates a new, independent blockchain simulation. Each simulation is isolated, so you can create multiple simulations simultaneously without them interfering with each other.
2424

2525
## How to use simulated networks
2626

@@ -33,7 +33,7 @@ When you run your tests or scripts, Hardhat automatically creates an in-process
3333
```ts
3434
import { network } from "hardhat";
3535

36-
const connection = await network.connect();
36+
const connection = await network.create();
3737
```
3838

3939
This creates a new blockchain simulation based on the Network Config specified in your configuration file or via the `--network` flag.

src/content/docs/docs/explanations/global-options.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Global Options are options that Hardhat can receive and are available across all
1111

1212
They are different from Hardhat Task options and arguments in that they are meant to control the functionality of Hardhat itself, and not that of a single task.
1313

14-
For example, `--network` is a global option that controls the default value that `network.connect()` uses when no network config name is provided.
14+
For example, `--network` is a global option that controls the default value that `network.create()` uses when no network config name is provided.
1515

1616
Plugins can define their own Global Options, so the complete list depends on your setup. To see all available options, run:
1717

src/content/docs/docs/explanations/multichain-support.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ When you connect to a network using the [Network Manager](/docs/reference/networ
5353
// scripts/example-op.ts
5454
import { network } from "hardhat";
5555

56-
const { viem } = await network.connect({
56+
const { viem } = await network.create({
5757
network: "hardhatOp",
5858
chainType: "op",
5959
});
@@ -68,7 +68,7 @@ const l1Gas = await publicClient.estimateL1Gas({
6868

6969
The network simulation will be created using the `op` Chain Type and its simulation will be stricter.
7070

71-
Moreover, the Chain Type information will be available to network-related plugins, both at runtime and at type-level. This allows them to modify their behavior and the result of `network.connect()` depending on the Chain Type, potentially adding more functionality.
71+
Moreover, the Chain Type information will be available to network-related plugins, both at runtime and at type-level. This allows them to modify their behavior and the result of `network.create()` depending on the Chain Type, potentially adding more functionality.
7272

7373
For example, the method highlighted in the snippet above is only available when you're using the `op` Chain Type.
7474

src/content/docs/docs/explanations/network-management.mdx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,14 @@ This object allows you to create network connections. You can do it like this:
6969
```ts
7070
import { network } from "hardhat";
7171

72-
const networkConnection = await network.connect();
72+
const networkConnection = await network.create();
7373
```
7474

7575
By default, it creates a `NetworkConnection` using the Network Config called `default`, which is always present. You can choose a different fallback Network Config name by using the `network` [Global Option](/docs/explanations/global-options), like this:
7676

7777
<Run command="hardhat --network mainnet" />
7878

79-
Every time you call `network.connect()` a new independent `NetworkConnection` object is created:
79+
Every time you call `network.create()` a new independent `NetworkConnection` object is created:
8080

8181
- If the Network Config is of type `"http"`, the JSON-RPC server in the `url` setting will be used. No synchronization between two HTTP Network Connections is performed by Hardhat, either at the HTTP layer or at the protocol layer (e.g. when handling account nonces).
8282

@@ -86,19 +86,21 @@ Every time you call `network.connect()` a new independent `NetworkConnection` ob
8686
This document makes a distinction between blockchain and Network Config, their names, Network Connection, and creating a Network Connection, to be precise in its explanations.
8787

8888
In practice, most people will simplify this vocabulary and just say that, for example, running the command above connected to the network mainnet. Using "connect" interchangeably with creating a Network Connection, and the network name with the blockchain they intend to connect to.
89+
90+
If you want to reuse the same connection across different parts of your code, you can use `network.getOrCreate()` instead. It returns an existing connection if one was previously created with the same network name and chain type.
8991
:::
9092

9193
### Using an explicit Network Config
9294

93-
You can also create a connection using an explicit Network Config, and not just relying on `--network`. This is done by providing parameters to `network.connect()`:
95+
You can also create a connection using an explicit Network Config, and not just relying on `--network`. This is done by providing parameters to `network.create()`:
9496

9597
```ts
96-
const mainnetConnection = await network.connect("mainnet");
98+
const mainnetConnection = await network.create("mainnet");
9799

98-
const simulatedConnection = await network.connect("simulatedNetwork");
100+
const simulatedConnection = await network.create("simulatedNetwork");
99101

100102
// Creating a second simulated blockchain
101-
const anotherSimulated = await network.connect("simulatedNetwork");
103+
const anotherSimulated = await network.create("simulatedNetwork");
102104
```
103105

104106
### Cleaning up your Network Connections
@@ -111,7 +113,7 @@ await networkConnection.close();
111113

112114
## The `NetworkConnection` object
113115

114-
The result of calling `await network.connect()` is a `NetworkConnection` object.
116+
The result of calling `await network.create()` is a `NetworkConnection` object.
115117

116118
By default, they have the basics to interact with a blockchain:
117119

@@ -135,7 +137,7 @@ For example, if you use the [`@nomicfoundation/hardhat-toolbox-viem`](/docs/plug
135137
The idiomatic way to use the Network Connection fields is by creating them like this:
136138

137139
```ts
138-
const { viem, networkHelpers, ignition } = await network.connect();
140+
const { viem, networkHelpers, ignition } = await network.create();
139141
```
140142

141143
:::

src/content/docs/docs/guides/deployment/using-scripts.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ To build a deployment script using viem, create the `scripts/deploy-counter.ts`
2222
// scripts/deploy-counter.ts
2323
import { network } from "hardhat";
2424

25-
const { viem, networkName } = await network.connect();
25+
const { viem, networkName } = await network.create();
2626
const client = await viem.getPublicClient();
2727

2828
console.log(`Deploying Counter to ${networkName}...`);
@@ -48,7 +48,7 @@ To build a deployment script using ethers, create the `scripts/deploy-counter.ts
4848
// scripts/deploy-counter.ts
4949
import { network } from "hardhat";
5050

51-
const { ethers, networkName } = await network.connect();
51+
const { ethers, networkName } = await network.create();
5252

5353
console.log(`Deploying Counter to ${networkName}...`);
5454

src/content/docs/docs/guides/forking.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ import { network } from "hardhat";
5555

5656
describe("Example", function () {
5757
it("should use the forked network", async function () {
58-
const { viem } = await network.connect("mainnetFork");
58+
const { viem } = await network.create("mainnetFork");
5959

6060
// This test uses the forked Mainnet network
6161
});
6262

6363
it("should use the default network", async function () {
64-
const { viem } = await network.connect();
64+
const { viem } = await network.create();
6565

6666
// This test uses the default local network
6767
});

src/content/docs/docs/guides/hardhat-console.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ The `console` task starts a [Node.js REPL](https://nodejs.org/en/learn/command-l
2222
Here's an example where you deploy a contract and interact with it:
2323

2424
```
25-
> const { viem } = await network.connect()
25+
> const { viem } = await network.create()
2626
> const counter = await viem.deployContract("Counter")
2727
> await counter.write.inc()
2828
> await counter.read.x()
@@ -37,7 +37,7 @@ To skip compilation before starting the console, pass the `--no-compile` flag:
3737

3838
The `console` task accepts an optional list of positional arguments. Each argument you provide will be executed as a command immediately after the REPL starts, letting you run a series of commands automatically:
3939

40-
<Run command='hardhat console "const { viem } = await network.connect();" />' />
40+
<Run command='hardhat console "const { viem } = await network.create();" />' />
4141

4242
Make sure to wrap each command in quotes to avoid issues with your shell interpreting special characters.
4343

@@ -66,7 +66,7 @@ export default defineConfig({
6666
// tasks/my-console.ts
6767
export default function myConsoleTask(_, hre) {
6868
return hre.tasks.getTask("console").run({
69-
commands: ["const { viem } = await network.connect();"],
69+
commands: ["const { viem } = await network.create();"],
7070
});
7171
}
7272
```

0 commit comments

Comments
 (0)