Skip to content

Commit 4ee6d3b

Browse files
committed
Refactor copy/paste logic
1 parent 6708c58 commit 4ee6d3b

File tree

2 files changed

+97
-176
lines changed

2 files changed

+97
-176
lines changed

crytic_compile/platform/foundry.py

Lines changed: 3 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from crytic_compile.platform.types import Type
1717
from crytic_compile.utils.naming import convert_filename, extract_name
1818
from crytic_compile.utils.natspec import Natspec
19+
from .hardhat import hardhat_like_parsing
1920

2021
from .solc import relative_to_short
2122

@@ -94,94 +95,8 @@ def compile(self, crytic_compile: "CryticCompile", **kwargs: str) -> None:
9495
out_directory,
9596
"build-info",
9697
)
97-
files = sorted(
98-
os.listdir(build_directory), key=lambda x: os.path.getmtime(Path(build_directory, x))
99-
)
100-
files = [f for f in files if f.endswith(".json")]
101-
if not files:
102-
txt = f"`forge build` failed. Can you run it?\n{build_directory} is empty"
103-
raise InvalidCompilation(txt)
104-
105-
for file in files:
106-
build_info = Path(build_directory, file)
107-
108-
# The file here should always ends .json, but just in case use ife
109-
uniq_id = file if ".json" not in file else file[0:-5]
110-
compilation_unit = CompilationUnit(crytic_compile, uniq_id)
111-
112-
with open(build_info, encoding="utf8") as file_desc:
113-
loaded_json = json.load(file_desc)
114-
115-
targets_json = loaded_json["output"]
116-
117-
version_from_config = loaded_json["solcVersion"] # TODO supper vyper
118-
input_json = loaded_json["input"]
119-
compiler = "solc" if input_json["language"] == "Solidity" else "vyper"
120-
optimized = input_json["settings"]["optimizer"]["enabled"]
121-
122-
compilation_unit.compiler_version = CompilerVersion(
123-
compiler=compiler, version=version_from_config, optimized=optimized
124-
)
125-
126-
skip_filename = compilation_unit.compiler_version.version in [
127-
f"0.4.{x}" for x in range(0, 10)
128-
]
129-
130-
if "contracts" in targets_json:
131-
for original_filename, contracts_info in targets_json["contracts"].items():
132-
133-
filename = convert_filename(
134-
original_filename,
135-
relative_to_short,
136-
crytic_compile,
137-
working_dir=self._target,
138-
)
139-
140-
source_unit = compilation_unit.create_source_unit(filename)
141-
142-
for original_contract_name, info in contracts_info.items():
143-
contract_name = extract_name(original_contract_name)
144-
145-
source_unit.contracts_names.add(contract_name)
146-
compilation_unit.filename_to_contracts[filename].add(contract_name)
147-
148-
source_unit.abis[contract_name] = info["abi"]
149-
source_unit.bytecodes_init[contract_name] = info["evm"]["bytecode"][
150-
"object"
151-
]
152-
source_unit.bytecodes_runtime[contract_name] = info["evm"][
153-
"deployedBytecode"
154-
]["object"]
155-
source_unit.srcmaps_init[contract_name] = info["evm"]["bytecode"][
156-
"sourceMap"
157-
].split(";")
158-
source_unit.srcmaps_runtime[contract_name] = info["evm"][
159-
"deployedBytecode"
160-
]["sourceMap"].split(";")
161-
userdoc = info.get("userdoc", {})
162-
devdoc = info.get("devdoc", {})
163-
natspec = Natspec(userdoc, devdoc)
164-
source_unit.natspec[contract_name] = natspec
165-
166-
if "sources" in targets_json:
167-
for path, info in targets_json["sources"].items():
168-
if skip_filename:
169-
path = convert_filename(
170-
self._target,
171-
relative_to_short,
172-
crytic_compile,
173-
working_dir=self._target,
174-
)
175-
else:
176-
path = convert_filename(
177-
path,
178-
relative_to_short,
179-
crytic_compile,
180-
working_dir=self._target,
181-
)
182-
183-
source_unit = compilation_unit.create_source_unit(path)
184-
source_unit.ast = info["ast"]
98+
99+
hardhat_like_parsing(crytic_compile, self._target, build_directory, self._target)
185100

