Skip to content

Commit 7ed3edf

Browse files
authored
Merge pull request #12 from moonstream-to/hardhat
Add support for hardhat projects
2 parents 6b18bdb + 660921d commit 7ed3edf

File tree

4 files changed

+72
-9
lines changed

4 files changed

+72
-9
lines changed

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ is serving to human-understandable information about the facets and the function
1313
We support side information obtained from:
1414

1515
- [x] [brownie](https://github.com/eth-brownie/brownie) build artifacts
16-
- [ ] [hardhat](https://hardhat.org/) build artifacts
16+
- [x] [foundry](https://github.com/foundry-rs/foundry) build artifacts
17+
- [x] [hardhat](https://hardhat.org/) build artifacts
1718
- [ ] Etherscan/Polygonscan/etc.
1819

1920
Inspector Facet can build a complete audit log of all Diamond-related operations on an EIP2535 proxy
@@ -85,6 +86,28 @@ inspector-facet \
8586
--format json
8687
```
8788

89+
#### With a `hardhat` project
90+
91+
```bash
92+
inspector-facet \
93+
--network <brownie network name for blockchain> \
94+
--address <address of diamond contract> \
95+
--project <path to foundry project> \
96+
--hardhat \
97+
--format human
98+
```
99+
100+
The following command produces JSON output and can be used to inspect a Diamond contract programatically
101+
(e.g. as part of a CI/CD pipeline):
102+
```bash
103+
inspector-facet \
104+
--network <brownie network name for blockchain> \
105+
--address <address of diamond contract> \
106+
--project <path to foundry project> \
107+
--hardhat \
108+
--format json
109+
```
110+
88111
#### Non-standard build directories
89112

90113
The `--build-dir` command allows you to specify the name of the build directory in your `brownie` or

inspector_facet/abi.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ def encode_function_signature(function_abi: Dict[str, Any]) -> Optional[str]:
5252
encoded_signature = Web3.keccak(text=function_signature)[:4]
5353
return encoded_signature.hex()
5454

55-
def foundry_project_abis(project_dir: str, build_dirname: None) -> Dict[str, List[Dict[str, Any]]]:
55+
def foundry_project_abis(project_dir: str, build_dirname: Optional[str] = None) -> Dict[str, List[Dict[str, Any]]]:
5656
"""
5757
Load all ABIs for project contracts and return then in a dictionary keyed by contract name.
5858
5959
Inputs:
6060
- project_dir
61-
Path to brownie project
61+
Path to foundry project
6262
"""
6363
if build_dirname is None:
6464
build_dirname = "out"
@@ -81,7 +81,7 @@ def foundry_project_abis(project_dir: str, build_dirname: None) -> Dict[str, Lis
8181

8282
return abis
8383

84-
def brownie_project_abis(project_dir: str, build_dirname: None) -> Dict[str, List[Dict[str, Any]]]:
84+
def brownie_project_abis(project_dir: str, build_dirname: Optional[str] = None) -> Dict[str, List[Dict[str, Any]]]:
8585
"""
8686
Load all ABIs for project contracts and return then in a dictionary keyed by contract name.
8787
@@ -107,3 +107,32 @@ def brownie_project_abis(project_dir: str, build_dirname: None) -> Dict[str, Lis
107107
abis[contract_name] = contract_abi
108108

109109
return abis
110+
111+
def hardhat_project_abis(project_dir: str, build_dirname: Optional[str] = None):
112+
"""
113+
Load all ABIs for project contracts and return then in a dictionary keyed by contract name.
114+
115+
Inputs:
116+
- project_dir
117+
Path to foundry project
118+
"""
119+
if build_dirname is None:
120+
build_dirname = "artifacts"
121+
122+
build_dir = os.path.join(project_dir, build_dirname)
123+
build_files = glob.glob(os.path.join(build_dir, "**/*.json"), recursive=True)
124+
125+
abis: Dict[str, List[Dict[str, Any]]] = {}
126+
127+
for filepath in build_files:
128+
contract_name, _ = os.path.splitext(os.path.basename(filepath))
129+
with open(filepath, "r") as ifp:
130+
contract_artifact = json.load(ifp)
131+
132+
try:
133+
contract_abi = contract_artifact.get("abi", [])
134+
abis[contract_name] = contract_abi
135+
except Exception:
136+
continue
137+
138+
return abis

inspector_facet/cli.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import sys
44
from typing import Any, Dict, List, Optional, Tuple
55

6-
from .abi import brownie_project_abis, foundry_project_abis
6+
from .abi import brownie_project_abis, foundry_project_abis, hardhat_project_abis
77
from .facets import (
88
events_from_moonworm_crawldata,
99
facets_from_loupe,
@@ -151,14 +151,25 @@ def main():
151151
help="Use the foundry project structure instead of the brownie project structure",
152152
)
153153

154+
parser.add_argument(
155+
"--hardhat",
156+
action="store_true",
157+
help="Use the hardhat project structure instead of the brownie project structure",
158+
)
159+
154160
parser.add_argument("--build-dir", default=None, required=False, help="Name of build directory (if it isn't the default name)")
155161

156162
args = parser.parse_args()
157163

158-
if not args.foundry:
159-
abis = brownie_project_abis(args.project, args.build_dir)
160-
else:
164+
if args.foundry and args.hardhat:
165+
raise ValueError("You cannot specify both --foundry and --hardhat at the same time")
166+
167+
if args.foundry:
161168
abis = foundry_project_abis(args.project, args.build_dir)
169+
elif args.hardhat:
170+
abis = hardhat_project_abis(args.project, args.build_dir)
171+
else:
172+
abis = brownie_project_abis(args.project, args.build_dir)
162173

163174
if not args.timeline:
164175
facets = None

inspector_facet/version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.3.1
1+
0.4.0

0 commit comments

Comments
 (0)