Skip to content

Commit fbece10

Browse files
committed
decode_function_input: test more types
1 parent d21e838 commit fbece10

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

tests/core/contracts/test_contract_method_abi_decoding.py

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
import json
55
import pytest
66

7-
from eth_abi import (
8-
encode_single,
9-
)
10-
117
ABI_A = json.loads('[{"constant":false,"inputs":[],"name":"a","outputs":[],"type":"function"}]')
128
ABI_B = json.loads('[{"constant":false,"inputs":[{"name":"","type":"uint256"}],"name":"a","outputs":[],"type":"function"}]') # noqa: E501
139
ABI_C = json.loads('[{"constant":false,"inputs":[],"name":"a","outputs":[],"type":"function"},{"constant":false,"inputs":[{"name":"","type":"bytes32"}],"name":"a","outputs":[],"type":"function"},{"constant":false,"inputs":[{"name":"","type":"uint256"}],"name":"a","outputs":[],"type":"function"}]') # noqa: E501
1410
ABI_D = json.loads('[{ "constant": false, "inputs": [ { "name": "b", "type": "bytes32[]" } ], "name": "byte_array", "outputs": [], "payable": false, "type": "function" }]') # noqa: E501
15-
A = encode_single('bytes32', b'a')
11+
ABI_BYTES = json.loads('[{"constant":false,"inputs":[{"name":"bytesarg","type":"bytes"}],"name":"bytesfunc","outputs":[],"type":"function"}]') # noqa: E501
12+
ABI_STRING = json.loads('[{"constant":false,"inputs":[{"name":"stringarg","type":"string"}],"name":"stringfunc","outputs":[],"type":"function"}]') # noqa: E501
13+
ABI_ADDRESS = json.loads('[{"constant":false,"inputs":[{"name":"addressarg","type":"address"}],"name":"addressfunc","outputs":[],"type":"function"}]') # noqa: E501
14+
a32bytes = b'a'.ljust(32, b'\x00')
1615

1716

1817
@pytest.mark.parametrize(
@@ -44,21 +43,21 @@
4443
'0x9f3fab586100000000000000000000000000000000000000000000000000000000000000',
4544
'a',
4645
[b'a'],
47-
{'': A},
46+
{'': a32bytes},
4847
),
4948
(
5049
ABI_C,
5150
'0x9f3fab586100000000000000000000000000000000000000000000000000000000000000',
5251
'a',
5352
['0x61'],
54-
{'': A},
53+
{'': a32bytes},
5554
),
5655
(
5756
ABI_C,
5857
'0x9f3fab586100000000000000000000000000000000000000000000000000000000000000',
5958
'a',
6059
['61'],
61-
{'': A},
60+
{'': a32bytes},
6261
),
6362
(
6463
ABI_C,
@@ -67,6 +66,27 @@
6766
[127],
6867
{'': 127},
6968
),
69+
(
70+
ABI_BYTES,
71+
'0xb606a9f6000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000016100000000000000000000000000000000000000000000000000000000000000', # noqa: E501
72+
'bytesfunc',
73+
[],
74+
{'bytesarg': b'a'},
75+
),
76+
(
77+
ABI_STRING,
78+
'0x33b4005f000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000016100000000000000000000000000000000000000000000000000000000000000', # noqa: E501
79+
'stringfunc',
80+
[],
81+
{'stringarg': b'a'}, # When using eth-abi v2, this will return a str instead
82+
),
83+
(
84+
ABI_ADDRESS,
85+
'0x4767be6c000000000000000000000000ffffffffffffffffffffffffffffffffffffffff',
86+
'addressfunc',
87+
[],
88+
{'addressarg': '0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF'},
89+
),
7090
),
7191
)
7292
def test_contract_abi_decoding(web3, abi, data, method, arguments, expected):
@@ -75,6 +95,10 @@ def test_contract_abi_decoding(web3, abi, data, method, arguments, expected):
7595
assert func.fn_name == method
7696
assert params == expected
7797

98+
reinvoke_func = contract.functions[func.fn_name](**params)
99+
rebuild_txn = reinvoke_func.buildTransaction({'gas': 0, 'nonce': 0, 'to': '\x00' * 20})
100+
assert rebuild_txn['data'] == data
101+
78102

79103
@pytest.mark.parametrize(
80104
'abi,method,expected,data',
@@ -97,3 +121,7 @@ def test_contract_abi_encoding_kwargs(web3, abi, method, expected, data):
97121
func, params = contract.decode_function_input(data)
98122
assert func.fn_name == method
99123
assert params == expected
124+
125+
reinvoke_func = contract.functions[func.fn_name](**params)
126+
rebuild_txn = reinvoke_func.buildTransaction({'gas': 0, 'nonce': 0, 'to': '\x00' * 20})
127+
assert rebuild_txn['data'] == data

web3/contract.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1207,7 +1207,7 @@ def buildTransaction(self, transaction=None):
12071207

12081208
if not self.address and 'to' not in built_transaction:
12091209
raise ValueError(
1210-
"When using `ContractFunction.buildTransaction` from a Contract factory"
1210+
"When using `ContractFunction.buildTransaction` from a contract factory "
12111211
"you must provide a `to` address with the transaction"
12121212
)
12131213
if self.address and 'to' in built_transaction:

0 commit comments

Comments
 (0)