44import  json 
55import  pytest 
66
7- from  eth_abi  import  (
8-     encode_single ,
9- )
10- 
117ABI_A  =  json .loads ('[{"constant":false,"inputs":[],"name":"a","outputs":[],"type":"function"}]' )
128ABI_B  =  json .loads ('[{"constant":false,"inputs":[{"name":"","type":"uint256"}],"name":"a","outputs":[],"type":"function"}]' )  # noqa: E501 
139ABI_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 
1410ABI_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 ( 
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 , 
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) 
7292def  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 
0 commit comments