Skip to content

Commit 25954b6

Browse files
committedOct 25, 2019
use eth-utils apply_formatter_to_array
1 parent 27ae247 commit 25954b6

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed
 

‎web3/_utils/validation.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
is_list_like,
1414
is_string,
1515
)
16+
from eth_utils.curried import (
17+
apply_formatter_to_array,
18+
)
1619
from eth_utils.hexadecimal import (
1720
encode_hex,
1821
)
@@ -37,9 +40,6 @@
3740
length_of_array_type,
3841
sub_type_of_array_type,
3942
)
40-
from web3._utils.formatters import (
41-
apply_formatter_to_array,
42-
)
4343
from web3.exceptions import (
4444
InvalidAddress,
4545
)

‎web3/middleware/pythonic.py

+20-13
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
remove_0x_prefix,
1616
text_if_str,
1717
to_checksum_address,
18+
to_list,
1819
)
1920
from eth_utils.toolz import (
2021
complement,
@@ -128,14 +129,18 @@ def to_hexbytes(num_bytes, val, variable_length=False):
128129
whisper_log_formatter = apply_formatters_to_dict(WHISPER_LOG_FORMATTERS)
129130

130131

132+
def apply_list_to_array_formatter(formatter):
133+
return to_list(apply_formatter_to_array(formatter))
134+
135+
131136
LOG_ENTRY_FORMATTERS = {
132137
'blockHash': apply_formatter_if(is_not_null, to_hexbytes(32)),
133138
'blockNumber': apply_formatter_if(is_not_null, to_integer_if_hex),
134139
'transactionIndex': apply_formatter_if(is_not_null, to_integer_if_hex),
135140
'transactionHash': apply_formatter_if(is_not_null, to_hexbytes(32)),
136141
'logIndex': to_integer_if_hex,
137142
'address': to_checksum_address,
138-
'topics': apply_formatter_to_array(to_hexbytes(32)),
143+
'topics': apply_list_to_array_formatter(to_hexbytes(32)),
139144
'data': to_ascii_if_bytes,
140145
}
141146

@@ -152,7 +157,7 @@ def to_hexbytes(num_bytes, val, variable_length=False):
152157
'status': to_integer_if_hex,
153158
'gasUsed': to_integer_if_hex,
154159
'contractAddress': apply_formatter_if(is_not_null, to_checksum_address),
155-
'logs': apply_formatter_to_array(log_entry_formatter),
160+
'logs': apply_list_to_array_formatter(log_entry_formatter),
156161
'logsBloom': to_hexbytes(256),
157162
}
158163

@@ -173,14 +178,14 @@ def to_hexbytes(num_bytes, val, variable_length=False):
173178
'number': apply_formatter_if(is_not_null, to_integer_if_hex),
174179
'parentHash': apply_formatter_if(is_not_null, to_hexbytes(32)),
175180
'sha3Uncles': apply_formatter_if(is_not_null, to_hexbytes(32)),
176-
'uncles': apply_formatter_to_array(to_hexbytes(32)),
181+
'uncles': apply_list_to_array_formatter(to_hexbytes(32)),
177182
'difficulty': to_integer_if_hex,
178183
'receiptsRoot': to_hexbytes(32),
179184
'stateRoot': to_hexbytes(32),
180185
'totalDifficulty': to_integer_if_hex,
181186
'transactions': apply_one_of_formatters((
182-
(is_array_of_dicts, apply_formatter_to_array(transaction_formatter)),
183-
(is_array_of_strings, apply_formatter_to_array(to_hexbytes(32))),
187+
(is_array_of_dicts, apply_list_to_array_formatter(transaction_formatter)),
188+
(is_array_of_strings, apply_list_to_array_formatter(to_hexbytes(32))),
184189
)),
185190
'transactionsRoot': to_hexbytes(32),
186191
}
@@ -192,17 +197,19 @@ def to_hexbytes(num_bytes, val, variable_length=False):
192197
STORAGE_PROOF_FORMATTERS = {
193198
'key': HexBytes,
194199
'value': HexBytes,
195-
'proof': apply_formatter_to_array(HexBytes),
200+
'proof': apply_list_to_array_formatter(HexBytes),
196201
}
197202

198203
ACCOUNT_PROOF_FORMATTERS = {
199204
'address': to_checksum_address,
200-
'accountProof': apply_formatter_to_array(HexBytes),
205+
'accountProof': apply_list_to_array_formatter(HexBytes),
201206
'balance': to_integer_if_hex,
202207
'codeHash': to_hexbytes(32),
203208
'nonce': to_integer_if_hex,
204209
'storageHash': to_hexbytes(32),
205-
'storageProof': apply_formatter_to_array(apply_formatters_to_dict(STORAGE_PROOF_FORMATTERS))
210+
'storageProof': apply_list_to_array_formatter(
211+
apply_formatters_to_dict(STORAGE_PROOF_FORMATTERS)
212+
)
206213
}
207214

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

259266

260267
filter_result_formatter = apply_one_of_formatters((
261-
(is_array_of_dicts, apply_formatter_to_array(log_entry_formatter)),
262-
(is_array_of_strings, apply_formatter_to_array(to_hexbytes(32))),
268+
(is_array_of_dicts, apply_list_to_array_formatter(log_entry_formatter)),
269+
(is_array_of_strings, apply_list_to_array_formatter(to_hexbytes(32))),
263270
))
264271

265272

@@ -328,7 +335,7 @@ def to_hexbytes(num_bytes, val, variable_length=False):
328335
},
329336
result_formatters={
330337
# Eth
331-
'eth_accounts': apply_formatter_to_array(to_checksum_address),
338+
'eth_accounts': apply_list_to_array_formatter(to_checksum_address),
332339
'eth_blockNumber': to_integer_if_hex,
333340
'eth_chainId': to_integer_if_hex,
334341
'eth_coinbase': to_checksum_address,
@@ -374,12 +381,12 @@ def to_hexbytes(num_bytes, val, variable_length=False):
374381
'eth_syncing': apply_formatter_if(is_not_false, syncing_formatter),
375382
# personal
376383
'personal_importRawKey': to_checksum_address,
377-
'personal_listAccounts': apply_formatter_to_array(to_checksum_address),
384+
'personal_listAccounts': apply_list_to_array_formatter(to_checksum_address),
378385
'personal_newAccount': to_checksum_address,
379386
'personal_signTypedData': HexBytes,
380387
'personal_sendTransaction': to_hexbytes(32),
381388
# SHH
382-
'shh_getFilterMessages': apply_formatter_to_array(whisper_log_formatter),
389+
'shh_getFilterMessages': apply_list_to_array_formatter(whisper_log_formatter),
383390
# Transaction Pool
384391
'txpool_content': transaction_pool_content_formatter,
385392
'txpool_inspect': transaction_pool_inspect_formatter,

0 commit comments

Comments
 (0)