forked from iexbase/tron-api-python
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcontract.py
More file actions
411 lines (337 loc) · 12.3 KB
/
Copy pathcontract.py
File metadata and controls
411 lines (337 loc) · 12.3 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
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
# --------------------------------------------------------------------
# Copyright (c) iEXBase. All rights reserved.
# Licensed under the MIT License.
# See License.txt in the project root for license information.
# --------------------------------------------------------------------
import copy
from eth_abi import decode
from eth_utils import (
function_abi_to_4byte_selector,
to_hex
)
from hexbytes import HexBytes
from trx_utils import (
encode_hex,
is_text,
deprecated_for,
combomethod
)
from tronapi.common.abi import (
filter_by_type,
merge_args_and_kwargs,
abi_to_signature,
fallback_func_abi_exists,
check_if_arguments_can_be_encoded,
map_abi_data
)
from tronapi.common.contracts import (
find_matching_fn_abi,
encode_abi,
get_function_info,
FallbackFn
)
from tronapi.common.datatypes import PropertyCheckingFactory
from tronapi.common.encoding import to_4byte_hex
from tronapi.common.normalizers import (
normalize_abi,
normalize_bytecode,
BASE_RETURN_NORMALIZERS
)
from tronapi.exceptions import (
NoABIFunctionsFound,
MismatchedABI,
FallbackNotFound
)
class NonExistentFallbackFunction:
@staticmethod
def _raise_exception():
raise FallbackNotFound("No fallback function was found in the contract ABI.")
def __getattr__(self, attr):
return NonExistentFallbackFunction._raise_exception
class ContractFunction:
"""Base class for contract functions"""
address = None
function_identifier = None
tron = None
contract_abi = None
abi = None
transaction = None
arguments = None
def __init__(self, abi=None):
self.abi = abi
self.fn_name = type(self).__name__
def __call__(self, *args, **kwargs):
clone = copy.copy(self)
if args is None:
clone.args = tuple()
else:
clone.args = args
if kwargs is None:
clone.kwargs = {}
else:
clone.kwargs = kwargs
clone._set_function_info()
return clone
def _set_function_info(self):
if not self.abi:
self.abi = find_matching_fn_abi(
self.contract_abi,
self.function_identifier,
self.args,
self.kwargs
)
if self.function_identifier is FallbackFn:
self.selector = encode_hex(b'')
elif is_text(self.function_identifier):
self.selector = encode_hex(function_abi_to_4byte_selector(self.abi))
else:
raise TypeError("Unsupported function identifier")
self.arguments = merge_args_and_kwargs(self.abi, self.args, self.kwargs)
@classmethod
def factory(cls, class_name, **kwargs):
return PropertyCheckingFactory(class_name, (cls,), kwargs)(kwargs.get('abi'))
def __repr__(self):
if self.abi:
_repr = '<Function %s' % abi_to_signature(self.abi)
if self.arguments is not None:
_repr += ' bound to %r' % (self.arguments,)
return _repr + '>'
return '<Function %s>' % self.fn_name
class ContractFunctions:
"""Class containing contract function objects
"""
def __init__(self, abi, tron, address=None):
if abi:
self.abi = abi
self._functions = filter_by_type('function', self.abi)
for func in self._functions:
setattr(
self,
func['name'],
ContractFunction.factory(
func['name'],
tron=tron,
contract_abi=self.abi,
address=address,
function_identifier=func['name']))
def __iter__(self):
if not hasattr(self, '_functions') or not self._functions:
return
for func in self._functions:
yield func['name']
def __getattr__(self, function_name):
if '_functions' not in self.__dict__:
raise NoABIFunctionsFound(
"The abi for this contract contains no function definitions. ",
"Are you sure you provided the correct contract abi?"
)
elif function_name not in self.__dict__['_functions']:
raise MismatchedABI(
"The function '{}' was not found in this contract's abi. ".format(function_name),
"Are you sure you provided the correct contract abi?"
)
else:
return super().__getattribute__(function_name)
def __getitem__(self, function_name):
return getattr(self, function_name)
class Contract:
# set during class construction
tron = None
# instance level properties
address = None
# class properties (overridable at instance level)
abi = None
bytecode = None
bytecode_runtime = None
functions = None
events = None
def __init__(self, address=None):
"""Create a new smart contract proxy object.
:param address: Contract address as 0x hex string
"""
if self.tron is None:
raise AttributeError(
'The `Contract` class has not been initialized. Please use the '
'`tron.contract` interface to create your contract class.'
)
if address:
self.address = self.tron.address.to_hex(address)
if not self.address:
raise TypeError("The address argument is required to instantiate a contract.")
self.functions = ContractFunctions(self.abi, self.tron, self.address)
self.fallback = Contract.get_fallback_function(self.abi, self.tron, self.address)
@classmethod
def factory(cls, tron, class_name=None, **kwargs):
kwargs['tron'] = tron
normalizers = {
'abi': normalize_abi,
'bytecode': normalize_bytecode,
'bytecode_runtime': normalize_bytecode,
}
contract = PropertyCheckingFactory(
class_name or cls.__name__,
(cls,),
kwargs,
normalizers=normalizers
)
setattr(contract, 'functions', ContractFunctions(contract.abi, contract.tron))
setattr(contract, 'fallback', Contract.get_fallback_function(contract.abi, contract.tron))
return contract
@classmethod
@deprecated_for("contract.constructor.transact")
def deploy(cls, **kwargs):
"""Deploy Contract
Deploys a contract.
Returns TransactionExtention, which contains an unsigned transaction.
Example:
.. code-block:: python
>>> MyContract.deploy(
fee_limit=10**9,
call_value=0,
consume_user_resource_percent=10
)
Args:
**kwargs: Transaction parameters for the deployment
transaction as a dict
"""
return cls.tron.transaction_builder.create_smart_contract(
**kwargs,
abi=cls.abi,
bytecode=to_hex(cls.bytecode)
)
@classmethod
def constructor(cls):
if cls.bytecode is None:
raise ValueError(
"Cannot call constructor on a contract that does not have 'bytecode' associated "
"with it"
)
return ContractConstructor(cls.tron,
cls.abi,
cls.bytecode)
@combomethod
def encodeABI(cls, fn_name, args=None, kwargs=None, data=None):
"""Encodes the arguments using the Tron ABI for the contract function
that matches the given name and arguments..
"""
fn_abi, fn_selector, fn_arguments = get_function_info(
fn_name, contract_abi=cls.abi, args=args, kwargs=kwargs,
)
if data is None:
data = fn_selector
return encode_abi(cls.tron, fn_abi, fn_arguments, data)
@staticmethod
def get_fallback_function(abi, tron, address=None):
if abi and fallback_func_abi_exists(abi):
return ContractFunction.factory(
'fallback',
tron=tron,
contract_abi=abi,
address=address,
function_identifier=FallbackFn)()
return NonExistentFallbackFunction()
@combomethod
def all_functions(self):
return find_functions_by_identifier(
self.abi, self.tron, self.address, lambda _: True
)
@combomethod
def get_function_by_signature(self, signature):
if ' ' in signature:
raise ValueError(
'Function signature should not contain any spaces. '
'Found spaces in input: %s' % signature
)
def callable_check(fn_abi):
return abi_to_signature(fn_abi) == signature
fns = find_functions_by_identifier(self.abi, self.tron, self.address, callable_check)
return get_function_by_identifier(fns, 'signature')
@combomethod
def find_functions_by_name(self, fn_name):
def callable_check(fn_abi):
return fn_abi['name'] == fn_name
return find_functions_by_identifier(
self.abi, self.tron, self.address, callable_check
)
@combomethod
def get_function_by_name(self, fn_name):
fns = self.find_functions_by_name(fn_name)
return get_function_by_identifier(fns, 'name')
@combomethod
def get_function_by_selector(self, selector):
def callable_check(fn_abi):
return encode_hex(function_abi_to_4byte_selector(fn_abi)) == to_4byte_hex(selector)
fns = find_functions_by_identifier(self.abi, self.tron, self.address, callable_check)
return get_function_by_identifier(fns, 'selector')
@combomethod
def decode_function_input(self, data):
data = HexBytes(data)
selector, params = data[:4], data[4:]
func = self.get_function_by_selector(selector)
names = [x['name'] for x in func.abi['inputs']]
types = [x['type'] for x in func.abi['inputs']]
decoded = decode(types, params)
normalized = map_abi_data(BASE_RETURN_NORMALIZERS, types, decoded)
return func, dict(zip(names, normalized))
@combomethod
def find_functions_by_args(self, *args):
def callable_check(fn_abi):
return check_if_arguments_can_be_encoded(fn_abi, args=args, kwargs={})
return find_functions_by_identifier(
self.abi, self.tron, self.address, callable_check
)
@combomethod
def get_function_by_args(self, *args):
fns = self.find_functions_by_args(*args)
return get_function_by_identifier(fns, 'args')
class ContractConstructor:
"""
Class for contract constructor API.
"""
def __init__(self, tron, abi, bytecode):
self.tron = tron
self.abi = abi
self.bytecode = bytecode
@staticmethod
def check_forbidden_keys_in_transaction(transaction, forbidden_keys=None):
keys_found = set(transaction.keys()) & set(forbidden_keys)
if keys_found:
raise ValueError("Cannot set {} in transaction".format(', '.join(keys_found)))
@combomethod
def transact(self, **kwargs):
"""Deploy Contract
Deploys a contract.
Returns TransactionExtention, which contains an unsigned transaction.
Args:
**kwargs: Additional options to send
"""
return self.tron.transaction_builder.create_smart_contract(
**kwargs,
abi=self.abi,
bytecode=to_hex(self.bytecode)
)
def find_functions_by_identifier(contract_abi, tron, address, callable_check):
fns_abi = filter_by_type('function', contract_abi)
return [
ContractFunction.factory(
fn_abi['name'],
tron=tron,
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, identifier):
if len(fns) > 1:
raise ValueError(
'Found multiple functions with matching {0}. '
'Found: {1!r}'.format(identifier, fns)
)
elif len(fns) == 0:
raise ValueError(
'Could not find any function with matching {0}'.format(identifier)
)
return fns[0]