forked from FISCO-BCOS/python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontracts.py
More file actions
267 lines (225 loc) · 8.14 KB
/
contracts.py
File metadata and controls
267 lines (225 loc) · 8.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import functools
from utils.function_identifiers import (
FallbackFn,
)
from eth_abi import (
encode_abi as eth_abi_encode_abi,
)
from eth_utils import (
add_0x_prefix,
encode_hex,
function_abi_to_4byte_selector,
is_text,
)
from eth_utils.toolz import (
pipe,
valmap,
)
from hexbytes import (
HexBytes,
)
from utils.abi import (
abi_to_signature,
check_if_arguments_can_be_encoded,
filter_by_argument_count,
filter_by_argument_name,
filter_by_encodability,
filter_by_name,
filter_by_type,
get_abi_input_types,
get_aligned_abi_inputs,
get_fallback_func_abi,
map_abi_data,
merge_args_and_kwargs,
)
from utils.encoding import (
to_hex,
)
from utils.normalizers import (
abi_address_to_hex,
abi_bytes_to_bytes,
abi_string_to_text,
)
from utils.exceptions import (
ValidationError,
)
from utils.abi import get_constructor_abi
def find_matching_event_abi(abi, event_name=None, argument_names=None):
filters = [
functools.partial(filter_by_type, 'event'),
]
if event_name is not None:
filters.append(functools.partial(filter_by_name, event_name))
if argument_names is not None:
filters.append(
functools.partial(filter_by_argument_name, argument_names)
)
event_abi_candidates = pipe(abi, *filters)
if len(event_abi_candidates) == 1:
return event_abi_candidates[0]
elif not event_abi_candidates:
raise ValueError("No matching events found")
else:
raise ValueError("Multiple events found")
def find_matching_fn_abi(abi, fn_identifier=None, args=None, kwargs=None):
args = args or tuple()
kwargs = kwargs or dict()
num_arguments = len(args) + len(kwargs)
#print("len ",len(args))
#print("num_arguments",num_arguments)
if fn_identifier is FallbackFn:
return get_fallback_func_abi(abi)
if not is_text(fn_identifier):
raise TypeError("Unsupported function identifier")
name_filter = functools.partial(filter_by_name, fn_identifier)
arg_count_filter = functools.partial(filter_by_argument_count, num_arguments)
encoding_filter = functools.partial(filter_by_encodability, args, kwargs)
function_candidates = pipe(abi, name_filter, arg_count_filter, encoding_filter)
if len(function_candidates) == 1:
return function_candidates[0]
else:
matching_identifiers = name_filter(abi)
matching_function_signatures = [abi_to_signature(func) for func in matching_identifiers]
#print("matching_function_signatures",matching_function_signatures)
arg_count_matches = len(arg_count_filter(matching_identifiers))
encoding_matches = len(encoding_filter(matching_identifiers))
if arg_count_matches == 0:
diagnosis = "\nFunction invocation failed due to improper number of arguments."
elif encoding_matches == 0:
diagnosis = "\nFunction invocation failed due to no matching argument types."
elif encoding_matches > 1:
diagnosis = (
"\nAmbiguous argument encoding. "
"Provided arguments can be encoded to multiple functions matching this call."
)
message = (
"\nCould not identify the intended function with name `{name}`, "
"positional argument(s) of type `{arg_types}` and "
"keyword argument(s) of type `{kwarg_types}`."
"\nFound {num_candidates} function(s) with the name `{name}`: {candidates}"
"{diagnosis}"
).format(
name=fn_identifier,
arg_types=tuple(map(type, args)),
kwarg_types=valmap(type, kwargs),
num_candidates=len(matching_identifiers),
candidates=matching_function_signatures,
diagnosis=diagnosis,
)
raise ValidationError(message)
def encode_abi(abi, arguments, data=None):
argument_types = get_abi_input_types(abi)
if not check_if_arguments_can_be_encoded(abi, arguments, {}):
raise TypeError(
"One or more arguments could not be encoded to the necessary "
"ABI type. Expected types are: {0}".format(
', '.join(argument_types),
)
)
normalizers = [
abi_address_to_hex,
abi_bytes_to_bytes,
abi_string_to_text,
]
normalized_arguments = map_abi_data(
normalizers,
argument_types,
arguments,
)
#print("argument_types",argument_types)
#print("normalized_arguments",normalized_arguments)
encoded_arguments = eth_abi_encode_abi(
argument_types,
normalized_arguments,
)
#print("encoded_arguments",data,encoded_arguments)
if data:
return to_hex(HexBytes(data) + encoded_arguments)
else:
return encode_hex(encoded_arguments)
def prepare_transaction(
address,
fn_identifier,
contract_abi=None,
fn_abi=None,
transaction=None,
fn_args=None,
fn_kwargs=None):
"""
:parameter `is_function_abi` is used to distinguish function abi from contract abi
Returns a dictionary of the transaction that could be used to call this
TODO: make this a public API
TODO: add new prepare_deploy_transaction API
"""
if fn_abi is None:
fn_abi = find_matching_fn_abi(contract_abi, fn_identifier, fn_args, fn_kwargs)
validate_payable(transaction, fn_abi)
if transaction is None:
prepared_transaction = {}
else:
prepared_transaction = dict(**transaction)
if 'data' in prepared_transaction:
raise ValueError("Transaction parameter may not contain a 'data' key")
if address:
prepared_transaction.setdefault('to', address)
prepared_transaction['data'] = encode_transaction_data(
fn_identifier,
contract_abi,
fn_abi,
fn_args,
fn_kwargs,
)
return prepared_transaction
def encode_transaction_data(
fn_identifier,
contract_abi=None,
fn_abi=None,
args=None,
kwargs=None):
if fn_identifier is FallbackFn:
fn_abi, fn_selector, fn_arguments = get_fallback_function_info(contract_abi, fn_abi)
elif is_text(fn_identifier):
fn_abi, fn_selector, fn_arguments = get_function_info(
fn_identifier, contract_abi, fn_abi, args, kwargs,
)
else:
raise TypeError("Unsupported function identifier")
#print("fn_selector",fn_selector)
return add_0x_prefix(encode_abi(fn_abi, fn_arguments, fn_selector))
def get_aligned_function_data(contract_abi=None, fn_abi=None, args=None, kwargs=None):
if fn_abi is None:
fn_abi = get_constructor_abi(contract_abi)
if kwargs is None:
kwargs = {}
fn_arguments = merge_args_and_kwargs(fn_abi, args, kwargs)
_, aligned_fn_arguments = get_aligned_abi_inputs(fn_abi, fn_arguments)
return add_0x_prefix(encode_abi(fn_abi, fn_arguments, ""))
def get_fallback_function_info(contract_abi=None, fn_abi=None):
if fn_abi is None:
fn_abi = get_fallback_func_abi(contract_abi)
fn_selector = encode_hex(b'')
fn_arguments = tuple()
return fn_abi, fn_selector, fn_arguments
def get_function_info(fn_name, contract_abi=None, fn_abi=None, args=None, kwargs=None):
if args is None:
args = tuple()
if kwargs is None:
kwargs = {}
if fn_abi is None:
fn_abi = find_matching_fn_abi(contract_abi, fn_name, args, kwargs)
fn_selector = encode_hex(function_abi_to_4byte_selector(fn_abi))
fn_arguments = merge_args_and_kwargs(fn_abi, args, kwargs)
_, aligned_fn_arguments = get_aligned_abi_inputs(fn_abi, fn_arguments)
return fn_abi, fn_selector, aligned_fn_arguments
def validate_payable(transaction, abi):
"""Raise ValidationError if non-zero ether
is sent to a non payable function.
"""
if 'value' in transaction:
if transaction['value'] != 0:
if "payable" in abi and not abi["payable"]:
raise ValidationError(
"Sending non-zero ether to a contract function "
"with payable=False. Please ensure that "
"transaction's value is 0."
)