186101
@staticmethod
187102
def is_supported(target: str, **kwargs: str) -> bool:

crytic_compile/platform/hardhat.py

Lines changed: 94 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,99 @@
2626
LOGGER = logging.getLogger("CryticCompile")
2727

2828

29+
def hardhat_like_parsing(
30+
crytic_compile: "CryticCompile", target: str, build_directory: Path, working_dir: str
31+
) -> None:
32+
files = sorted(
33+
os.listdir(build_directory), key=lambda x: os.path.getmtime(Path(build_directory, x))
34+
)
35+
files = [str(f) for f in files if str(f).endswith(".json")]
36+
if not files:
37+
txt = f"`hardhat compile` failed. Can you run it?\n{build_directory} is empty"
38+
raise InvalidCompilation(txt)
39+
40+
for file in files:
41+
build_info = Path(build_directory, file)
42+
43+
# The file here should always ends .json, but just in case use ife
44+
uniq_id = file if ".json" not in file else file[0:-5]
45+
compilation_unit = CompilationUnit(crytic_compile, uniq_id)
46+
47+
with open(build_info, encoding="utf8") as file_desc:
48+
loaded_json = json.load(file_desc)
49+
50+
targets_json = loaded_json["output"]
51+
52+
version_from_config = loaded_json["solcVersion"] # TODO supper vyper
53+
input_json = loaded_json["input"]
54+
compiler = "solc" if input_json["language"] == "Solidity" else "vyper"
55+
optimized = input_json["settings"]["optimizer"]["enabled"]
56+
57+
compilation_unit.compiler_version = CompilerVersion(
58+
compiler=compiler, version=version_from_config, optimized=optimized
59+
)
60+
61+
skip_filename = compilation_unit.compiler_version.version in [
62+
f"0.4.{x}" for x in range(0, 10)
63+
]
64+
65+
if "contracts" in targets_json:
66+
for original_filename, contracts_info in targets_json["contracts"].items():
67+
68+
filename = convert_filename(
69+
original_filename,
70+
relative_to_short,
71+
crytic_compile,
72+
working_dir=working_dir,
73+
)
74+
75+
source_unit = compilation_unit.create_source_unit(filename)
76+
77+
for original_contract_name, info in contracts_info.items():
78+
contract_name = extract_name(original_contract_name)
79+
80+
source_unit.contracts_names.add(contract_name)
81+
compilation_unit.filename_to_contracts[filename].add(contract_name)
82+
83+
source_unit.abis[contract_name] = info["abi"]
84+
source_unit.bytecodes_init[contract_name] = info["evm"]["bytecode"][
85+
"object"
86+
]
87+
source_unit.bytecodes_runtime[contract_name] = info["evm"][
88+
"deployedBytecode"
89+
]["object"]
90+
source_unit.srcmaps_init[contract_name] = info["evm"]["bytecode"][
91+
"sourceMap"
92+
].split(";")
93+
source_unit.srcmaps_runtime[contract_name] = info["evm"][
94+
"deployedBytecode"
95+
]["sourceMap"].split(";")
96+
userdoc = info.get("userdoc", {})
97+
devdoc = info.get("devdoc", {})
98+
natspec = Natspec(userdoc, devdoc)
99+
source_unit.natspec[contract_name] = natspec
100+
101+
if "sources" in targets_json:
102+
for path, info in targets_json["sources"].items():
103+
if skip_filename:
104+
path = convert_filename(
105+
target,
106+
relative_to_short,
107+
crytic_compile,
108+
working_dir=working_dir,
109+
)
110+
else:
111+
path = convert_filename(
112+
path,
113+
relative_to_short,
114+
crytic_compile,
115+
working_dir=working_dir,
116+
)
117+
118+
source_unit = compilation_unit.create_source_unit(path)
119+
source_unit.ast = info["ast"]
120+
121+
29122
class Hardhat(AbstractPlatform):
30123
"""
31124
Hardhat platform
@@ -88,94 +181,7 @@ def compile(self, crytic_compile: "CryticCompile", **kwargs: str) -> None:
88181
if stderr:
89182
LOGGER.error(stderr)
90183

91-
files = sorted(
92-
os.listdir(build_directory), key=lambda x: os.path.getmtime(Path(build_directory, x))
93-
)
94-
files = [f for f in files if f.endswith(".json")]
95-
if not files:
96-
txt = f"`hardhat compile` failed. Can you run it?\n{build_directory} is empty"
97-
raise InvalidCompilation(txt)
98-
99-
for file in files:
100-
build_info = Path(build_directory, file)
101-
102-
# The file here should always ends .json, but just in case use ife
103-
uniq_id = file if ".json" not in file else file[0:-5]
104-
compilation_unit = CompilationUnit(crytic_compile, uniq_id)
105-
106-
with open(build_info, encoding="utf8") as file_desc:
107-
loaded_json = json.load(file_desc)
108-
109-
targets_json = loaded_json["output"]
110-
111-
version_from_config = loaded_json["solcVersion"] # TODO supper vyper
112-
input_json = loaded_json["input"]
113-
compiler = "solc" if input_json["language"] == "Solidity" else "vyper"
114-
optimized = input_json["settings"]["optimizer"]["enabled"]
115-
116-
compilation_unit.compiler_version = CompilerVersion(
117-
compiler=compiler, version=version_from_config, optimized=optimized
118-
)
119-
120-
skip_filename = compilation_unit.compiler_version.version in [
121-
f"0.4.{x}" for x in range(0, 10)
122-
]
123-
124-
if "contracts" in targets_json:
125-
for original_filename, contracts_info in targets_json["contracts"].items():
126-
127-
filename = convert_filename(
128-
original_filename,
129-
relative_to_short,
130-
crytic_compile,
131-
working_dir=hardhat_working_dir,
132-
)
133-
134-
source_unit = compilation_unit.create_source_unit(filename)
135-
136-
for original_contract_name, info in contracts_info.items():
137-
contract_name = extract_name(original_contract_name)
138-
139-
source_unit.contracts_names.add(contract_name)
140-
compilation_unit.filename_to_contracts[filename].add(contract_name)
141-
142-
source_unit.abis[contract_name] = info["abi"]
143-
source_unit.bytecodes_init[contract_name] = info["evm"]["bytecode"][
144-
"object"
145-
]
146-
source_unit.bytecodes_runtime[contract_name] = info["evm"][
147-
"deployedBytecode"
148-
]["object"]
149-
source_unit.srcmaps_init[contract_name] = info["evm"]["bytecode"][
150-
"sourceMap"
151-
].split(";")
152-
source_unit.srcmaps_runtime[contract_name] = info["evm"][
153-
"deployedBytecode"
154-
]["sourceMap"].split(";")
155-
userdoc = info.get("userdoc", {})
156-
devdoc = info.get("devdoc", {})
157-
natspec = Natspec(userdoc, devdoc)
158-
source_unit.natspec[contract_name] = natspec
159-
160-
if "sources" in targets_json:
161-
for path, info in targets_json["sources"].items():
162-
if skip_filename:
163-
path = convert_filename(
164-
self._target,
165-
relative_to_short,
166-
crytic_compile,
167-
working_dir=hardhat_working_dir,
168-
)
169-
else:
170-
path = convert_filename(
171-
path,
172-
relative_to_short,
173-
crytic_compile,
174-
working_dir=hardhat_working_dir,
175-
)
176-
177-
source_unit = compilation_unit.create_source_unit(path)
178-
source_unit.ast = info["ast"]
184+
hardhat_like_parsing(crytic_compile, self._target, build_directory, hardhat_working_dir)
179185

180186
@staticmethod
181187
def is_supported(target: str, **kwargs: str) -> bool:

0 commit comments

Comments
 (0)