Skip to content

Commit 4739e0f

Browse files
committed
update trigger_contract
1 parent ef647ea commit 4739e0f

File tree

1 file changed

+45
-40
lines changed

1 file changed

+45
-40
lines changed

tronapi/transactionbuilder.py

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ def delete_proposal(self, proposal_id: int, issuer_address: str = None):
400400
'proposal_id': int(proposal_id)
401401
})
402402

403-
def update_account(self, account_name, account: str=None):
403+
def update_account(self, account_name, account: str = None):
404404
"""Modify account name
405405
406406
Note: Username is allowed to edit only once.
@@ -502,49 +502,44 @@ def create_smart_contract(self, **kwargs):
502502
return self.tron.manager.request('/wallet/deploycontract',
503503
transaction)
504504

505-
def trigger_smart_contract(self, contract_address,
506-
function_selector,
507-
fee_limit: int = 1000000000,
508-
call_value: int = 0,
509-
parameters=None,
510-
issuer_address=None
511-
):
512-
"""Trigger Smart Contract (Beta Version)
505+
def trigger_smart_contract(self, **kwargs):
506+
"""Trigger Smart Contract
513507
Calls a function on a contract
514508
515509
Args:
516-
contract_address (str): Contract address, converted to a hex string
517-
function_selector (str): Function signature. No spaces.
518-
fee_limit (int): Maximum TRX consumption, measured in SUN(1TRX = 1,000,000SUN)
519-
call_value (int): Amount of TRX transferred with this transaction, measured in SUN(1TRX = 1,000,000SUN)
520-
parameters (any): Call the virtual machine format of the parameter [1, 2],
521-
use the js tool provided by remix, convert the parameter array [1, 2]
522-
called by the contract caller into
523-
the parameter format required by the virtual machine.
524-
525-
issuer_address (str): address that is trigger the contract
510+
**kwargs: Fill in the required parameters
526511
527512
Examples:
528513
>>> tron = Tron()
529514
>>> tron.transaction_builder.trigger_smart_contract(
530-
>>> '413c8143e98b3e2fe1b1a8fb82b34557505a752390',
531-
>>> 'set(uint256,uint256)',
532-
>>> 30000,
533-
>>> 0,
534-
>>> [
535-
>>> {'type': 'int256', 'value': 1},
536-
>>> {'type': 'int256', 'value': 1}
537-
>>> ])
515+
>>> contract_address='413c8143e98b3e2fe1b1a8fb82b34557505a752390',
516+
>>> function_selector='set(uint256,uint256)',
517+
>>> fee_limit=30000,
518+
>>> call_value=0,
519+
>>> parameters=[
520+
>>> {'type': 'int256', 'value': 1},
521+
>>> {'type': 'int256', 'value': 1}
522+
]
523+
)
538524
539525
Returns:
540526
TransactionExtention, TransactionExtention contains unsigned Transaction
541527
"""
542528

543-
if parameters is None:
544-
parameters = []
529+
contract_address = kwargs.setdefault('contract_address', None)
530+
function_selector = kwargs.setdefault('function_selector', None)
531+
parameters = kwargs.setdefault('parameters', [])
532+
issuer_address = kwargs.setdefault('issuer_address', self.tron.default_address.hex)
533+
call_value = kwargs.setdefault('call_value', 0)
534+
fee_limit = kwargs.setdefault('fee_limit', 1000000000)
535+
token_value = kwargs.setdefault('token_value', 0)
536+
token_id = kwargs.setdefault('token_id', 0)
545537

546-
if issuer_address is None:
547-
issuer_address = self.tron.default_address.hex
538+
if not is_integer(token_value) or token_value < 0:
539+
raise ValueError('Invalid options.tokenValue provided')
540+
541+
if not is_integer(token_id) or token_id < 0:
542+
raise ValueError('Invalid options.tokenId provided')
548543

549544
if not self.tron.isAddress(contract_address):
550545
raise InvalidAddress('Invalid contract address provided')
@@ -558,6 +553,8 @@ def trigger_smart_contract(self, contract_address,
558553
if not is_integer(fee_limit) or fee_limit <= 0 or fee_limit > 1000000000:
559554
raise ValueError('Invalid fee limit provided')
560555

556+
function_selector = function_selector.replace('/\s*/g', '')
557+
561558
if len(parameters) > 0:
562559
types = []
563560
values = []
@@ -579,20 +576,28 @@ def trigger_smart_contract(self, contract_address,
579576
else:
580577
parameters = ''
581578

582-
return self.tron.manager.request('/wallet/triggersmartcontract', {
579+
data = {
583580
'contract_address': self.tron.address.to_hex(contract_address),
584581
'owner_address': self.tron.address.to_hex(issuer_address),
585582
'function_selector': function_selector,
586583
'fee_limit': int(fee_limit),
587584
'call_value': int(call_value),
588585
'parameter': parameters
589-
})
586+
}
587+
588+
if token_value:
589+
data.call_token_value = int(token_value)
590+
591+
if token_id:
592+
data.token_id = int(token_id)
593+
594+
return self.tron.manager.request('/wallet/triggersmartcontract', data)
590595

591596
def create_trx_exchange(self,
592597
token_name: str,
593598
token_balance: int,
594599
trx_balance: int,
595-
account: str=None):
600+
account: str = None):
596601
"""Create an exchange between a token and TRX.
597602
Token Name should be a CASE SENSITIVE string.
598603
Note: PLEASE VERIFY THIS ON TRONSCAN.
@@ -627,7 +632,7 @@ def create_token_exchange(self,
627632
first_token_balance: int,
628633
second_token_name: str,
629634
second_token_balance: int,
630-
owner_address: str=None):
635+
owner_address: str = None):
631636
"""Create an exchange between a token and another token.
632637
DO NOT USE THIS FOR TRX.
633638
Token Names should be a CASE SENSITIVE string.
@@ -661,7 +666,7 @@ def inject_exchange_tokens(self,
661666
exchange_id: int,
662667
token_name: str,
663668
token_amount: int = 0,
664-
owner_address: str=None):
669+
owner_address: str = None):
665670
"""Adds tokens into a bancor style exchange.
666671
Will add both tokens at market rate.
667672
@@ -830,7 +835,7 @@ def withdraw_exchange_tokens(self,
830835
exchange_id: int,
831836
token_name: str,
832837
token_amount: int = 0,
833-
owner_address: str=None):
838+
owner_address: str = None):
834839
"""Withdraws tokens from a bancor style exchange.
835840
Will withdraw at market rate both tokens.
836841
@@ -865,7 +870,7 @@ def trade_exchange_tokens(self,
865870
token_name: str,
866871
token_amount_sold: int = 0,
867872
token_amount_expected: int = 0,
868-
owner_address: str=None):
873+
owner_address: str = None):
869874
"""Trade tokens on a bancor style exchange.
870875
Expected value is a validation and used to cap the total amt of token 2 spent.
871876
@@ -904,7 +909,7 @@ def trade_exchange_tokens(self,
904909
def update_setting(self,
905910
contract_address,
906911
user_fee_percentage,
907-
owner_address: str=None):
912+
owner_address: str = None):
908913
"""Update userFeePercentage.
909914
910915
Args:
@@ -938,7 +943,7 @@ def update_setting(self,
938943
def update_energy_limit(self,
939944
contract_address,
940945
origin_energy_limit,
941-
owner_address: str=None):
946+
owner_address: str = None):
942947
"""Update energy limit.
943948
944949
Args:

0 commit comments

Comments
 (0)