Skip to content

Commit b66a86d

Browse files
zoeyTMkanej
authored andcommitted
Update scripts guide (#4853)
* update scripts guide to remove deployment example * de-emphasize scripts in general
1 parent f6414ed commit b66a86d

File tree

13 files changed

+78
-138
lines changed

13 files changed

+78
-138
lines changed

docs/src/content/hardhat-network/docs/guides/forking-other-networks.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ Hardhat Network will by default fork from a recent mainnet block. While this mig
7373

7474
There are two reasons for this:
7575

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.
7777
- 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.
7878

7979
**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.

docs/src/content/hardhat-network/docs/overview/index.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ When Hardhat executes your tests, scripts or tasks, an in-process Hardhat Networ
2525

2626
There's no need to make any changes to your tests or scripts.
2727

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`.
2929

3030
### Running stand-alone in order to support wallets and other software
3131

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:
3333

3434
```
3535
$ npx hardhat node

docs/src/content/hardhat-runner/docs/advanced/building-plugins.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Some examples of things you could achieve by creating a plugin are: running a li
1212

1313
## Extending the Hardhat Runtime Environment
1414

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.
1616

1717
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.
1818

@@ -129,7 +129,7 @@ An example on how to add fields to the Hardhat config can be found in [`src/inde
129129

130130
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`.
131131

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`.
133133

134134
### Optimizing your plugin for better startup time
135135

docs/src/content/hardhat-runner/docs/advanced/scripts.md

+18-33
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
# Writing scripts with Hardhat
22

3-
<!-- TODO: We should write this guide based on a different common use case for scripts now that Ignition exists -->
4-
53
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).
64

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.
86

97
There are two ways of writing a script that accesses the [Hardhat Runtime Environment](../advanced/hardhat-runtime-environment.md).
108

@@ -26,45 +24,32 @@ const hre = require("hardhat");
2624

2725
You can get access to all your tasks and plugins. To run these scripts you simply go through node: `node script.js`.
2826

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:
30-
31-
```
32-
$ ls -l
33-
total 400
34-
drwxr-xr-x 3 fzeoli staff 96 Jul 30 15:27 contracts
35-
-rw-r--r-- 1 fzeoli staff 195 Jul 30 15:27 hardhat.config.js
36-
drwxr-xr-x 502 fzeoli staff 16064 Jul 30 15:31 node_modules
37-
-rw-r--r-- 1 fzeoli staff 194953 Jul 30 15:31 package-lock.json
38-
-rw-r--r-- 1 fzeoli staff 365 Jul 30 15:31 package.json
39-
drwxr-xr-x 3 fzeoli staff 96 Jul 30 15:27 scripts
40-
drwxr-xr-x 3 fzeoli staff 96 Jul 30 15:27 test
41-
```
27+
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:
4228

43-
Inside `scripts/` you will find `deploy.js`. Read through its comments to have a better idea of what it does.
29+
```js
30+
const hre = require("hardhat");
4431

45-
<!-- FIX: What needs to be swapped in for this? -->
46-
<!-- <<< @/../packages/hardhat-core/sample-projects/javascript/scripts/deploy.js -->
32+
async function main() {
33+
const accounts = await hre.ethers.getSigners();
4734

48-
Now run the script:
35+
for (const account of accounts) {
36+
console.log(account.address);
37+
}
38+
}
4939

50-
```
51-
$ node scripts/deploy.js
52-
Lock with 1 ETH deployed to: 0x5FbDB2315678afecb367f032d93F642f64180aa3
40+
main().catch((error) => {
41+
console.error(error);
42+
process.exitCode = 1;
43+
});
5344
```
5445

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:
5847

59-
```js
60-
await hre.run("compile");
48+
```sh
49+
node scripts/accounts.js
6150
```
6251

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.
6853

6954
### Hardhat arguments
7055

docs/src/content/hardhat-runner/docs/getting-started/index.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ If you are using Windows, we **strongly recommend** using [WSL 2](https://docs.m
6565

6666
:::
6767

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.
6969

7070
To create the sample project, run `npx hardhat init` in your project folder:
7171

@@ -321,7 +321,7 @@ To learn more check out the [Hardhat Ignition documentation](/ignition).
321321

322322
### Connecting a wallet or Dapp to Hardhat Network
323323

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.
325325

326326
To run Hardhat Network in this way, run `npx hardhat node`:
327327

docs/src/content/hardhat-runner/docs/guides/_dirinfo.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ order:
66
- /test-contracts
77
- /deploying
88
- /verifying
9-
- /tasks-and-scripts
9+
- /tasks
1010
- /hardhat-console
1111
- /typescript
1212
- /command-line-completion

docs/src/content/hardhat-runner/docs/guides/hardhat-console.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Type ".help" for more information.
1111

1212
The `compile` task will be called before opening the console prompt, but you can skip this with the `--no-compile` parameter.
1313

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.
1515

1616
For example, you'll have access in the global scope to the `config` object:
1717

docs/src/content/hardhat-runner/docs/guides/project-setup.md

-2
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ The initialized project has the following structure:
111111
```
112112
contracts/
113113
ignition/modules/
114-
scripts/
115114
test/
116115
hardhat.config.js
117116
```
@@ -121,7 +120,6 @@ These are the default paths for a Hardhat project.
121120
- `contracts/` is where the source files for your contracts should be.
122121
- `ignition/modules/` is where the Ignition modules that handle contract deployments should be.
123122
- `test/` is where your tests should go.
124-
- `scripts/` is where simple automation scripts go.
125123

126124
If you need to change these paths, take a look at the [paths configuration section](../config/index.md#path-configuration).
127125

docs/src/content/hardhat-runner/docs/guides/tasks-and-scripts.md

-85
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Writing tasks
2+
3+
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+
const accounts = await hre.ethers.getSigners();
16+
17+
for (const account of 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).

docs/src/content/hardhat-runner/docs/guides/typescript.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ drwxr-xr-x 3 pato wheel 96 Oct 20 12:50 contracts/
9090
drwxr-xr-x 434 pato wheel 13888 Oct 20 12:52 node_modules/
9191
-rw-r--r-- 1 pato wheel 604835 Oct 20 12:52 package-lock.json
9292
-rw-r--r-- 1 pato wheel 460 Oct 20 12:52 package.json
93-
drwxr-xr-x 3 pato wheel 96 Oct 20 12:50 scripts/
93+
drwxr-xr-x 3 pato wheel 96 Oct 20 12:50 ignition/modules/
9494
drwxr-xr-x 3 pato wheel 96 Oct 20 12:50 test/
9595
```
9696

@@ -118,7 +118,7 @@ Finally, you need to create a [`tsconfig.json`](https://www.typescriptlang.org/d
118118

119119
<<< @/../packages/hardhat-core/sample-projects/typescript/tsconfig.json
120120

121-
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.
122122

123123
## Type-checking your project
124124

@@ -128,11 +128,11 @@ For example, if you run `npx hardhat test` and one of your tests has a compilati
128128

129129
Since type-checking adds significant overhead, we recommend to do it only in your CI or in pre-commit/pre-push hooks.
130130

131-
## Writing tests and scripts in TypeScript
131+
## Writing tests in TypeScript
132132

133133
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"`.
134134

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.
136136

137137
## Type-safe smart contract interactions
138138

@@ -157,7 +157,7 @@ Typescript allows defining custom [path mappings](https://www.typescriptlang.org
157157
}
158158
```
159159

160-
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`:
161161

162162
```typescript
163163
import { HardhatUserConfig } from "hardhat/config";
@@ -172,9 +172,9 @@ const config: HardhatUserConfig = {
172172
export default config;
173173
```
174174

175-
## Running your tests and scripts directly with `ts-node`
175+
## Running your tests directly with `ts-node`
176176

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).
178178

179179
This can also be enabled with `TS_NODE_FILES=true`.
180180

docs/src/content/hardhat-runner/docs/other-guides/ganache-tests.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ npx hardhat --network localhost test
2222

2323
If you don't want to manually start and stop Ganache every time, you can use the `hardhat-ganache` plugin.
2424

25-
This plugin creates a network called `ganache`, and automatically starts and stops Ganache before and after running your tests and scripts.
25+
This plugin creates a network called `ganache`, and automatically starts and stops Ganache before and after running your tests.
2626

2727
To use it, you have to install it with npm
2828

0 commit comments

Comments
 (0)