4
4
import json
5
5
import pytest
6
6
7
- from eth_abi import (
8
- encode_single ,
9
- )
10
-
11
7
ABI_A = json .loads ('[{"constant":false,"inputs":[],"name":"a","outputs":[],"type":"function"}]' )
12
8
ABI_B = json .loads ('[{"constant":false,"inputs":[{"name":"","type":"uint256"}],"name":"a","outputs":[],"type":"function"}]' ) # noqa: E501
13
9
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
14
10
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 ' )
16
15
17
16
18
17
@pytest .mark .parametrize (
44
43
'0x9f3fab586100000000000000000000000000000000000000000000000000000000000000' ,
45
44
'a' ,
46
45
[b'a' ],
47
- {'' : A },
46
+ {'' : a32bytes },
48
47
),
49
48
(
50
49
ABI_C ,
51
50
'0x9f3fab586100000000000000000000000000000000000000000000000000000000000000' ,
52
51
'a' ,
53
52
['0x61' ],
54
- {'' : A },
53
+ {'' : a32bytes },
55
54
),
56
55
(
57
56
ABI_C ,
58
57
'0x9f3fab586100000000000000000000000000000000000000000000000000000000000000' ,
59
58
'a' ,
60
59
['61' ],
61
- {'' : A },
60
+ {'' : a32bytes },
62
61
),
63
62
(
64
63
ABI_C ,
67
66
[127 ],
68
67
{'' : 127 },
69
68
),
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
+ ),
70
90
),
71
91
)
72
92
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):
75
95
assert func .fn_name == method
76
96
assert params == expected
77
97
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
+
78
102
79
103
@pytest .mark .parametrize (
80
104
'abi,method,expected,data' ,
@@ -97,3 +121,7 @@ def test_contract_abi_encoding_kwargs(web3, abi, method, expected, data):
97
121
func , params = contract .decode_function_input (data )
98
122
assert func .fn_name == method
99
123
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