From d8322c2a3983536dd4e04c0c8f5d5fadbeb8528d Mon Sep 17 00:00:00 2001 From: yhtiyar Date: Tue, 9 Nov 2021 12:37:50 +0300 Subject: [PATCH] fix no constructor --- moonworm/cli.py.template | 3 ++- moonworm/generator.py | 18 ++++++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/moonworm/cli.py.template b/moonworm/cli.py.template index 1912bc8..c13a50a 100644 --- a/moonworm/cli.py.template +++ b/moonworm/cli.py.template @@ -23,7 +23,8 @@ for abi_item in CONTRACT_ABI: CONTRACT_FUNCTIONS[abi_item["name"]] = abi_item if abi_item["type"] == "constructor": CONTRACT_FUNCTIONS["constructor"] = abi_item - +if CONTRACT_FUNCTIONS["constructor"] is None: + CONTRACT_FUNCTIONS["constructor"] = {{"inputs" : []}} def init_web3(ipc_path: str) -> Web3: return Web3(web3.HTTPProvider(ipc_path)) diff --git a/moonworm/generator.py b/moonworm/generator.py index b28b0af..c2c0985 100644 --- a/moonworm/generator.py +++ b/moonworm/generator.py @@ -96,7 +96,14 @@ def generate_contract_class( ] ), ) - contract_constructor = [c for c in abi if c["type"] == "constructor"][0] + contract_constructor = [c for c in abi if c["type"] == "constructor"] + if len(contract_constructor) == 1: + contract_constructor = contract_constructor[0] + elif len(contract_constructor) == 0: + contract_constructor = {"inputs": []} + else: + raise ValueError("Multiple constructors found in ABI") + contract_constructor["name"] = "constructor" class_functions = ( [class_constructor] @@ -261,7 +268,14 @@ def generate_argument_parser_function(abi: List[Dict[str, Any]]) -> cst.Function subparsers.extend(generate_function_subparser(function_abi, "description")) # Deploy argparser: - contract_constructor = [item for item in abi if item["type"] == "constructor"][0] + contract_constructor = [item for item in abi if item["type"] == "constructor"] + if len(contract_constructor) == 1: + contract_constructor = contract_constructor[0] + elif len(contract_constructor) == 0: + contract_constructor = {"inputs": []} + else: + raise Exception("Multiple constructors found") + deploy_argument_parsers = [] default_arg_counter = 1 for arg in contract_constructor["inputs"]: