Skip to content

Commit

Permalink
Merge pull request #12 from moonstream-to/hardhat
Browse files Browse the repository at this point in the history
Add support for hardhat projects
  • Loading branch information
zomglings authored Oct 25, 2024
2 parents 6b18bdb + 660921d commit 7ed3edf
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 9 deletions.
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ is serving to human-understandable information about the facets and the function
We support side information obtained from:

- [x] [brownie](https://github.com/eth-brownie/brownie) build artifacts
- [ ] [hardhat](https://hardhat.org/) build artifacts
- [x] [foundry](https://github.com/foundry-rs/foundry) build artifacts
- [x] [hardhat](https://hardhat.org/) build artifacts
- [ ] Etherscan/Polygonscan/etc.

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

#### With a `hardhat` project

```bash
inspector-facet \
--network <brownie network name for blockchain> \
--address <address of diamond contract> \
--project <path to foundry project> \
--hardhat \
--format human
```

The following command produces JSON output and can be used to inspect a Diamond contract programatically
(e.g. as part of a CI/CD pipeline):
```bash
inspector-facet \
--network <brownie network name for blockchain> \
--address <address of diamond contract> \
--project <path to foundry project> \
--hardhat \
--format json
```

#### Non-standard build directories

The `--build-dir` command allows you to specify the name of the build directory in your `brownie` or
Expand Down
35 changes: 32 additions & 3 deletions inspector_facet/abi.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ def encode_function_signature(function_abi: Dict[str, Any]) -> Optional[str]:
encoded_signature = Web3.keccak(text=function_signature)[:4]
return encoded_signature.hex()

def foundry_project_abis(project_dir: str, build_dirname: None) -> Dict[str, List[Dict[str, Any]]]:
def foundry_project_abis(project_dir: str, build_dirname: Optional[str] = None) -> Dict[str, List[Dict[str, Any]]]:
"""
Load all ABIs for project contracts and return then in a dictionary keyed by contract name.
Inputs:
- project_dir
Path to brownie project
Path to foundry project
"""
if build_dirname is None:
build_dirname = "out"
Expand All @@ -81,7 +81,7 @@ def foundry_project_abis(project_dir: str, build_dirname: None) -> Dict[str, Lis

return abis

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

return abis

def hardhat_project_abis(project_dir: str, build_dirname: Optional[str] = None):
"""
Load all ABIs for project contracts and return then in a dictionary keyed by contract name.
Inputs:
- project_dir
Path to foundry project
"""
if build_dirname is None:
build_dirname = "artifacts"

build_dir = os.path.join(project_dir, build_dirname)
build_files = glob.glob(os.path.join(build_dir, "**/*.json"), recursive=True)

abis: Dict[str, List[Dict[str, Any]]] = {}

for filepath in build_files:
contract_name, _ = os.path.splitext(os.path.basename(filepath))
with open(filepath, "r") as ifp:
contract_artifact = json.load(ifp)

try:
contract_abi = contract_artifact.get("abi", [])
abis[contract_name] = contract_abi
except Exception:
continue

return abis
19 changes: 15 additions & 4 deletions inspector_facet/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import sys
from typing import Any, Dict, List, Optional, Tuple

from .abi import brownie_project_abis, foundry_project_abis
from .abi import brownie_project_abis, foundry_project_abis, hardhat_project_abis
from .facets import (
events_from_moonworm_crawldata,
facets_from_loupe,
Expand Down Expand Up @@ -151,14 +151,25 @@ def main():
help="Use the foundry project structure instead of the brownie project structure",
)

parser.add_argument(
"--hardhat",
action="store_true",
help="Use the hardhat project structure instead of the brownie project structure",
)

parser.add_argument("--build-dir", default=None, required=False, help="Name of build directory (if it isn't the default name)")

args = parser.parse_args()

if not args.foundry:
abis = brownie_project_abis(args.project, args.build_dir)
else:
if args.foundry and args.hardhat:
raise ValueError("You cannot specify both --foundry and --hardhat at the same time")

if args.foundry:
abis = foundry_project_abis(args.project, args.build_dir)
elif args.hardhat:
abis = hardhat_project_abis(args.project, args.build_dir)
else:
abis = brownie_project_abis(args.project, args.build_dir)

if not args.timeline:
facets = None
Expand Down
2 changes: 1 addition & 1 deletion inspector_facet/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.3.1
0.4.0

0 comments on commit 7ed3edf

Please sign in to comment.