Skip to content

Commit 85c1837

Browse files
committed
Add and pass test for data tree normalization
1 parent 91b4e19 commit 85c1837

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

tests/core/utilities/test_abi.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,18 @@ def test_abi_data_tree(types, data, expected):
251251
BASE_RETURN_NORMALIZERS,
252252
[['0x5B2063246F2191f18F2675ceDB8b28102e957458'] * 2],
253253
),
254+
(
255+
["(address,address)[]"],
256+
[[(
257+
'0x5b2063246f2191f18f2675cedb8b28102e957458',
258+
'0xebe0da78ecb266c7ea605dc889c64849f860383f',
259+
)] * 2],
260+
BASE_RETURN_NORMALIZERS,
261+
[[(
262+
'0x5B2063246F2191f18F2675ceDB8b28102e957458',
263+
'0xeBe0DA78ecb266C7EA605DC889c64849F860383F',
264+
)] * 2],
265+
),
254266
],
255267
)
256268
def test_map_abi_data(types, data, funcs, expected):

web3/_utils/abi.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
)
2323
from eth_abi.grammar import (
2424
ABIType,
25+
TupleType,
2526
parse,
2627
)
2728
from eth_abi.registry import (
@@ -669,11 +670,27 @@ def abi_sub_tree(abi_type: Optional[Union[TypeStr, ABIType]], data_value: Any) -
669670
if isinstance(abi_type, TypeStr):
670671
abi_type = parse(abi_type)
671672

673+
# In the two special cases below, we rebuild the given data structures with
674+
# annotated items
672675
if abi_type.is_array:
673-
it = abi_type.item_type
674-
return ABITypedData([abi_type.to_type_str(), [abi_sub_tree(it, i) for i in data_value]])
675-
else:
676-
return ABITypedData([abi_type.to_type_str(), data_value])
676+
# If type is array, determine item type and annotate all
677+
# items in iterable with that type
678+
item_type_str = abi_type.item_type.to_type_str()
679+
data_value = type(data_value)(
680+
abi_sub_tree(item_type_str, i) for i in data_value
681+
)
682+
elif isinstance(abi_type, TupleType):
683+
# Otherwise, if type is tuple, determine component types and annotate
684+
# items in iterable respectively with those types
685+
data_value = type(data_value)(
686+
abi_sub_tree(comp_type.to_type_str(), i)
687+
for comp_type, i in zip(abi_type.components, data_value)
688+
)
689+
690+
return ABITypedData([
691+
abi_type.to_type_str(),
692+
data_value,
693+
])
677694

678695

679696
def strip_abi_type(elements):

0 commit comments

Comments
 (0)