Skip to content

Commit

Permalink
use eth-utils apply_formatter_to_array
Browse files Browse the repository at this point in the history
  • Loading branch information
kclowes committed Oct 25, 2019
1 parent 27ae247 commit 25954b6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
6 changes: 3 additions & 3 deletions web3/_utils/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
is_list_like,
is_string,
)
from eth_utils.curried import (
apply_formatter_to_array,
)
from eth_utils.hexadecimal import (
encode_hex,
)
Expand All @@ -37,9 +40,6 @@
length_of_array_type,
sub_type_of_array_type,
)
from web3._utils.formatters import (
apply_formatter_to_array,
)
from web3.exceptions import (
InvalidAddress,
)
Expand Down
33 changes: 20 additions & 13 deletions web3/middleware/pythonic.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
remove_0x_prefix,
text_if_str,
to_checksum_address,
to_list,
)
from eth_utils.toolz import (
complement,
Expand Down Expand Up @@ -128,14 +129,18 @@ def to_hexbytes(num_bytes, val, variable_length=False):
whisper_log_formatter = apply_formatters_to_dict(WHISPER_LOG_FORMATTERS)


def apply_list_to_array_formatter(formatter):
return to_list(apply_formatter_to_array(formatter))


LOG_ENTRY_FORMATTERS = {
'blockHash': apply_formatter_if(is_not_null, to_hexbytes(32)),
'blockNumber': apply_formatter_if(is_not_null, to_integer_if_hex),
'transactionIndex': apply_formatter_if(is_not_null, to_integer_if_hex),
'transactionHash': apply_formatter_if(is_not_null, to_hexbytes(32)),
'logIndex': to_integer_if_hex,
'address': to_checksum_address,
'topics': apply_formatter_to_array(to_hexbytes(32)),
'topics': apply_list_to_array_formatter(to_hexbytes(32)),
'data': to_ascii_if_bytes,
}

Expand All @@ -152,7 +157,7 @@ def to_hexbytes(num_bytes, val, variable_length=False):
'status': to_integer_if_hex,
'gasUsed': to_integer_if_hex,
'contractAddress': apply_formatter_if(is_not_null, to_checksum_address),
'logs': apply_formatter_to_array(log_entry_formatter),
'logs': apply_list_to_array_formatter(log_entry_formatter),
'logsBloom': to_hexbytes(256),
}

Expand All @@ -173,14 +178,14 @@ def to_hexbytes(num_bytes, val, variable_length=False):
'number': apply_formatter_if(is_not_null, to_integer_if_hex),
'parentHash': apply_formatter_if(is_not_null, to_hexbytes(32)),
'sha3Uncles': apply_formatter_if(is_not_null, to_hexbytes(32)),
'uncles': apply_formatter_to_array(to_hexbytes(32)),
'uncles': apply_list_to_array_formatter(to_hexbytes(32)),
'difficulty': to_integer_if_hex,
'receiptsRoot': to_hexbytes(32),
'stateRoot': to_hexbytes(32),
'totalDifficulty': to_integer_if_hex,
'transactions': apply_one_of_formatters((
(is_array_of_dicts, apply_formatter_to_array(transaction_formatter)),
(is_array_of_strings, apply_formatter_to_array(to_hexbytes(32))),
(is_array_of_dicts, apply_list_to_array_formatter(transaction_formatter)),
(is_array_of_strings, apply_list_to_array_formatter(to_hexbytes(32))),
)),
'transactionsRoot': to_hexbytes(32),
}
Expand All @@ -192,17 +197,19 @@ def to_hexbytes(num_bytes, val, variable_length=False):
STORAGE_PROOF_FORMATTERS = {
'key': HexBytes,
'value': HexBytes,
'proof': apply_formatter_to_array(HexBytes),
'proof': apply_list_to_array_formatter(HexBytes),
}

ACCOUNT_PROOF_FORMATTERS = {
'address': to_checksum_address,
'accountProof': apply_formatter_to_array(HexBytes),
'accountProof': apply_list_to_array_formatter(HexBytes),
'balance': to_integer_if_hex,
'codeHash': to_hexbytes(32),
'nonce': to_integer_if_hex,
'storageHash': to_hexbytes(32),
'storageProof': apply_formatter_to_array(apply_formatters_to_dict(STORAGE_PROOF_FORMATTERS))
'storageProof': apply_list_to_array_formatter(
apply_formatters_to_dict(STORAGE_PROOF_FORMATTERS)
)
}

proof_formatter = apply_formatters_to_dict(ACCOUNT_PROOF_FORMATTERS)
Expand Down Expand Up @@ -258,8 +265,8 @@ def to_hexbytes(num_bytes, val, variable_length=False):


filter_result_formatter = apply_one_of_formatters((
(is_array_of_dicts, apply_formatter_to_array(log_entry_formatter)),
(is_array_of_strings, apply_formatter_to_array(to_hexbytes(32))),
(is_array_of_dicts, apply_list_to_array_formatter(log_entry_formatter)),
(is_array_of_strings, apply_list_to_array_formatter(to_hexbytes(32))),
))


Expand Down Expand Up @@ -328,7 +335,7 @@ def to_hexbytes(num_bytes, val, variable_length=False):
},
result_formatters={
# Eth
'eth_accounts': apply_formatter_to_array(to_checksum_address),
'eth_accounts': apply_list_to_array_formatter(to_checksum_address),
'eth_blockNumber': to_integer_if_hex,
'eth_chainId': to_integer_if_hex,
'eth_coinbase': to_checksum_address,
Expand Down Expand Up @@ -374,12 +381,12 @@ def to_hexbytes(num_bytes, val, variable_length=False):
'eth_syncing': apply_formatter_if(is_not_false, syncing_formatter),
# personal
'personal_importRawKey': to_checksum_address,
'personal_listAccounts': apply_formatter_to_array(to_checksum_address),
'personal_listAccounts': apply_list_to_array_formatter(to_checksum_address),
'personal_newAccount': to_checksum_address,
'personal_signTypedData': HexBytes,
'personal_sendTransaction': to_hexbytes(32),
# SHH
'shh_getFilterMessages': apply_formatter_to_array(whisper_log_formatter),
'shh_getFilterMessages': apply_list_to_array_formatter(whisper_log_formatter),
# Transaction Pool
'txpool_content': transaction_pool_content_formatter,
'txpool_inspect': transaction_pool_inspect_formatter,
Expand Down

0 comments on commit 25954b6

Please sign in to comment.