|
26 | 26 | LOGGER = logging.getLogger("CryticCompile")
|
27 | 27 |
|
28 | 28 |
|
| 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 | + |
29 | 122 | class Hardhat(AbstractPlatform):
|
30 | 123 | """
|
31 | 124 | Hardhat platform
|
@@ -88,94 +181,7 @@ def compile(self, crytic_compile: "CryticCompile", **kwargs: str) -> None:
|
88 | 181 | if stderr:
|
89 | 182 | LOGGER.error(stderr)
|
90 | 183 |
|
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) |
179 | 185 |
|
180 | 186 | @staticmethod
|
181 | 187 | def is_supported(target: str, **kwargs: str) -> bool:
|
|
0 commit comments