|
| 1 | +from binascii import ( |
| 2 | + unhexlify, |
| 3 | +) |
| 4 | +import json |
| 5 | +import pytest |
| 6 | + |
| 7 | +ABI_A = json.loads('[{"constant":false,"inputs":[],"name":"noargfunc","outputs":[],"type":"function"}]') # noqa: E501 |
| 8 | +ABI_B = json.loads('[{"constant":false,"inputs":[{"name":"uintarg","type":"uint256"}],"name":"uintfunc","outputs":[],"type":"function"}]') # noqa: E501 |
| 9 | +ABI_C = json.loads('[{"constant":false,"inputs":[],"name":"namesakefunc","outputs":[],"type":"function"},{"constant":false,"inputs":[{"name":"bytesarg","type":"bytes32"}],"name":"namesakefunc","outputs":[],"type":"function"},{"constant":false,"inputs":[{"name":"uintarg","type":"uint256"}],"name":"namesakefunc","outputs":[],"type":"function"}]') # noqa: E501 |
| 10 | +ABI_D = json.loads('[{ "constant": false, "inputs": [ { "name": "b", "type": "bytes32[]" } ], "name": "byte_array", "outputs": [], "payable": false, "type": "function" }]') # noqa: E501 |
| 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') |
| 15 | + |
| 16 | + |
| 17 | +@pytest.mark.parametrize( |
| 18 | + 'abi,data,method,expected', |
| 19 | + ( |
| 20 | + ( |
| 21 | + ABI_A, |
| 22 | + '0xc4c1a40b', |
| 23 | + 'noargfunc', |
| 24 | + {}, |
| 25 | + ), |
| 26 | + ( |
| 27 | + ABI_B, |
| 28 | + '0xcc6820de0000000000000000000000000000000000000000000000000000000000000001', |
| 29 | + 'uintfunc', |
| 30 | + {'uintarg': 1}, |
| 31 | + ), |
| 32 | + ( |
| 33 | + ABI_C, |
| 34 | + '0x22d86fa3', |
| 35 | + 'namesakefunc', |
| 36 | + {}, |
| 37 | + ), |
| 38 | + ( |
| 39 | + ABI_C, |
| 40 | + '0x40c05b2f0000000000000000000000000000000000000000000000000000000000000001', |
| 41 | + 'namesakefunc', |
| 42 | + {'uintarg': 1}, |
| 43 | + ), |
| 44 | + ( |
| 45 | + ABI_C, |
| 46 | + '0xf931d77c6100000000000000000000000000000000000000000000000000000000000000', |
| 47 | + 'namesakefunc', |
| 48 | + {'bytesarg': a32bytes}, |
| 49 | + ), |
| 50 | + ( |
| 51 | + ABI_BYTES, |
| 52 | + '0xb606a9f6000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000016100000000000000000000000000000000000000000000000000000000000000', # noqa: E501 |
| 53 | + 'bytesfunc', |
| 54 | + {'bytesarg': b'a'}, |
| 55 | + ), |
| 56 | + ( |
| 57 | + ABI_STRING, |
| 58 | + '0x33b4005f000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000016100000000000000000000000000000000000000000000000000000000000000', # noqa: E501 |
| 59 | + 'stringfunc', |
| 60 | + {'stringarg': 'a'}, |
| 61 | + ), |
| 62 | + ( |
| 63 | + ABI_ADDRESS, |
| 64 | + '0x4767be6c000000000000000000000000ffffffffffffffffffffffffffffffffffffffff', |
| 65 | + 'addressfunc', |
| 66 | + {'addressarg': '0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF'}, |
| 67 | + ), |
| 68 | + ), |
| 69 | +) |
| 70 | +def test_contract_abi_decoding(web3, abi, data, method, expected): |
| 71 | + contract = web3.eth.contract(abi=abi) |
| 72 | + func, params = contract.decode_function_input(data) |
| 73 | + assert func.fn_name == method |
| 74 | + assert params == expected |
| 75 | + |
| 76 | + reinvoke_func = contract.functions[func.fn_name](**params) |
| 77 | + rebuild_txn = reinvoke_func.buildTransaction({'gas': 0, 'nonce': 0, 'to': '\x00' * 20}) |
| 78 | + assert rebuild_txn['data'] == data |
| 79 | + |
| 80 | + |
| 81 | +@pytest.mark.parametrize( |
| 82 | + 'abi,method,expected,data', |
| 83 | + ( |
| 84 | + ( |
| 85 | + ABI_D, |
| 86 | + 'byte_array', |
| 87 | + { |
| 88 | + 'b': [ |
| 89 | + unhexlify('5595c210956e7721f9b692e702708556aa9aabb14ea163e96afa56ffbe9fa809'), |
| 90 | + unhexlify('6f8d2fa18448afbfe4f82143c384484ad09a0271f3a3c0eb9f629e703f883125'), |
| 91 | + ], |
| 92 | + }, |
| 93 | + '0xf166d6f8000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000025595c210956e7721f9b692e702708556aa9aabb14ea163e96afa56ffbe9fa8096f8d2fa18448afbfe4f82143c384484ad09a0271f3a3c0eb9f629e703f883125', # noqa: E501 |
| 94 | + ), |
| 95 | + ), |
| 96 | +) |
| 97 | +def test_contract_abi_encoding_kwargs(web3, abi, method, expected, data): |
| 98 | + contract = web3.eth.contract(abi=abi) |
| 99 | + func, params = contract.decode_function_input(data) |
| 100 | + assert func.fn_name == method |
| 101 | + assert params == expected |
| 102 | + |
| 103 | + reinvoke_func = contract.functions[func.fn_name](**params) |
| 104 | + rebuild_txn = reinvoke_func.buildTransaction({'gas': 0, 'nonce': 0, 'to': '\x00' * 20}) |
| 105 | + assert rebuild_txn['data'] == data |
0 commit comments