def _build_contract_data(compilation_unit: "CompilationUnit") -> Dict: contracts = {} for filename, contract_names in compilation_unit.filename_to_contracts.items( ): for contract_name in contract_names: abi = str(compilation_unit.abi(contract_name)) abi = abi.replace("'", '"') abi = abi.replace("True", "true") abi = abi.replace("False", "false") abi = abi.replace(" ", "") exported_name = combine_filename_name(filename.absolute, contract_name) contracts[exported_name] = { "srcmap": ";".join(compilation_unit.srcmap_init(contract_name)), "srcmap-runtime": ";".join(compilation_unit.srcmap_runtime(contract_name)), "abi": abi, "bin": compilation_unit.bytecode_init(contract_name), "bin-runtime": compilation_unit.bytecode_runtime(contract_name), "userdoc": compilation_unit.natspec[contract_name].userdoc.export(), "devdoc": compilation_unit.natspec[contract_name].devdoc.export(), } return contracts
def export(crytic_compile: "CryticCompile", **kwargs: str) -> Union[str, None]: """ Export the project to the solc format :param crytic_compile: :param kwargs: :return: """ # Obtain objects to represent each contract contracts = dict() for contract_name in crytic_compile.contracts_names: abi = str(crytic_compile.abi(contract_name)) abi = abi.replace("'", '"') abi = abi.replace("True", "true") abi = abi.replace("False", "false") abi = abi.replace(" ", "") exported_name = combine_filename_name( crytic_compile.contracts_filenames[contract_name].absolute, contract_name) contracts[exported_name] = { "srcmap": ";".join(crytic_compile.srcmap_init(contract_name)), "srcmap-runtime": ";".join(crytic_compile.srcmap_runtime(contract_name)), "abi": abi, "bin": crytic_compile.bytecode_init(contract_name), "bin-runtime": crytic_compile.bytecode_runtime(contract_name), } # Create additional informational objects. sources = { filename: { "AST": ast } for (filename, ast) in crytic_compile.asts.items() } source_list = [x.absolute for x in crytic_compile.filenames] # Create our root object to contain the contracts and other information. output = { "sources": sources, "sourceList": source_list, "contracts": contracts } # If we have an export directory specified, we output the JSON. export_dir = kwargs.get("export_dir", None) if export_dir: if not os.path.exists(export_dir): os.makedirs(export_dir) path = os.path.join(export_dir, "combined_solc.json") with open(path, "w", encoding="utf8") as file_desc: json.dump(output, file_desc) return path return None
def export_to_solc_from_compilation_unit(compilation_unit: "CompilationUnit", key: str, export_dir: str) -> Optional[str]: contracts = dict() for contract_name in compilation_unit.contracts_names: abi = str(compilation_unit.abi(contract_name)) abi = abi.replace("'", '"') abi = abi.replace("True", "true") abi = abi.replace("False", "false") abi = abi.replace(" ", "") exported_name = combine_filename_name( compilation_unit.contracts_filenames[contract_name].absolute, contract_name) contracts[exported_name] = { "srcmap": ";".join(compilation_unit.srcmap_init(contract_name)), "srcmap-runtime": ";".join(compilation_unit.srcmap_runtime(contract_name)), "abi": abi, "bin": compilation_unit.bytecode_init(contract_name), "bin-runtime": compilation_unit.bytecode_runtime(contract_name), "userdoc": compilation_unit.natspec[contract_name].userdoc.export(), "devdoc": compilation_unit.natspec[contract_name].devdoc.export(), } # Create additional informational objects. sources = { filename: { "AST": ast } for (filename, ast) in compilation_unit.asts.items() } source_list = [ x.absolute for x in compilation_unit.crytic_compile.filenames ] # needed for Echidna, see https://github.com/crytic/crytic-compile/issues/112 first_source_list = list(filter(lambda f: "@" in f, source_list)) second_source_list = list(filter(lambda f: "@" not in f, source_list)) first_source_list.sort() second_source_list.sort() source_list = first_source_list + second_source_list # Create our root object to contain the contracts and other information. output = { "sources": sources, "sourceList": source_list, "contracts": contracts } # If we have an export directory specified, we output the JSON. if export_dir: if not os.path.exists(export_dir): os.makedirs(export_dir) path = os.path.join(export_dir, f"{key}.json") with open(path, "w", encoding="utf8") as file_desc: json.dump(output, file_desc) return path return None