Skip to content

Commit b5f0d62

Browse files
committed
feat: auto-generate crytic_compile.config.json
1 parent 1e5deb2 commit b5f0d62

File tree

1 file changed

+14
-45
lines changed

1 file changed

+14
-45
lines changed

crytic_compile/platform/etherscan.py

Lines changed: 14 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -423,51 +423,20 @@ def compile(self, crytic_compile: "CryticCompile", **kwargs: str) -> None:
423423
evm_version=evm_version,
424424
via_ir=via_ir_enabled,
425425
)
426-
427-
# Process filenames in target compilationUnit to build solc_remaps.txt
428-
remaps = self.solc_remaps_generator(compilation_unit)
429-
430-
# Convert to string to use with crytic-compile Target.sol --solc-remaps $(cat solc_remaps.txt)
431-
remaps_str = ",".join(remaps)
432-
with open(os.path.join(working_dir, "solc_remaps.txt"), "w") as f:
433-
f.write(f'"{remaps_str}"')
434-
435-
def solc_remaps_generator(self, compilationTarget: CompilationUnit) -> List[str]:
436-
"""Generate remapings for --solc-remaps argument. Uses absolute paths.
437-
438-
Args:
439-
compilationTarget (CompilationUnit): current compilation target
440-
441-
Returns:
442-
List[str]: List of remappings deduced from filenames and compilationUnit.source_units
443-
"""
444-
solc_remaps = []
445-
processed_remaps = set() # Avoid duplicates
446-
source_units = compilationTarget.source_units
447-
for filename, source_unit in source_units.items():
448-
short_name = filename.short
449-
if short_name.startswith("@"):
450-
remap_name = short_name.split("/", 1)[0]
451-
452-
# Skip if this remap_name has already been processed
453-
if remap_name in processed_remaps:
454-
continue
455-
456-
remap_path = filename.absolute
457-
458-
# Extract the path up to and including the remap_name directory
459-
remap_index = remap_path.find(remap_name + "/")
460-
if remap_index != -1:
461-
remap_path = remap_path[: remap_index + len(remap_name)]
462-
463-
print(f"{remap_name}={remap_path}")
464-
465-
solc_remaps.append(f"{remap_name}={remap_path}")
466-
467-
# Mark this remap_name as processed
468-
processed_remaps.add(remap_name)
469-
470-
return solc_remaps
426+
427+
metadata_config = {
428+
"solc_remaps": remappings if remappings else {},
429+
"solc_solcs_select": compiler_version,
430+
"solc_args": ("--via-ir" if via_ir_enabled else "")
431+
+ ("--optimize --optimize-runs " + str(optimize_runs) if optimize_runs else "")
432+
+ ("--evm-version " + evm_version if evm_version else ""),
433+
}
434+
435+
with open(
436+
os.path.join(working_dir if working_dir else export_dir, "crytic_compile.config.json"),
437+
"w",
438+
) as f:
439+
json.dump(metadata_config, f)
471440

472441
def clean(self, **_kwargs: str) -> None:
473442
pass

0 commit comments

Comments
 (0)