How to Send the USDT (TRC-20) Transacton using Python tronpy?
To send a USDT (Tether) transaction using TronPy, you can follow the steps outlined below:
Install the necessary packages: Ensure you have TronPy and its dependencies installed in your Python environment. You can use pip to install them:
1 | pip install tronpy |
pip install tronpy
Import the required modules: Import the necessary modules from TronPy to interact with the Tron blockchain and the USDT contract (see Function to Return the USDT/USDD/USDC Contract Address on Tron Blockchain (Main Net, Nile, Shasta)).
1 2 3 | from tronpy import Tron from tronpy.keys import PrivateKey from tronpy.providers import HTTPProvider |
from tronpy import Tron from tronpy.keys import PrivateKey from tronpy.providers import HTTPProvider
Connect to the Tron network: Create a connection to a Tron network using a provider. In this example, we’ll use the Tron mainnet.
1 2 3 4 | full_node = HTTPProvider("https://api.trongrid.io") solidity_node = HTTPProvider("https://api.trongrid.io") event_server = HTTPProvider("https://api.trongrid.io") tron = Tron(full_node=full_node, solidity_node=solidity_node, event_server=event_server) |
full_node = HTTPProvider("https://api.trongrid.io") solidity_node = HTTPProvider("https://api.trongrid.io") event_server = HTTPProvider("https://api.trongrid.io") tron = Tron(full_node=full_node, solidity_node=solidity_node, event_server=event_server)
Load your private key: Load the private key associated with the address you want to send the USDT transaction from. Make sure you have some TRX balance in that address to cover the transaction fees.
1 2 | private_key = PrivateKey(bytes.fromhex("YOUR_PRIVATE_KEY")) address = private_key.public_key.to_base58check_address() |
private_key = PrivateKey(bytes.fromhex("YOUR_PRIVATE_KEY")) address = private_key.public_key.to_base58check_address()
Create the transaction data: Build the transaction data for the USDT transfer. You need to specify the recipient’s address, the amount of USDT to send (in decimal format), and the contract address of the USDT token.
1 2 3 4 5 6 7 8 9 | token_address = "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t" # USDT contract address on Tron mainnet recipient_address = "ADDRESS_OF_RECIPIENT" amount = 1000 # Amount of USDT to send (in decimal format) amount_in_wei = int(amount * 10 ** 6) # Convert to USDT's decimal precision (6 decimals) token_contract = tron.get_contract(token_address) transaction = token_contract.functions.transfer(recipient_address, amount_in_wei).build_transaction( owner_address=address ) |
token_address = "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t" # USDT contract address on Tron mainnet recipient_address = "ADDRESS_OF_RECIPIENT" amount = 1000 # Amount of USDT to send (in decimal format) amount_in_wei = int(amount * 10 ** 6) # Convert to USDT's decimal precision (6 decimals) token_contract = tron.get_contract(token_address) transaction = token_contract.functions.transfer(recipient_address, amount_in_wei).build_transaction( owner_address=address )
Sign and broadcast the transaction: Sign the transaction using your private key and broadcast it to the Tron network.
1 2 3 | signed_txn = tron.trx.sign(transaction, private_key) response = tron.trx.broadcast(signed_txn) print(response) |
signed_txn = tron.trx.sign(transaction, private_key) response = tron.trx.broadcast(signed_txn) print(response)
That’s it! The USDT transaction should now be sent to the Tron network. Make sure to replace “YOUR_PRIVATE_KEY” with your actual private key, “ADDRESS_OF_RECIPIENT” with the recipient’s address, and verify the USDT contract address if you’re using a different network. Remember to have enough TRX balance in your account to cover the transaction fees.
The complete Python source code to send USDT (TRC-20) on Tron Blockchain is given as the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | from tronpy import Tron from tronpy.keys import PrivateKey from tronpy.providers import HTTPProvider full_node = HTTPProvider("https://api.trongrid.io") solidity_node = HTTPProvider("https://api.trongrid.io") event_server = HTTPProvider("https://api.trongrid.io") tron = Tron(full_node=full_node, solidity_node=solidity_node, event_server=event_server) private_key = PrivateKey(bytes.fromhex("YOUR_PRIVATE_KEY")) address = private_key.public_key.to_base58check_address() token_address = "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t" # USDT contract address on Tron mainnet recipient_address = "ADDRESS_OF_RECIPIENT" amount = 1000 # Amount of USDT to send (in decimal format) amount_in_wei = int(amount * 10 ** 6) # Convert to USDT's decimal precision (6 decimals) token_contract = tron.get_contract(token_address) transaction = token_contract.functions.transfer(recipient_address, amount_in_wei).build_transaction( owner_address=address ) signed_txn = tron.trx.sign(transaction, private_key) response = tron.trx.broadcast(signed_txn) print(response) |
from tronpy import Tron from tronpy.keys import PrivateKey from tronpy.providers import HTTPProvider full_node = HTTPProvider("https://api.trongrid.io") solidity_node = HTTPProvider("https://api.trongrid.io") event_server = HTTPProvider("https://api.trongrid.io") tron = Tron(full_node=full_node, solidity_node=solidity_node, event_server=event_server) private_key = PrivateKey(bytes.fromhex("YOUR_PRIVATE_KEY")) address = private_key.public_key.to_base58check_address() token_address = "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t" # USDT contract address on Tron mainnet recipient_address = "ADDRESS_OF_RECIPIENT" amount = 1000 # Amount of USDT to send (in decimal format) amount_in_wei = int(amount * 10 ** 6) # Convert to USDT's decimal precision (6 decimals) token_contract = tron.get_contract(token_address) transaction = token_contract.functions.transfer(recipient_address, amount_in_wei).build_transaction( owner_address=address ) signed_txn = tron.trx.sign(transaction, private_key) response = tron.trx.broadcast(signed_txn) print(response)
Here is a more clear way:
1 2 3 4 5 6 7 8 | tx = ( self._contract.functions.transfer( recipient_address, amount * 10 ** 6) .with_owner(self._address) .build() .sign(self._private_key) ) |
tx = ( self._contract.functions.transfer( recipient_address, amount * 10 ** 6) .with_owner(self._address) .build() .sign(self._private_key) )
Tron Blockchain
- Passive Income: Staking TRX on Tron Blockchain and Earn Voting Rewards (4.8% APR)
- TRON Blockchain: How to Send the USDT (TRC-20) Transacton using Python tronpy?
- How to Get Balance of TRX or USDT/USDD/USDC (TRC-20) Smart Contract Tokens on TRON Blockchain?
- Function to Return the USDT/USDD/USDC Contract Address on Tron Blockchain (Main Net, Nile, Shasta)
- How to Send/Transfer USDT/USDD/USDC on Tron Blockchain using Tronweb?
- Javascript Function to Send Trx on Tron Blockchain based on TronWeb
- How to Generate an Account on Tron Blockchain using Python SDK?
- How to Claim the Witness (Super Representative) Voting Rewards on Tron Blockchain using Node Js (Javascript)?
- Automate Freeze Balance on Tron Blockchain using NodeJs with TronWeb/TronGrid
- Python Tool of Base58 Decode Check
- TRON Blockchain: How to Check the Number of Peers the Tron Node is Connected to?
- TRON Blockchain: How to Check If a Node is synchronized and Fully Functional?
- How to Activate a TRON (TRX) Blockchain Wallet Address?
- Delayed Swap due to Numeric Underflow Bug by using Tron’s triggerSmartContract Method
–EOF (The Ultimate Computing & Technology Blog) —
Last Post: Azure Billing: How to Update the "Sold To" Address?
Next Post: ChatGPT-4 uses Math Wolfram Plugin to Solve a Math Brain Teaser Question