def extract_interface_str(code, contract_name, interface_codes=None): sigs = sig_utils.mk_full_signature( parser.parse_to_ast(code), sig_formatter=lambda x, y: (x, y), interface_codes=interface_codes, ) events = [sig for sig, _ in sigs if isinstance(sig, EventSignature)] functions = [sig for sig, _ in sigs if isinstance(sig, FunctionSignature)] out = "" # Print events. for idx, event in enumerate(events): if idx == 0: out += "# Events\n\n" event_args_str = ', '.join([arg.name + ': ' + str(arg.typ) for arg in event.args]) out += f"{event.name}: event({{{event_args_str}}})\n" # Print functions. def render_decorator(sig): o = "\n" if sig.const: o += "@constant\n" if not sig.private: o += "@public\n" return o for idx, func in enumerate(functions): if idx == 0: out += "\n# Functions\n" if not func.private and func.name != '__init__': args = ", ".join([arg.name + ": " + str(arg.typ) for arg in func.args]) out += f"{render_decorator(func)}def {func.name}({args}){render_return(func)}:\n pass\n" # noqa: E501 out += "\n" return out
def extract_interface_str(global_ctx): sigs = sig_utils.mk_full_signature(global_ctx, sig_formatter=lambda x: x) events = [i for i in sigs if isinstance(i, EventSignature)] functions = [i for i in sigs if isinstance(i, FunctionSignature)] out = "" # Print events. for idx, event in enumerate(events): if idx == 0: out += "# Events\n\n" if event.args: event_args_str = "\n ".join( [arg.name + ": " + str(arg.typ) for arg in event.args]) else: event_args_str = "pass" out += f"event {event.name}:\n {event_args_str}\n" # Print functions. def render_decorator(sig): o = "\n" if sig.mutability != "nonpayable": o += f"@{sig.mutability}\n" if not sig.internal: o += "@external\n" return o for idx, func in enumerate(functions): if idx == 0: out += "\n# Functions\n" if not func.internal and func.name != "__init__": args = ", ".join( [arg.name + ": " + str(arg.typ) for arg in func.args]) out += f"{render_decorator(func)}def {func.name}({args}){render_return(func)}:\n pass\n" # noqa: E501 out += "\n" return out
def extract_sigs(sig_code): if sig_code['type'] == 'vyper': return sig_utils.mk_full_signature(parser.parse_to_ast( sig_code['code']), sig_formatter=lambda x, y: x) elif sig_code['type'] == 'json': return mk_full_signature_from_json(sig_code['code']) else: raise Exception( ("Unknown interface signature type '{}' supplied. " "'vyper' & 'json' are supported").format(sig_code['type']))
def extract_sigs(sig_code): if sig_code['type'] == 'vyper': interface_ast = parser.parse_to_ast(sig_code['code']) return sig_utils.mk_full_signature( [i for i in interface_ast if not isinstance(i, (ast.Import, ast.ImportFrom))], sig_formatter=lambda x, y: x ) elif sig_code['type'] == 'json': return mk_full_signature_from_json(sig_code['code']) else: raise Exception( (f"Unknown interface signature type '{sig_code['type']}' supplied. " "'vyper' & 'json' are supported") )
def extract_sigs(sig_code): if sig_code['type'] == 'vyper': interface_ast = [ i for i in parser.parse_to_ast(sig_code['code']) if isinstance(i, ast.FunctionDef) or ( isinstance(i, ast.AnnAssign) and i.target.id != "implements") ] return sig_utils.mk_full_signature(interface_ast, sig_formatter=lambda x, y: x) elif sig_code['type'] == 'json': return mk_full_signature_from_json(sig_code['code']) else: raise Exception(( f"Unknown interface signature type '{sig_code['type']}' supplied. " "'vyper' & 'json' are supported"))
def extract_external_interface(global_ctx, contract_name): sigs = sig_utils.mk_full_signature(global_ctx, sig_formatter=lambda x: x,) functions = [i for i in sigs if isinstance(i, FunctionSignature)] cname = Path(contract_name).stem.capitalize() out = "" offset = 4 * " " for idx, func in enumerate(functions): if idx == 0: out += f"\n# External Interfaces\ninterface {cname}:\n" if not func.private and func.name != "__init__": args = ", ".join([arg.name + ": " + str(arg.typ) for arg in func.args]) out += offset + f"def {func.name}({args}){render_return(func)}: {func.mutability}\n" out += "\n" return out
def extract_sigs(sig_code): if sig_code["type"] == "vyper": interface_ast = [ i for i in vy_ast.parse_to_ast(sig_code["code"]) if isinstance(i, vy_ast.FunctionDef) or isinstance(i, vy_ast.EventDef) or (isinstance(i, vy_ast.AnnAssign) and i.target.id != "implements") ] global_ctx = GlobalContext.get_global_context(interface_ast) return sig_utils.mk_full_signature(global_ctx, sig_formatter=lambda x: x) elif sig_code["type"] == "json": return mk_full_signature_from_json(sig_code["code"]) else: raise Exception(( f"Unknown interface signature type '{sig_code['type']}' supplied. " "'vyper' & 'json' are supported"))
def mk_full_signature(code, *args, **kwargs): abi = sig_utils.mk_full_signature(parse_to_ast(code), *args, **kwargs) # Add gas estimates for each function to ABI gas_estimates = gas_estimate(code, *args, **kwargs) for func in abi: try: func_signature = func['name'] except KeyError: # constructor and fallback functions don't have a name continue func_name, _, _ = func_signature.partition('(') # This check ensures we skip __init__ since it has no estimate if func_name in gas_estimates: # TODO: mutation func['gas'] = gas_estimates[func_name] return abi
def build_abi_output(compiler_data: CompilerData) -> list: abi = sig_utils.mk_full_signature(compiler_data.global_ctx) # Add gas estimates for each function to ABI gas_estimates = build_gas_estimates(compiler_data.lll_nodes) for func in abi: try: func_signature = func["name"] except KeyError: # constructor and fallback functions don't have a name continue func_name, _, _ = func_signature.partition("(") # This check ensures we skip __init__ since it has no estimate if func_name in gas_estimates: # TODO: mutation func["gas"] = gas_estimates[func_name] return abi
def extract_external_interface(code, contract_name, interface_codes=None): sigs = sig_utils.mk_full_signature( parser.parse_to_ast(code), sig_formatter=lambda x, y: (x, y), interface_codes=interface_codes, ) functions = [sig for sig, _ in sigs if isinstance(sig, FunctionSignature)] cname = Path(contract_name).stem.capitalize() out = "" offset = 4 * " " for idx, func in enumerate(functions): if idx == 0: out += f"\n# External Contracts\ncontract {cname}:\n" if not func.private and func.name != '__init__': args = ", ".join([arg.name + ": " + str(arg.typ) for arg in func.args]) func_type = "constant" if func.const else "modifying" out += offset + f"def {func.name}({args}){render_return(func)}: {func_type}\n" out += "\n" return out