32
32
is_0x_prefixed ,
33
33
is_address ,
34
34
is_bytes ,
35
- is_dict ,
36
35
is_integer ,
37
36
is_null ,
38
37
is_string ,
88
87
)
89
88
from web3 .datastructures import (
90
89
AttributeDict ,
90
+ ReadableAttributeDict ,
91
91
)
92
92
from web3 .exceptions import (
93
93
BlockNotFound ,
@@ -155,6 +155,19 @@ def is_attrdict(val: Any) -> bool:
155
155
not_attrdict = complement (is_attrdict )
156
156
157
157
158
+ @curry
159
+ def type_aware_apply_formatters_to_dict (
160
+ formatters : Formatters ,
161
+ value : Union [AttributeDict [str , Any ], Dict [str , Any ]],
162
+ ) -> Union [ReadableAttributeDict [str , Any ], Dict [str , Any ]]:
163
+ formatted_dict : Dict [str , Any ] = apply_formatters_to_dict (formatters , dict (value ))
164
+ return (
165
+ AttributeDict .recursive (formatted_dict )
166
+ if is_attrdict (value )
167
+ else formatted_dict
168
+ )
169
+
170
+
158
171
TRANSACTION_RESULT_FORMATTERS = {
159
172
"blockHash" : apply_formatter_if (is_not_null , to_hexbytes (32 )),
160
173
"blockNumber" : apply_formatter_if (is_not_null , to_integer_if_hex ),
@@ -179,7 +192,9 @@ def is_attrdict(val: Any) -> bool:
179
192
}
180
193
181
194
182
- transaction_result_formatter = apply_formatters_to_dict (TRANSACTION_RESULT_FORMATTERS )
195
+ transaction_result_formatter = type_aware_apply_formatters_to_dict (
196
+ TRANSACTION_RESULT_FORMATTERS
197
+ )
183
198
184
199
185
200
def apply_list_to_array_formatter (formatter : Any ) -> Callable [..., Any ]:
@@ -198,7 +213,7 @@ def apply_list_to_array_formatter(formatter: Any) -> Callable[..., Any]:
198
213
}
199
214
200
215
201
- log_entry_formatter = apply_formatters_to_dict (LOG_ENTRY_FORMATTERS )
216
+ log_entry_formatter = type_aware_apply_formatters_to_dict (LOG_ENTRY_FORMATTERS )
202
217
203
218
204
219
RECEIPT_FORMATTERS = {
@@ -219,7 +234,7 @@ def apply_list_to_array_formatter(formatter: Any) -> Callable[..., Any]:
219
234
}
220
235
221
236
222
- receipt_formatter = apply_formatters_to_dict (RECEIPT_FORMATTERS )
237
+ receipt_formatter = type_aware_apply_formatters_to_dict (RECEIPT_FORMATTERS )
223
238
224
239
BLOCK_FORMATTERS = {
225
240
"baseFeePerGas" : to_integer_if_hex ,
@@ -256,7 +271,7 @@ def apply_list_to_array_formatter(formatter: Any) -> Callable[..., Any]:
256
271
}
257
272
258
273
259
- block_formatter = apply_formatters_to_dict (BLOCK_FORMATTERS )
274
+ block_formatter = type_aware_apply_formatters_to_dict (BLOCK_FORMATTERS )
260
275
261
276
262
277
SYNCING_FORMATTERS = {
@@ -268,7 +283,7 @@ def apply_list_to_array_formatter(formatter: Any) -> Callable[..., Any]:
268
283
}
269
284
270
285
271
- syncing_formatter = apply_formatters_to_dict (SYNCING_FORMATTERS )
286
+ syncing_formatter = type_aware_apply_formatters_to_dict (SYNCING_FORMATTERS )
272
287
273
288
274
289
TRANSACTION_POOL_CONTENT_FORMATTERS = {
@@ -283,7 +298,7 @@ def apply_list_to_array_formatter(formatter: Any) -> Callable[..., Any]:
283
298
}
284
299
285
300
286
- transaction_pool_content_formatter = apply_formatters_to_dict (
301
+ transaction_pool_content_formatter = type_aware_apply_formatters_to_dict (
287
302
TRANSACTION_POOL_CONTENT_FORMATTERS
288
303
)
289
304
@@ -294,7 +309,7 @@ def apply_list_to_array_formatter(formatter: Any) -> Callable[..., Any]:
294
309
}
295
310
296
311
297
- transaction_pool_inspect_formatter = apply_formatters_to_dict (
312
+ transaction_pool_inspect_formatter = type_aware_apply_formatters_to_dict (
298
313
TRANSACTION_POOL_INSPECT_FORMATTERS
299
314
)
300
315
@@ -308,7 +323,7 @@ def apply_list_to_array_formatter(formatter: Any) -> Callable[..., Any]:
308
323
),
309
324
}
310
325
311
- fee_history_formatter = apply_formatters_to_dict (FEE_HISTORY_FORMATTERS )
326
+ fee_history_formatter = type_aware_apply_formatters_to_dict (FEE_HISTORY_FORMATTERS )
312
327
313
328
STORAGE_PROOF_FORMATTERS = {
314
329
"key" : HexBytes ,
@@ -324,19 +339,19 @@ def apply_list_to_array_formatter(formatter: Any) -> Callable[..., Any]:
324
339
"nonce" : to_integer_if_hex ,
325
340
"storageHash" : to_hexbytes (32 ),
326
341
"storageProof" : apply_list_to_array_formatter (
327
- apply_formatters_to_dict (STORAGE_PROOF_FORMATTERS )
342
+ type_aware_apply_formatters_to_dict (STORAGE_PROOF_FORMATTERS )
328
343
),
329
344
}
330
345
331
- proof_formatter = apply_formatters_to_dict (ACCOUNT_PROOF_FORMATTERS )
346
+ proof_formatter = type_aware_apply_formatters_to_dict (ACCOUNT_PROOF_FORMATTERS )
332
347
333
348
FILTER_PARAMS_FORMATTERS = {
334
349
"fromBlock" : apply_formatter_if (is_integer , integer_to_hex ),
335
350
"toBlock" : apply_formatter_if (is_integer , integer_to_hex ),
336
351
}
337
352
338
353
339
- filter_params_formatter = apply_formatters_to_dict (FILTER_PARAMS_FORMATTERS )
354
+ filter_params_formatter = type_aware_apply_formatters_to_dict (FILTER_PARAMS_FORMATTERS )
340
355
341
356
342
357
filter_result_formatter = apply_one_of_formatters (
@@ -351,7 +366,9 @@ def apply_list_to_array_formatter(formatter: Any) -> Callable[..., Any]:
351
366
"maxPriorityFeePerGas" : to_hex_if_integer ,
352
367
}
353
368
354
- transaction_request_formatter = apply_formatters_to_dict (TRANSACTION_REQUEST_FORMATTERS )
369
+ transaction_request_formatter = type_aware_apply_formatters_to_dict (
370
+ TRANSACTION_REQUEST_FORMATTERS
371
+ )
355
372
transaction_param_formatter = compose (
356
373
remove_key_if ("to" , lambda txn : txn ["to" ] in {"" , b"" , None }),
357
374
remove_key_if ("gasPrice" , lambda txn : txn ["gasPrice" ] in {"" , b"" , None }),
@@ -398,22 +415,22 @@ def apply_list_to_array_formatter(formatter: Any) -> Callable[..., Any]:
398
415
"tx" : transaction_result_formatter ,
399
416
}
400
417
401
- signed_tx_formatter = apply_formatters_to_dict (SIGNED_TX_FORMATTER )
418
+ signed_tx_formatter = type_aware_apply_formatters_to_dict (SIGNED_TX_FORMATTER )
402
419
403
- FILTER_PARAM_NORMALIZERS = apply_formatters_to_dict (
420
+ FILTER_PARAM_NORMALIZERS = type_aware_apply_formatters_to_dict (
404
421
{"address" : apply_formatter_if (is_string , lambda x : [x ])}
405
422
)
406
423
407
424
408
425
GETH_WALLET_FORMATTER = {"address" : to_checksum_address }
409
426
410
- geth_wallet_formatter = apply_formatters_to_dict (GETH_WALLET_FORMATTER )
427
+ geth_wallet_formatter = type_aware_apply_formatters_to_dict (GETH_WALLET_FORMATTER )
411
428
412
429
GETH_WALLETS_FORMATTER = {
413
430
"accounts" : apply_list_to_array_formatter (geth_wallet_formatter ),
414
431
}
415
432
416
- geth_wallets_formatter = apply_formatters_to_dict (GETH_WALLETS_FORMATTER )
433
+ geth_wallets_formatter = type_aware_apply_formatters_to_dict (GETH_WALLETS_FORMATTER )
417
434
418
435
419
436
PYTHONIC_REQUEST_FORMATTERS : Dict [RPCEndpoint , Callable [..., Any ]] = {
@@ -556,10 +573,6 @@ def apply_list_to_array_formatter(formatter: Any) -> Callable[..., Any]:
556
573
RPC .net_peerCount : to_integer_if_hex ,
557
574
}
558
575
559
- ATTRDICT_FORMATTER = {
560
- "*" : apply_formatter_if (is_dict and not_attrdict , AttributeDict .recursive )
561
- }
562
-
563
576
METHOD_NORMALIZERS : Dict [RPCEndpoint , Callable [..., Any ]] = {
564
577
RPC .eth_getLogs : apply_formatter_at_index (FILTER_PARAM_NORMALIZERS , 0 ),
565
578
RPC .eth_newFilter : apply_formatter_at_index (FILTER_PARAM_NORMALIZERS , 0 ),
@@ -820,10 +833,7 @@ def get_result_formatters(
820
833
partial_formatters = apply_module_to_formatters (
821
834
formatters_requiring_module , module , method_name
822
835
)
823
- attrdict_formatter = apply_formatter_if (
824
- is_dict and not_attrdict , AttributeDict .recursive
825
- )
826
- return compose (* partial_formatters , attrdict_formatter , * formatters )
836
+ return compose (* partial_formatters , * formatters )
827
837
828
838
829
839
def get_error_formatters (
0 commit comments