Skip to content

Commit a7610b7

Browse files
committed
update contract
1 parent 0e06e21 commit a7610b7

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

tronapi/base/contracts.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,23 @@
66

77
import functools
88

9-
from eth_utils import to_hex
109
from hexbytes import HexBytes
1110

11+
from eth_utils import (
12+
to_hex,
13+
function_abi_to_4byte_selector
14+
)
1215
from tronapi.base.abi import (
1316
filter_by_name,
1417
filter_by_encodability,
1518
filter_by_argument_count,
1619
get_fallback_func_abi,
17-
abi_to_signature, get_abi_input_types, check_if_arguments_can_be_encoded, map_abi_data)
20+
abi_to_signature,
21+
get_abi_input_types,
22+
check_if_arguments_can_be_encoded,
23+
map_abi_data,
24+
merge_args_and_kwargs
25+
)
1826

1927
from tronapi.base.function_identifiers import FallbackFn
2028
from tronapi.base.normalizers import abi_address_to_hex, abi_bytes_to_bytes, abi_string_to_text
@@ -126,3 +134,19 @@ def encode_abi(tron, abi, arguments, data=None):
126134
return to_hex(HexBytes(data) + encoded_arguments)
127135
else:
128136
return encode_hex(encoded_arguments)
137+
138+
139+
def get_function_info(fn_name, contract_abi=None, fn_abi=None, args=None, kwargs=None):
140+
if args is None:
141+
args = tuple()
142+
if kwargs is None:
143+
kwargs = {}
144+
145+
if fn_abi is None:
146+
fn_abi = find_matching_fn_abi(contract_abi, fn_name, args, kwargs)
147+
148+
fn_selector = encode_hex(function_abi_to_4byte_selector(fn_abi))
149+
150+
fn_arguments = merge_args_and_kwargs(fn_abi, args, kwargs)
151+
152+
return fn_abi, fn_selector, fn_arguments

tronapi/contract.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
fallback_func_abi_exists,
1616
check_if_arguments_can_be_encoded
1717
)
18-
from tronapi.base.contracts import find_matching_fn_abi
18+
from tronapi.base.contracts import find_matching_fn_abi, encode_abi, get_function_info
1919
from tronapi.base.datatypes import PropertyCheckingFactory
2020
from tronapi.base.decorators import (
2121
combomethod,
@@ -241,6 +241,20 @@ def constructor(cls):
241241
cls.abi,
242242
cls.bytecode)
243243

244+
@combomethod
245+
def encodeABI(cls, fn_name, args=None, kwargs=None, data=None):
246+
"""Encodes the arguments using the Tron ABI for the contract function
247+
that matches the given name and arguments..
248+
"""
249+
fn_abi, fn_selector, fn_arguments = get_function_info(
250+
fn_name, contract_abi=cls.abi, args=args, kwargs=kwargs,
251+
)
252+
253+
if data is None:
254+
data = fn_selector
255+
256+
return encode_abi(cls.tron, fn_abi, fn_arguments, data)
257+
244258
@staticmethod
245259
def get_fallback_function(abi, tron, address=None):
246260
if abi and fallback_func_abi_exists(abi):

0 commit comments

Comments
 (0)