iexbase/tron-api-python

How to transfer trc20 token?

matinaci opened this issue · 6 comments

While we can transfer trc20 tokens via extensions but I could not find a code to transfer by python, do you have any idea?

er20 you can send via contract (trigger_contract)

def SendTRC20(from_address, to_address, contract_address, amount, private_key, fee_limit):
from_address_zeros = (64 - len(from_address)) * "0"
_from_address = from_address_zeros + from_address
to_address_zeros = (64 - len(to_address)) * "0"
_to_address = to_address_zeros + to_address
amount = str(hex((amount)1000000)).replace('0x','')
zeros = (64-len(amount))
"0"
amount = zeros + amount
parameter = _to_address + amount
fee_limit = 100000000
data ={"contract_address":contract_address,"function_selector":"transfer(address,uint256)","parameter": parameter,
"fee_limit":fee_limit, "call_value":0,"owner_address":from_address}
data = str(data).replace("""'""",'"')
response = requests.post("https://nile.trongrid.io/wallet/triggersmartcontract", data=data)
res = json.loads(response.content.decode())
transaction = res["transaction"]
private_key = private_key
transaction.pop("visible")
transaction = str(transaction).replace("""'""",'"')
data = {"transaction":transaction,"privateKey":private_key}
response = requests.post("https://nile.trongrid.io/wallet/gettransactionsign", data=str(data))
res = json.loads(response.content.decode())
res.pop("visible")
res = str(res).replace("""'""",'"')
data = res
response = requests.post("https://nile.trongrid.io/wallet/broadcasttransaction", data=str(data))
res = json.loads(response.content.decode())
txid = res["txid"]
return txid

Note : For using this function u have to set address in hexadecimal format.
from_address = string (address which sends the token (example:41E65C21794B63F72076CDCDCBFDFD6570D02FA8C3 ))
to_address = string (address which gives the token)
contract_address = string (token contract address which u use (example : "41c60f4fd695b75c7d6bb3d3a05704ae9ec7f519f7" WIN token))
amount = int (how much u send)
private_key = string (sender private_key)
fee_limit = int

I am using nile testnet so i use this api but in shasta tesnet u have to use https://api.shasta.trongrid.io/wallet/{function_name}

data = {"transaction":transaction,"privateKey":private_key}
response = requests.post("https://nile.trongrid.io/wallet/gettransactionsign", data=str(data))
res = json.loads(response.content.decode())

I keep getting errors in this part.


data = {"transaction": transaction, "privateKey": private_key}
response = requests.post("https://api.trongrid.io/wallet/gettransactionsign", data=str(data))
 res2 = json.loads(response.content.decode())

[err]
     res2 = json.loads(response.content.decode())
 File "/Users/juwonhwang/opt/anaconda3/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/Users/juwonhwang/opt/anaconda3/lib/python3.8/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/Users/juwonhwang/opt/anaconda3/lib/python3.8/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

How can I fix this?

u used the mainnet api but maybe your wallet on testnet? Please check your private key and network then print the response because now u are getting none value

I am trying to transfer the TRC20 tokens on the mainnet to another wallet.

transaction :  {"txID": "d62944f5e0b4cca262a549af5b7282fbb7cad2b13aa6f76c1e821961d59ac711", "raw_data": {"contract": [{"parameter": {"value": {"data": "a9059cbb0000000000000000000000411d3c4c8499df6948d91f637b6fab83d32045c67b000000000000000000000000000000000000000000000030ca024f987b900000", "owner_address": "41d4be2e2fa3f496c537f3dd0417c52bb0e17919fd", "contract_address": "41adf3517f7fd80b91b49ef6ac07cb569b6dc7c93d"}, "type_url": "type.googleapis.com/protocol.TriggerSmartContract"}, "type": "TriggerSmartContract"}], "ref_block_bytes": "876f", "ref_block_hash": "93ee8796295e07cc", "expiration": 1646256948000, "fee_limit": 100000000, "timestamp": 1646256890241}, "raw_data_hex": "0a02876f220893ee8796295e07cc40a086bee4f42f5aae01081f12a9010a31747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e54726967676572536d617274436f6e747261637412740a1541d4be2e2fa3f496c537f3dd0417c52bb0e17919fd121541adf3517f7fd80b91b49ef6ac07cb569b6dc7c93d2244a9059cbb0000000000000000000000411d3c4c8499df6948d91f637b6fab83d32045c67b000000000000000000000000000000000000000000000030ca024f987b9000007081c3bae4f42f900180c2d72f"}

I got this value the first time, but I got an error from the second 'gettransactionsign'.