From 9a6a789f9a2e7c9241533faccf6b9834315a3e30 Mon Sep 17 00:00:00 2001 From: Felipe Alvarado Date: Thu, 27 Jun 2024 14:14:24 +0200 Subject: [PATCH] Apply PR suggestions --- src/safe_cli/main.py | 18 +++---- .../tx_builder/tx_builder_file_decoder.py | 49 +++++++++---------- 2 files changed, 32 insertions(+), 35 deletions(-) diff --git a/src/safe_cli/main.py b/src/safe_cli/main.py index 90eafd98..ee8b9b2e 100644 --- a/src/safe_cli/main.py +++ b/src/safe_cli/main.py @@ -290,22 +290,20 @@ def tx_builder( safe_address, node_url, private_key, interactive ) data = json.loads(file_path.read_text()) - safe_txs = [] - for tx in convert_to_proposed_transactions(data): - safe_txs.append( - safe_operator.prepare_safe_transaction(tx.to, tx.value, tx.data) - ) + safe_txs = [ + safe_operator.prepare_safe_transaction(tx.to, tx.value, tx.data) + for tx in convert_to_proposed_transactions(data) + ] if len(safe_txs) == 0: raise typer.BadParameter("No transactions found.") if len(safe_txs) == 1: safe_operator.execute_safe_transaction(safe_txs[0]) - return - - multisend_tx = safe_operator.batch_safe_txs(safe_operator.get_nonce(), safe_txs) - if multisend_tx is not None: - safe_operator.execute_safe_transaction(multisend_tx) + else: + multisend_tx = safe_operator.batch_safe_txs(safe_operator.get_nonce(), safe_txs) + if multisend_tx is not None: + safe_operator.execute_safe_transaction(multisend_tx) @app.command() diff --git a/src/safe_cli/tx_builder/tx_builder_file_decoder.py b/src/safe_cli/tx_builder/tx_builder_file_decoder.py index 84c841c1..12190b41 100644 --- a/src/safe_cli/tx_builder/tx_builder_file_decoder.py +++ b/src/safe_cli/tx_builder/tx_builder_file_decoder.py @@ -7,7 +7,7 @@ from hexbytes import HexBytes from web3 import Web3 -from safe_cli.tx_builder.exceptions import ( +from .exceptions import ( InvalidContratMethodError, SoliditySyntaxError, TxBuilderEncodingError, @@ -38,33 +38,34 @@ def encode_contract_method_to_hex_data( contract_fields = contract_method.get("inputs", []) if contract_method else [] is_valid_contract_method = ( - contract_method_name and contract_method_name not in NON_VALID_CONTRACT_METHODS + contract_method_name is not None + and contract_method_name not in NON_VALID_CONTRACT_METHODS ) - if is_valid_contract_method: - try: - encoding_types = _parse_types_to_encoding_types(contract_fields) - values = [ - parse_input_value( - field["type"], contract_fields_values.get(field["name"], "") - ) - for field in contract_fields - ] - - function_signature = f"{contract_method_name}({','.join(encoding_types)})" - function_selector = Web3.keccak(text=function_signature)[:4] - encoded_parameters = encode_abi(encoding_types, values) - hex_encoded_data = HexBytes(function_selector + encoded_parameters) - return hex_encoded_data - except Exception as error: - raise TxBuilderEncodingError( - "Error encoding current form values to hex data:", error - ) - else: + if not is_valid_contract_method: raise InvalidContratMethodError( f"Invalid contract method {contract_method_name}" ) + try: + encoding_types = _parse_types_to_encoding_types(contract_fields) + values = [ + parse_input_value( + field["type"], contract_fields_values.get(field["name"], "") + ) + for field in contract_fields + ] + + function_signature = f"{contract_method_name}({','.join(encoding_types)})" + function_selector = Web3.keccak(text=function_signature)[:4] + encoded_parameters = encode_abi(encoding_types, values) + hex_encoded_data = HexBytes(function_selector + encoded_parameters) + return hex_encoded_data + except Exception as error: + raise TxBuilderEncodingError( + "Error encoding current form values to hex data:", error + ) + def parse_boolean_value(value: str) -> bool: if isinstance(value, str): @@ -124,9 +125,7 @@ def _get_base_field_type(field_type: str) -> str: if not trimmed_value: raise SoliditySyntaxError("Empty base field type for") - base_field_type_regex = re.compile( - r"^([a-zA-Z0-9]*)(((\[])|(\[[1-9]+[0-9]*]))*)?$" - ) + base_field_type_regex = re.compile(r"^([a-zA-Z0-9]*)(((\[])|(\[[1-9]+[0-9]*]))*)?$") match = base_field_type_regex.match(trimmed_value) if not match: raise SoliditySyntaxError(f"Unknown base field type from {trimmed_value}")