Skip to content

Commit d019f7f

Browse files
authored
Merge pull request #3429 from NomicFoundation/add-flatten-guide
Add guide about flattening contracts
2 parents bf5ebf2 + ab1cfc7 commit d019f7f

File tree

5 files changed

+76
-2
lines changed

5 files changed

+76
-2
lines changed

.changeset/nasty-maps-hang.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"hardhat": patch
3+
---
4+
5+
Added a minor clarification to the `flatten` task help.

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

+1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ order:
77
- /create-task
88
- /scripts
99
- /building-plugins
10+
- /flattening
1011
- href: /vscode-tests
1112
title: Running tests in VS Code
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Flattening your contracts
2+
3+
Hardhat comes with a built-in `flatten` task that lets you combine the source code of multiple Solidity files.
4+
5+
## Flattening all your files
6+
7+
If you use the `flatten` task without passing any other arguments, all the Solidity files in your project will be combined:
8+
9+
```
10+
$ npx hardhat flatten
11+
// Sources flattened with hardhat v2.12.3 https://hardhat.org
12+
13+
// File contracts/Bar.sol
14+
15+
// SPDX-License-Identifier: MIT
16+
pragma solidity ^0.8.0;
17+
18+
contract Bar {}
19+
20+
// File contracts/Qux.sol
21+
22+
...
23+
```
24+
25+
The result will be printed to stdout. You can create a file with the flattened sources using the `>` redirection operator:
26+
27+
```
28+
$ npx hardhat flatten > Flattened.sol
29+
30+
$ cat Flattened.sol
31+
// Sources flattened with hardhat v2.12.3 https://hardhat.org
32+
33+
// File contracts/Bar.sol
34+
35+
...
36+
```
37+
38+
## Flattening specific files
39+
40+
The `flatten` task can receive a path to the file you want to flatten:
41+
42+
```
43+
npx hardhat flatten contracts/Foo.sol
44+
```
45+
46+
In this case, the result will contain the source code of `Foo.sol` and all its transitive dependencies (the files that it imports, and the files that those files import, and so on).
47+
48+
You can also use multiple files:
49+
50+
```
51+
npx hardhat flatten contracts/Foo.sol contracts/Bar.sol
52+
```
53+
54+
But if `Bar.sol` is a dependency of `Foo.sol`, then the result will be the same as in the previous example.
55+
56+
As explained in the previous section, you can redirect the output to some file:
57+
58+
```
59+
npx hardhat flatten contracts/Foo.sol > Flattened.sol
60+
```
61+
62+
## Circular dependencies
63+
64+
Projects with circular dependencies cannot be flattened at the moment. If this is something you need, please upvote or comment [this issue](https://github.com/NomicFoundation/hardhat/issues/1486).

packages/hardhat-core/src/builtin-tasks/flatten.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,10 @@ subtask(TASK_FLATTEN_GET_DEPENDENCY_GRAPH)
124124
return dependencyGraph;
125125
});
126126

127-
task(TASK_FLATTEN, "Flattens and prints contracts and their dependencies")
127+
task(
128+
TASK_FLATTEN,
129+
"Flattens and prints contracts and their dependencies. If no file is passed, all the contracts in the project will be flattened."
130+
)
128131
.addOptionalVariadicPositionalParam(
129132
"files",
130133
"The files to flatten",

packages/hardhat-core/test/internal/cli/autocomplete.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ const coreTasks = [
4646
name: "console",
4747
},
4848
{
49-
description: "Flattens and prints contracts and their dependencies",
49+
description:
50+
"Flattens and prints contracts and their dependencies. If no file is passed, all the contracts in the project will be flattened.",
5051
name: "flatten",
5152
},
5253
{

0 commit comments

Comments
 (0)