Skip to content

Commit

Permalink
decode_function_input: test more types
Browse files Browse the repository at this point in the history
  • Loading branch information
carver committed Aug 7, 2018
1 parent d21e838 commit fbece10
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
44 changes: 36 additions & 8 deletions tests/core/contracts/test_contract_method_abi_decoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
import json
import pytest

from eth_abi import (
encode_single,
)

ABI_A = json.loads('[{"constant":false,"inputs":[],"name":"a","outputs":[],"type":"function"}]')
ABI_B = json.loads('[{"constant":false,"inputs":[{"name":"","type":"uint256"}],"name":"a","outputs":[],"type":"function"}]') # noqa: E501
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
ABI_D = json.loads('[{ "constant": false, "inputs": [ { "name": "b", "type": "bytes32[]" } ], "name": "byte_array", "outputs": [], "payable": false, "type": "function" }]') # noqa: E501
A = encode_single('bytes32', b'a')
ABI_BYTES = json.loads('[{"constant":false,"inputs":[{"name":"bytesarg","type":"bytes"}],"name":"bytesfunc","outputs":[],"type":"function"}]') # noqa: E501
ABI_STRING = json.loads('[{"constant":false,"inputs":[{"name":"stringarg","type":"string"}],"name":"stringfunc","outputs":[],"type":"function"}]') # noqa: E501
ABI_ADDRESS = json.loads('[{"constant":false,"inputs":[{"name":"addressarg","type":"address"}],"name":"addressfunc","outputs":[],"type":"function"}]') # noqa: E501
a32bytes = b'a'.ljust(32, b'\x00')


@pytest.mark.parametrize(
Expand Down Expand Up @@ -44,21 +43,21 @@
'0x9f3fab586100000000000000000000000000000000000000000000000000000000000000',
'a',
[b'a'],
{'': A},
{'': a32bytes},
),
(
ABI_C,
'0x9f3fab586100000000000000000000000000000000000000000000000000000000000000',
'a',
['0x61'],
{'': A},
{'': a32bytes},
),
(
ABI_C,
'0x9f3fab586100000000000000000000000000000000000000000000000000000000000000',
'a',
['61'],
{'': A},
{'': a32bytes},
),
(
ABI_C,
Expand All @@ -67,6 +66,27 @@
[127],
{'': 127},
),
(
ABI_BYTES,
'0xb606a9f6000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000016100000000000000000000000000000000000000000000000000000000000000', # noqa: E501
'bytesfunc',
[],
{'bytesarg': b'a'},
),
(
ABI_STRING,
'0x33b4005f000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000016100000000000000000000000000000000000000000000000000000000000000', # noqa: E501
'stringfunc',
[],
{'stringarg': b'a'}, # When using eth-abi v2, this will return a str instead
),
(
ABI_ADDRESS,
'0x4767be6c000000000000000000000000ffffffffffffffffffffffffffffffffffffffff',
'addressfunc',
[],
{'addressarg': '0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF'},
),
),
)
def test_contract_abi_decoding(web3, abi, data, method, arguments, expected):
Expand All @@ -75,6 +95,10 @@ def test_contract_abi_decoding(web3, abi, data, method, arguments, expected):
assert func.fn_name == method
assert params == expected

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


@pytest.mark.parametrize(
'abi,method,expected,data',
Expand All @@ -97,3 +121,7 @@ def test_contract_abi_encoding_kwargs(web3, abi, method, expected, data):
func, params = contract.decode_function_input(data)
assert func.fn_name == method
assert params == expected

reinvoke_func = contract.functions[func.fn_name](**params)
rebuild_txn = reinvoke_func.buildTransaction({'gas': 0, 'nonce': 0, 'to': '\x00' * 20})
assert rebuild_txn['data'] == data
2 changes: 1 addition & 1 deletion web3/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -1207,7 +1207,7 @@ def buildTransaction(self, transaction=None):

if not self.address and 'to' not in built_transaction:
raise ValueError(
"When using `ContractFunction.buildTransaction` from a Contract factory"
"When using `ContractFunction.buildTransaction` from a contract factory "
"you must provide a `to` address with the transaction"
)
if self.address and 'to' in built_transaction:
Expand Down

0 comments on commit fbece10

Please sign in to comment.