Skip to content

Commit

Permalink
Apply PR suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
falvaradorodriguez committed Jun 27, 2024
1 parent 7533955 commit 9a6a789
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 35 deletions.
18 changes: 8 additions & 10 deletions src/safe_cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
49 changes: 24 additions & 25 deletions src/safe_cli/tx_builder/tx_builder_file_decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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}")
Expand Down

0 comments on commit 9a6a789

Please sign in to comment.