diff --git a/web3/_utils/formatters.py b/web3/_utils/formatters.py index d04880db9b..c820ca4cfb 100644 --- a/web3/_utils/formatters.py +++ b/web3/_utils/formatters.py @@ -8,7 +8,9 @@ is_list_like, is_string, to_dict, - to_list, +) +from eth_utils.curried import ( + apply_formatter_at_index, ) from eth_utils.toolz import ( compose, @@ -28,21 +30,6 @@ def hex_to_integer(value): integer_to_hex = hex -@curry -@to_list -def apply_formatter_at_index(formatter, at_index, value): - if at_index + 1 > len(value): - raise IndexError( - "Not enough values in iterable to apply formatter. Got: {0}. " - "Need: {1}".format(len(value), at_index + 1) - ) - for index, item in enumerate(value): - if index == at_index: - yield formatter(item) - else: - yield item - - def apply_formatters_to_args(*formatters): return compose(*( apply_formatter_at_index(formatter, index) @@ -51,15 +38,6 @@ def apply_formatters_to_args(*formatters): )) -@curry -def apply_one_of_formatters(formatter_condition_pairs, value): - for formatter, condition in formatter_condition_pairs: - if condition(value): - return formatter(value) - else: - raise ValueError("The provided value did not satisfy any of the formatter conditions") - - def map_collection(func, collection): """ Apply func to each element of a collection, or value of a dictionary. diff --git a/web3/middleware/pythonic.py b/web3/middleware/pythonic.py index d9cb90b1dc..5489a9eec8 100644 --- a/web3/middleware/pythonic.py +++ b/web3/middleware/pythonic.py @@ -1,15 +1,13 @@ import codecs import operator -from eth_utils import ( - apply_one_of_formatters, -) from eth_utils.curried import ( apply_formatter_at_index, apply_formatter_if, apply_formatter_to_array, apply_formatters_to_dict, apply_formatters_to_sequence, + apply_one_of_formatters, is_address, is_bytes, is_integer, @@ -38,7 +36,6 @@ to_hex, ) from web3._utils.formatters import ( - apply_one_of_formatters, hex_to_integer, integer_to_hex, is_array_of_dicts, @@ -182,8 +179,8 @@ def to_hexbytes(num_bytes, val, variable_length=False): 'stateRoot': to_hexbytes(32), 'totalDifficulty': to_integer_if_hex, 'transactions': apply_one_of_formatters(( - (apply_formatter_to_array(transaction_formatter), is_array_of_dicts), - (apply_formatter_to_array(to_hexbytes(32)), is_array_of_strings), + (is_array_of_dicts, apply_formatter_to_array(transaction_formatter)), + (is_array_of_strings, apply_formatter_to_array(to_hexbytes(32))), )), 'transactionsRoot': to_hexbytes(32), } @@ -261,8 +258,8 @@ def to_hexbytes(num_bytes, val, variable_length=False): filter_result_formatter = apply_one_of_formatters(( - (apply_formatter_to_array(log_entry_formatter), is_array_of_dicts), - (apply_formatter_to_array(to_hexbytes(32)), is_array_of_strings), + (is_array_of_dicts, apply_formatter_to_array(log_entry_formatter)), + (is_array_of_strings, apply_formatter_to_array(to_hexbytes(32))), )) @@ -308,8 +305,8 @@ def to_hexbytes(num_bytes, val, variable_length=False): block_number_formatter, ]), 'eth_estimateGas': apply_one_of_formatters(( - (estimate_gas_without_block_id, is_length(1)), - (estimate_gas_with_block_id, is_length(2)), + (is_length(1), estimate_gas_without_block_id), + (is_length(2), estimate_gas_with_block_id), )), 'eth_sendTransaction': apply_formatter_at_index(transaction_param_formatter, 0), # personal