You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: docs/src/content/hardhat-network/docs/guides/forking-other-networks.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -73,7 +73,7 @@ Hardhat Network will by default fork from a recent mainnet block. While this mig
73
73
74
74
There are two reasons for this:
75
75
76
-
- The state your tests run against may change between runs. This could cause your tests or scripts to behave differently.
76
+
- The state your tests run against may change between runs. This could cause your tests to behave differently.
77
77
- Pinning enables caching. Every time data is fetched from mainnet, Hardhat Network caches it on disk to speed up future access. If you don't pin the block, there's going to be new data with each new block and the cache won't be useful. We measured up to 20x speed improvements with block pinning.
78
78
79
79
**You will need access to a node with archival data for this to work.** This is why we recommend [Infura] or [Alchemy], since their free plans include archival data.
Copy file name to clipboardexpand all lines: docs/src/content/hardhat-network/docs/overview/index.md
+2-2
Original file line number
Diff line number
Diff line change
@@ -25,11 +25,11 @@ When Hardhat executes your tests, scripts or tasks, an in-process Hardhat Networ
25
25
26
26
There's no need to make any changes to your tests or scripts.
27
27
28
-
Hardhat Network is simply another network. If you wanted to be explicit, you could run, for example, `npx hardhat run --network hardhat scripts/my-script.js`.
28
+
Hardhat Network is simply another network. If you wanted to be explicit, you could run, for example, `npx hardhat ignition deploy ./ignition/modules/MyModule.ts --network hardhat`.
29
29
30
30
### Running stand-alone in order to support wallets and other software
31
31
32
-
Alternatively, Hardhat Network can run in a stand-alone fashion so that external clients can connect to it. This could be a wallet, your Dapp front-end, or a script. To run Hardhat Network in this way, run:
32
+
Alternatively, Hardhat Network can run in a stand-alone fashion so that external clients can connect to it. This could be a wallet, your Dapp front-end, or a Hardhat Ignition deployment. To run Hardhat Network in this way, run:
Copy file name to clipboardexpand all lines: docs/src/content/hardhat-runner/docs/advanced/building-plugins.md
+2-2
Original file line number
Diff line number
Diff line change
@@ -12,7 +12,7 @@ Some examples of things you could achieve by creating a plugin are: running a li
12
12
13
13
## Extending the Hardhat Runtime Environment
14
14
15
-
Let’s go through the process of creating a plugin that adds new functionality to the Hardhat Runtime Environment. By doing this, we make sure our new feature is available everywhere. This means your plugin users can access it from tasks, tests, scripts, and the Hardhat console.
15
+
Let’s go through the process of creating a plugin that adds new functionality to the Hardhat Runtime Environment. By doing this, we make sure our new feature is available everywhere. This means your plugin users can access it from tasks, tests, scripts and the Hardhat console.
16
16
17
17
The Hardhat Runtime Environment (HRE) is configured through a queue of extension functions that you can add to using the `extendEnvironment()` function. It receives one parameter which is a callback which will be executed after the HRE is initialized. If `extendEnvironment` is called multiple times, its callbacks will be executed in order.
18
18
@@ -129,7 +129,7 @@ An example on how to add fields to the Hardhat config can be found in [`src/inde
129
129
130
130
To show better stack traces to your users when an error is meant to interrupt a task's execution, please consider throwing `HardhatPluginError` errors, which can be found in `hardhat/plugins`.
131
131
132
-
If your error originated in your user's code, like a test or script calling one of your functions, you shouldn't use `HardhatPluginError`.
132
+
If your error originated in your user's code, like a test calling one of your functions, you shouldn't use `HardhatPluginError`.
133
133
134
134
### Optimizing your plugin for better startup time
Copy file name to clipboardexpand all lines: docs/src/content/hardhat-runner/docs/advanced/scripts.md
+18-33
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,8 @@
1
1
# Writing scripts with Hardhat
2
2
3
-
<!-- TODO: We should write this guide based on a different common use case for scripts now that Ignition exists -->
4
-
5
3
In this guide we will go through the steps of creating a script with Hardhat. For a general overview of using Hardhat refer to the [Getting started guide](../getting-started/index.md).
6
4
7
-
You can write your own custom scripts that can use all of Hardhat's functionality. A classic use case is writing a deployment script for your smart contracts.
5
+
You can write your own custom scripts that can use all of Hardhat's functionality. A classic use case is writing a script that prints the list of available accounts.
8
6
9
7
There are two ways of writing a script that accesses the [Hardhat Runtime Environment](../advanced/hardhat-runtime-environment.md).
10
8
@@ -26,45 +24,32 @@ const hre = require("hardhat");
26
24
27
25
You can get access to all your tasks and plugins. To run these scripts you simply go through node: `node script.js`.
28
26
29
-
To try this out, let's look at [a fresh Hardhat project](../guides/project-setup.md). Run `npx hardhat init` and go through the steps to create a JavaScript project. When you're done, your project directory should look like this:
To try this out, create a new directory called `scripts` in your project's root directory. Then, inside that directory, create a file called `accounts.js` with the following content:
42
28
43
-
Inside `scripts/` you will find `deploy.js`. Read through its comments to have a better idea of what it does.
29
+
```js
30
+
consthre=require("hardhat");
44
31
45
-
<!-- FIX: What needs to be swapped in for this? -->
Lock with 1 ETH deployed to: 0x5FbDB2315678afecb367f032d93F642f64180aa3
40
+
main().catch((error) => {
41
+
console.error(error);
42
+
process.exitCode=1;
43
+
});
53
44
```
54
45
55
-
By accessing the [Hardhat Runtime Environment](../advanced/hardhat-runtime-environment.md) at the top, you are allowed to run the script in a standalone fashion.
56
-
57
-
Hardhat always runs the compile task when it's invoked via `npx hardhat run`, but in a standalone fashion you may want to call compile manually to make sure everything is compiled. This can be done by calling `hre.run("compile")`. Add the following line at the beginning of the `main` function and re-run the script with node:
46
+
Now run the script:
58
47
59
-
```js
60
-
awaithre.run("compile");
48
+
```sh
49
+
node scripts/accounts.js
61
50
```
62
51
63
-
```
64
-
$ node scripts/deploy.js
65
-
Nothing to compile
66
-
Lock with 1 ETH deployed to: 0x5FbDB2315678afecb367f032d93F642f64180aa3
67
-
```
52
+
By accessing the [Hardhat Runtime Environment](../advanced/hardhat-runtime-environment.md) at the top, you are allowed to run the script in a standalone fashion.
Copy file name to clipboardexpand all lines: docs/src/content/hardhat-runner/docs/getting-started/index.md
+2-2
Original file line number
Diff line number
Diff line change
@@ -65,7 +65,7 @@ If you are using Windows, we **strongly recommend** using [WSL 2](https://docs.m
65
65
66
66
:::
67
67
68
-
We will explore the basics of creating a Hardhat project with a sample contract, tests of that contract, and a script to deploy it.
68
+
We will explore the basics of creating a Hardhat project with a sample contract, tests of that contract, and a Hardhat Ignition module to deploy it.
69
69
70
70
To create the sample project, run `npx hardhat init` in your project folder:
71
71
@@ -321,7 +321,7 @@ To learn more check out the [Hardhat Ignition documentation](/ignition).
321
321
322
322
### Connecting a wallet or Dapp to Hardhat Network
323
323
324
-
By default, Hardhat will spin up a new in-memory instance of Hardhat Network on startup. It's also possible to run Hardhat Network in a standalone fashion so that external clients can connect to it. This could be a wallet, your Dapp front-end, or a script.
324
+
By default, Hardhat will spin up a new in-memory instance of Hardhat Network on startup. It's also possible to run Hardhat Network in a standalone fashion so that external clients can connect to it. This could be a wallet, your Dapp front-end, or a Hardhat Ignition deployment.
325
325
326
326
To run Hardhat Network in this way, run `npx hardhat node`:
Copy file name to clipboardexpand all lines: docs/src/content/hardhat-runner/docs/guides/hardhat-console.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -11,7 +11,7 @@ Type ".help" for more information.
11
11
12
12
The `compile` task will be called before opening the console prompt, but you can skip this with the `--no-compile` parameter.
13
13
14
-
The execution environment for the console is the same as for tasks, scripts and tests. This means the configuration has been processed, and the [Hardhat Runtime Environment] has been initialized and injected into the global scope.
14
+
The execution environment for the console is the same as for tasks and tests. This means the configuration has been processed, and the [Hardhat Runtime Environment] has been initialized and injected into the global scope.
15
15
16
16
For example, you'll have access in the global scope to the `config` object:
At its core, Hardhat is a task runner that allows you to automate your development workflow. It comes with some built-in tasks, like `compile` and `test`, but you can add your own custom tasks as well.
4
+
5
+
This guide will show you how to extend Hardhat's functionality using tasks. It assumes you have initialized a sample project. If you haven't done it, please read [this guide](./project-setup.md) first.
6
+
7
+
## Writing Hardhat Tasks
8
+
9
+
Let's write a very simple task that prints the list of available accounts, and explore how it works.
10
+
11
+
Copy this task definition and paste it into your hardhat config file:
12
+
13
+
```js
14
+
task("accounts", "Prints the list of accounts", async (taskArgs, hre) => {
15
+
constaccounts=awaithre.ethers.getSigners();
16
+
17
+
for (constaccountof accounts) {
18
+
console.log(account.address);
19
+
}
20
+
});
21
+
```
22
+
23
+
Now you should be able to run it:
24
+
25
+
```
26
+
npx hardhat accounts
27
+
```
28
+
29
+
We are using the `task` function to define our new task. Its first argument is the name of the task, and it's what we use in the command line to run it. The second argument is the description of the task, which is printed when you use `npx hardhat help`.
30
+
31
+
The third argument is an async function that gets executed when you run the task. It receives two arguments:
32
+
33
+
1. An object with the arguments for the task. We didn't define any yet.
34
+
2. The [Hardhat Runtime Environment](../advanced/hardhat-runtime-environment.md) or HRE, which contains all the functionality of Hardhat and its plugins. You can also find all of its properties injected into the `global` namespace during the task execution.
35
+
36
+
You are free to do anything you want in this function. In this case, we use `ethers.getSigners()` to obtain all the configured accounts and print each of their addresses.
37
+
38
+
You can add parameters to your tasks, and Hardhat will handle their parsing and validation for you.
39
+
40
+
You can also override existing tasks, which allows you to change how different parts of Hardhat work.
41
+
42
+
To learn more about tasks, please read [this guide](../advanced/create-task).
And that's really all it takes. Now you can write your config, tests, tasks and scripts in TypeScript.
121
+
And that's really all it takes. Now you can write your config, tests, tasks and Ignition modules in TypeScript.
122
122
123
123
## Type-checking your project
124
124
@@ -128,11 +128,11 @@ For example, if you run `npx hardhat test` and one of your tests has a compilati
128
128
129
129
Since type-checking adds significant overhead, we recommend to do it only in your CI or in pre-commit/pre-push hooks.
130
130
131
-
## Writing tests and scripts in TypeScript
131
+
## Writing tests in TypeScript
132
132
133
133
When using JavaScript, all the properties in the [Hardhat Runtime Environment](../advanced/hardhat-runtime-environment.md) are injected into the global scope. When using TypeScript nothing will be available in the global scope and you will need to import everything explicitly using, for example, `import { ethers } from "hardhat"`.
134
134
135
-
Follow the [Getting started guide](../getting-started/index.md) and create a TypeScript project for a complete example on how to write tests and scripts using TypeScript.
135
+
Follow the [Getting started guide](../getting-started/index.md) and create a TypeScript project for a complete example on how to write tests using TypeScript.
To support this option when running Hardhat tests or scripts, you need to install the package [`tsconfig-paths`](https://www.npmjs.com/package/tsconfig-paths) and register it in your `hardhat.config.ts`:
160
+
To support this option when running Hardhat tests, you need to install the package [`tsconfig-paths`](https://www.npmjs.com/package/tsconfig-paths) and register it in your `hardhat.config.ts`:
## Running your tests and scripts directly with `ts-node`
175
+
## Running your tests directly with `ts-node`
176
176
177
-
When running Hardhat scripts without the CLI, you need to use `ts-node`'s [`--files` flag](https://www.npmjs.com/package/ts-node#help-my-types-are-missing).
177
+
When running Hardhat tests without the CLI, you need to use `ts-node`'s [`--files` flag](https://www.npmjs.com/package/ts-node#help-my-types-are-missing).
178
178
179
179
This can also be enabled with `TS_NODE_FILES=true`.
0 commit comments