44# --------------------------------------------------------------------------------------------
55
66from datetime import datetime , timedelta
7- from typing import Dict
7+ from typing import Dict , Any , Tuple , List
88
99from tronapi .exceptions import InvalidTronError , TronError , InvalidAddress
1010from tronapi .utils .help import is_valid_url
@@ -216,7 +216,7 @@ def apply_for_sr(self, url, address):
216216 'url' : self .tron .toHex (text = url )
217217 })
218218
219- def vote (self , votes : Dict [ str , int ], voter_address : str = None ):
219+ def vote (self , votes : List [ Tuple [ str , int ] ], voter_address : str = None ):
220220 """Vote
221221 Vote on the super representative
222222
@@ -226,11 +226,9 @@ def vote(self, votes: Dict[str, int], voter_address: str = None):
226226
227227 Examples:
228228 >>> from tronapi import Tron
229- >>>
230229 >>> data = [
231230 >>> ('TRJpw2uqohP7FUmAEJgt57wakRn6aGQU6Z', 1)
232231 >>> ]
233- >>>
234232 >>> tron = Tron()
235233 >>> tron.transaction.vote(data)
236234
@@ -262,6 +260,36 @@ def vote(self, votes: Dict[str, int], voter_address: str = None):
262260 'votes' : _view_vote
263261 })
264262
263+ def create_proposal (self , parameters : Any , issuer_address = None ):
264+ """Creates a proposal to modify the network.
265+ Can only be created by a current Super Representative.
266+
267+ Args:
268+ parameters (Any): proposal parameters
269+ issuer_address: owner address
270+
271+ Examples:
272+ >>> from tronapi import Tron
273+ >>> data = [
274+ >>> {'key': 1, 'value': 2},
275+ >>> {'key': 1, 'value': 2}
276+ >>> ]
277+ >>> tron = Tron()
278+ >>> tron.transaction.create_proposal(data)
279+
280+
281+ """
282+ if issuer_address is None :
283+ issuer_address = self .tron .default_address .hex
284+
285+ if not self .tron .isAddress (issuer_address ):
286+ raise InvalidAddress ('Invalid issuerAddress provided' )
287+
288+ return self .tron .manager .request ('/wallet/proposalcreate' , {
289+ 'owner_address' : self .tron .address .to_hex (issuer_address ),
290+ 'parameters' : parameters
291+ })
292+
265293 def vote_proposal (self , proposal_id , has_approval , voter_address ):
266294 """Proposal approval
267295
@@ -353,9 +381,6 @@ def create_trx_exchange(self,
353381 if not self .tron .isAddress (account ):
354382 raise TronError ('Invalid address provided' )
355383
356- if not len (token_name ):
357- raise TronError ('Invalid tokenName provided' )
358-
359384 if token_balance <= 0 or trx_balance <= 0 :
360385 raise TronError ('Invalid amount provided' )
361386
@@ -367,6 +392,78 @@ def create_trx_exchange(self,
367392 'second_token_balance' : trx_balance
368393 })
369394
395+ def create_token_exchange (self ,
396+ first_token_name : str ,
397+ first_token_balance : int ,
398+ second_token_name : str ,
399+ second_token_balance : int ,
400+ owner_address = None ):
401+ """Create an exchange between a token and another token.
402+ DO NOT USE THIS FOR TRX.
403+ Token Names should be a CASE SENSITIVE string.
404+
405+ Args:
406+ first_token_name (str): the id of the first token
407+ first_token_balance (int): balance of the first token
408+ second_token_name (str): the id of the second token
409+ second_token_balance (int): balance of the second token
410+ owner_address: owner address
411+
412+ """
413+ if owner_address is None :
414+ owner_address = self .tron .default_address .hex
415+
416+ if not self .tron .isAddress (owner_address ):
417+ raise InvalidAddress ('Invalid address provided' )
418+
419+ if second_token_balance <= 0 or first_token_balance <= 0 :
420+ raise ValueError ('Invalid amount provided' )
421+
422+ return self .tron .manager .request ('/wallet/exchangecreate' , {
423+ 'owner_address' : self .tron .address .to_hex (owner_address ),
424+ 'first_token_id' : self .tron .toHex (text = first_token_name ),
425+ 'first_token_balance' : first_token_balance ,
426+ 'second_token_id' : self .tron .toHex (text = second_token_name ),
427+ 'second_token_balance' : second_token_balance
428+ })
429+
430+ def inject_exchange_tokens (self ,
431+ exchange_id : int ,
432+ token_name : str ,
433+ token_amount : int = 0 ,
434+ owner_address = None
435+ ):
436+ """Adds tokens into a bancor style exchange.
437+ Will add both tokens at market rate.
438+
439+ Args:
440+ exchange_id (int): non-negative integer exchange id
441+ token_name (str): token name
442+ token_amount (int): amount of token
443+ owner_address (str): token owner address in hex
444+
445+ Returns:
446+
447+ """
448+ if owner_address is None :
449+ owner_address = self .tron .default_address .hex
450+
451+ if not self .tron .isAddress (owner_address ):
452+ raise InvalidAddress ('Invalid owner_address provided' )
453+
454+ if exchange_id < 0 :
455+ raise ValueError ('Invalid exchange_id provided' )
456+
457+ if token_amount < 1 :
458+ raise ValueError ('Invalid token_amount provided' )
459+
460+ return self .tron .manager .request ('/wallet/exchangeinject' , {
461+ 'owner_address' : self .tron .address .to_hex (owner_address ),
462+ 'exchange_id' : exchange_id ,
463+ 'token_id' : self .tron .toHex (text = token_name ),
464+ 'quant' : token_amount
465+ })
466+
370467 def create_token (self , ** kwargs ):
371468 """Issue Token
372469
@@ -496,3 +593,78 @@ def create_token(self, **kwargs):
496593 })
497594
498595 return response
596+
597+ def withdraw_exchange_tokens (self ,
598+ exchange_id : int ,
599+ token_name : str ,
600+ token_amount : int = 0 ,
601+ owner_address = None ):
602+ """Withdraws tokens from a bancor style exchange.
603+ Will withdraw at market rate both tokens.
604+
605+ Args:
606+ exchange_id (int): non-negative integer exchange id
607+ token_name (str): token name
608+ token_amount (int): number of tokens withdraw
609+ owner_address (str): owner address in hex
610+
611+ """
612+ if owner_address is None :
613+ owner_address = self .tron .default_address .hex
614+
615+ if not self .tron .isAddress (owner_address ):
616+ raise InvalidAddress ('Invalid owner_address provided' )
617+
618+ if exchange_id < 0 :
619+ raise ValueError ('Invalid exchange_id provided' )
620+
621+ if token_amount < 1 :
622+ raise ValueError ('Invalid token_amount provided' )
623+
624+ return self .tron .manager .request ('/wallet/exchangewithdraw' , {
625+ 'owner_address' : self .tron .address .to_hex (owner_address ),
626+ 'exchange_id' : exchange_id ,
627+ 'token_id' : self .tron .toHex (text = token_name ),
628+ 'quant' : token_amount
629+ })
630+
631+ def trade_exchange_tokens (self ,
632+ exchange_id : int ,
633+ token_name : str ,
634+ token_amount_sold : int = 0 ,
635+ token_amount_expected : int = 0 ,
636+ owner_address = None ):
637+ """Trade tokens on a bancor style exchange.
638+ Expected value is a validation and used to cap the total amt of token 2 spent.
639+
640+ Args:
641+ exchange_id (int): non-negative integer exchange id
642+ token_name (str): token name
643+ token_amount_sold (int): amount f token actually sold
644+ token_amount_expected (int): amount of token expected
645+ owner_address (str): token owner address in hex
646+
647+ """
648+
649+ if owner_address is None :
650+ owner_address = self .tron .default_address .hex
651+
652+ if not self .tron .isAddress (owner_address ):
653+ raise InvalidAddress ('Invalid owner_address provided' )
654+
655+ if exchange_id < 0 :
656+ raise ValueError ('Invalid exchange_id provided' )
657+
658+ if token_amount_sold < 1 :
659+ raise ValueError ('Invalid token_amount_sold provided' )
660+
661+ if token_amount_expected < 1 :
662+ raise ValueError ('Invalid token_amount_expected provided' )
663+
664+ return self .tron .manager .request ('/wallet/exchangewithdraw' , {
665+ 'owner_address' : self .tron .address .to_hex (owner_address ),
666+ 'exchange_id' : exchange_id ,
667+ 'token_id' : self .tron .toHex (text = token_name ),
668+ 'quant' : token_amount_sold ,
669+ 'expected' : token_amount_expected
670+ })
0 commit comments