From fbece10834ae059ff353cf5321172b1879708b6a Mon Sep 17 00:00:00 2001 From: Jason Carver Date: Tue, 7 Aug 2018 12:37:41 -0700 Subject: [PATCH] decode_function_input: test more types --- .../test_contract_method_abi_decoding.py | 44 +++++++++++++++---- web3/contract.py | 2 +- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/tests/core/contracts/test_contract_method_abi_decoding.py b/tests/core/contracts/test_contract_method_abi_decoding.py index d705213e29..9aa832ba58 100644 --- a/tests/core/contracts/test_contract_method_abi_decoding.py +++ b/tests/core/contracts/test_contract_method_abi_decoding.py @@ -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( @@ -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, @@ -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): @@ -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', @@ -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 diff --git a/web3/contract.py b/web3/contract.py index 38e7c122fb..8136b9e2f9 100644 --- a/web3/contract.py +++ b/web3/contract.py @@ -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: