forked from ethereum/web3.py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
440 lines (389 loc) · 12 KB
/
utils.py
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
import itertools
from typing import (
TYPE_CHECKING,
Any,
Callable,
List,
Optional,
Sequence,
Tuple,
Type,
Union,
)
from eth_abi.exceptions import (
DecodingError,
)
from eth_typing import (
ChecksumAddress,
)
from hexbytes import (
HexBytes,
)
from web3._utils.abi import (
filter_by_type,
get_abi_output_types,
map_abi_data,
named_tree,
recursive_dict_to_namedtuple,
)
from web3._utils.async_transactions import (
fill_transaction_defaults as async_fill_transaction_defaults,
)
from web3._utils.contracts import (
find_matching_fn_abi,
prepare_transaction,
)
from web3._utils.normalizers import (
BASE_RETURN_NORMALIZERS,
)
from web3._utils.transactions import (
fill_transaction_defaults,
)
from web3.exceptions import (
BadFunctionCallOutput,
)
from web3.types import (
ABI,
ABIFunction,
BlockIdentifier,
CallOverride,
FunctionIdentifier,
TContractFn,
TxParams,
)
if TYPE_CHECKING:
from web3 import ( # noqa: F401
AsyncWeb3,
Web3,
)
ACCEPTABLE_EMPTY_STRINGS = ["0x", b"0x", "", b""]
def call_contract_function(
w3: "Web3",
address: ChecksumAddress,
normalizers: Tuple[Callable[..., Any], ...],
function_identifier: FunctionIdentifier,
transaction: TxParams,
block_id: Optional[BlockIdentifier] = None,
contract_abi: Optional[ABI] = None,
fn_abi: Optional[ABIFunction] = None,
state_override: Optional[CallOverride] = None,
ccip_read_enabled: Optional[bool] = None,
decode_tuples: Optional[bool] = False,
*args: Any,
**kwargs: Any,
) -> Any:
"""
Helper function for interacting with a contract function using the
`eth_call` API.
"""
call_transaction = prepare_transaction(
address,
w3,
fn_identifier=function_identifier,
contract_abi=contract_abi,
fn_abi=fn_abi,
transaction=transaction,
fn_args=args,
fn_kwargs=kwargs,
)
return_data = w3.eth.call(
call_transaction,
block_identifier=block_id,
state_override=state_override,
ccip_read_enabled=ccip_read_enabled,
)
if fn_abi is None:
fn_abi = find_matching_fn_abi(
contract_abi, w3.codec, function_identifier, args, kwargs
)
output_types = get_abi_output_types(fn_abi)
try:
output_data = w3.codec.decode(output_types, return_data)
except DecodingError as e:
# Provide a more helpful error message than the one provided by
# eth-abi-utils
is_missing_code_error = (
return_data in ACCEPTABLE_EMPTY_STRINGS
and w3.eth.get_code(address) in ACCEPTABLE_EMPTY_STRINGS
)
if is_missing_code_error:
msg = (
"Could not transact with/call contract function, is contract "
"deployed correctly and chain synced?"
)
else:
msg = (
f"Could not decode contract function call to {function_identifier} "
f"with return data: {str(return_data)}, output_types: {output_types}"
)
raise BadFunctionCallOutput(msg) from e
_normalizers = itertools.chain(
BASE_RETURN_NORMALIZERS,
normalizers,
)
normalized_data = map_abi_data(_normalizers, output_types, output_data)
if decode_tuples:
decoded = named_tree(fn_abi["outputs"], normalized_data)
normalized_data = recursive_dict_to_namedtuple(decoded)
if len(normalized_data) == 1:
return normalized_data[0]
else:
return normalized_data
def transact_with_contract_function(
address: ChecksumAddress,
w3: "Web3",
function_name: Optional[FunctionIdentifier] = None,
transaction: Optional[TxParams] = None,
contract_abi: Optional[ABI] = None,
fn_abi: Optional[ABIFunction] = None,
*args: Any,
**kwargs: Any,
) -> HexBytes:
"""
Helper function for interacting with a contract function by sending a
transaction.
"""
transact_transaction = prepare_transaction(
address,
w3,
fn_identifier=function_name,
contract_abi=contract_abi,
transaction=transaction,
fn_abi=fn_abi,
fn_args=args,
fn_kwargs=kwargs,
)
txn_hash = w3.eth.send_transaction(transact_transaction)
return txn_hash
def estimate_gas_for_function(
address: ChecksumAddress,
w3: "Web3",
fn_identifier: Optional[FunctionIdentifier] = None,
transaction: Optional[TxParams] = None,
contract_abi: Optional[ABI] = None,
fn_abi: Optional[ABIFunction] = None,
block_identifier: Optional[BlockIdentifier] = None,
*args: Any,
**kwargs: Any,
) -> int:
"""Estimates gas cost a function call would take.
Don't call this directly, instead use :meth:`Contract.estimate_gas`
on your contract instance.
"""
estimate_transaction = prepare_transaction(
address,
w3,
fn_identifier=fn_identifier,
contract_abi=contract_abi,
fn_abi=fn_abi,
transaction=transaction,
fn_args=args,
fn_kwargs=kwargs,
)
return w3.eth.estimate_gas(estimate_transaction, block_identifier)
def build_transaction_for_function(
address: ChecksumAddress,
w3: "Web3",
function_name: Optional[FunctionIdentifier] = None,
transaction: Optional[TxParams] = None,
contract_abi: Optional[ABI] = None,
fn_abi: Optional[ABIFunction] = None,
*args: Any,
**kwargs: Any,
) -> TxParams:
"""Builds a dictionary with the fields required to make the given transaction
Don't call this directly, instead use :meth:`Contract.build_transaction`
on your contract instance.
"""
prepared_transaction = prepare_transaction(
address,
w3,
fn_identifier=function_name,
contract_abi=contract_abi,
fn_abi=fn_abi,
transaction=transaction,
fn_args=args,
fn_kwargs=kwargs,
)
prepared_transaction = fill_transaction_defaults(w3, prepared_transaction)
return prepared_transaction
def find_functions_by_identifier(
contract_abi: ABI,
w3: Union["Web3", "AsyncWeb3"],
address: ChecksumAddress,
callable_check: Callable[..., Any],
function_type: Type[TContractFn],
) -> List[TContractFn]:
fns_abi = filter_by_type("function", contract_abi)
return [
function_type.factory(
fn_abi["name"],
w3=w3,
contract_abi=contract_abi,
address=address,
function_identifier=fn_abi["name"],
abi=fn_abi,
)
for fn_abi in fns_abi
if callable_check(fn_abi)
]
def get_function_by_identifier(
fns: Sequence[TContractFn], identifier: str
) -> TContractFn:
if len(fns) > 1:
raise ValueError(
f"Found multiple functions with matching {identifier}. " f"Found: {fns!r}"
)
elif len(fns) == 0:
raise ValueError(f"Could not find any function with matching {identifier}")
return fns[0]
# --- async --- #
async def async_call_contract_function(
async_w3: "AsyncWeb3",
address: ChecksumAddress,
normalizers: Tuple[Callable[..., Any], ...],
function_identifier: FunctionIdentifier,
transaction: TxParams,
block_id: Optional[BlockIdentifier] = None,
contract_abi: Optional[ABI] = None,
fn_abi: Optional[ABIFunction] = None,
state_override: Optional[CallOverride] = None,
ccip_read_enabled: Optional[bool] = None,
decode_tuples: Optional[bool] = False,
*args: Any,
**kwargs: Any,
) -> Any:
"""
Helper function for interacting with a contract function using the
`eth_call` API.
"""
call_transaction = prepare_transaction(
address,
async_w3,
fn_identifier=function_identifier,
contract_abi=contract_abi,
fn_abi=fn_abi,
transaction=transaction,
fn_args=args,
fn_kwargs=kwargs,
)
return_data = await async_w3.eth.call(
call_transaction,
block_identifier=block_id,
state_override=state_override,
ccip_read_enabled=ccip_read_enabled,
)
if fn_abi is None:
fn_abi = find_matching_fn_abi(
contract_abi, async_w3.codec, function_identifier, args, kwargs
)
output_types = get_abi_output_types(fn_abi)
try:
output_data = async_w3.codec.decode(output_types, return_data)
except DecodingError as e:
# Provide a more helpful error message than the one provided by
# eth-abi-utils
is_missing_code_error = (
return_data in ACCEPTABLE_EMPTY_STRINGS
and await async_w3.eth.get_code(address) in ACCEPTABLE_EMPTY_STRINGS
)
if is_missing_code_error:
msg = (
"Could not transact with/call contract function, is contract "
"deployed correctly and chain synced?"
)
else:
msg = (
f"Could not decode contract function call to {function_identifier} "
f"with return data: {str(return_data)}, output_types: {output_types}"
)
raise BadFunctionCallOutput(msg) from e
_normalizers = itertools.chain(
BASE_RETURN_NORMALIZERS,
normalizers,
)
normalized_data = map_abi_data(_normalizers, output_types, output_data)
if decode_tuples:
decoded = named_tree(fn_abi["outputs"], normalized_data)
normalized_data = recursive_dict_to_namedtuple(decoded)
if len(normalized_data) == 1:
return normalized_data[0]
else:
return normalized_data
async def async_transact_with_contract_function(
address: ChecksumAddress,
async_w3: "AsyncWeb3",
function_name: Optional[FunctionIdentifier] = None,
transaction: Optional[TxParams] = None,
contract_abi: Optional[ABI] = None,
fn_abi: Optional[ABIFunction] = None,
*args: Any,
**kwargs: Any,
) -> HexBytes:
"""
Helper function for interacting with a contract function by sending a
transaction.
"""
transact_transaction = prepare_transaction(
address,
async_w3,
fn_identifier=function_name,
contract_abi=contract_abi,
transaction=transaction,
fn_abi=fn_abi,
fn_args=args,
fn_kwargs=kwargs,
)
txn_hash = await async_w3.eth.send_transaction(transact_transaction)
return txn_hash
async def async_estimate_gas_for_function(
address: ChecksumAddress,
async_w3: "AsyncWeb3",
fn_identifier: Optional[FunctionIdentifier] = None,
transaction: Optional[TxParams] = None,
contract_abi: Optional[ABI] = None,
fn_abi: Optional[ABIFunction] = None,
block_identifier: Optional[BlockIdentifier] = None,
*args: Any,
**kwargs: Any,
) -> int:
"""Estimates gas cost a function call would take.
Don't call this directly, instead use :meth:`Contract.estimate_gas`
on your contract instance.
"""
estimate_transaction = prepare_transaction(
address,
async_w3,
fn_identifier=fn_identifier,
contract_abi=contract_abi,
fn_abi=fn_abi,
transaction=transaction,
fn_args=args,
fn_kwargs=kwargs,
)
return await async_w3.eth.estimate_gas(estimate_transaction, block_identifier)
async def async_build_transaction_for_function(
address: ChecksumAddress,
async_w3: "AsyncWeb3",
function_name: Optional[FunctionIdentifier] = None,
transaction: Optional[TxParams] = None,
contract_abi: Optional[ABI] = None,
fn_abi: Optional[ABIFunction] = None,
*args: Any,
**kwargs: Any,
) -> TxParams:
"""Builds a dictionary with the fields required to make the given transaction
Don't call this directly, instead use :meth:`Contract.build_transaction`
on your contract instance.
"""
prepared_transaction = prepare_transaction(
address,
async_w3,
fn_identifier=function_name,
contract_abi=contract_abi,
fn_abi=fn_abi,
transaction=transaction,
fn_args=args,
fn_kwargs=kwargs,
)
return await async_fill_transaction_defaults(async_w3, prepared_transaction)