How to Send/Transfer USDT/USDD/USDC Tokens on Tron Blockchain using Tronweb (Javascript)?


tron-blockchain How to Send/Transfer USDT/USDD/USDC Tokens on Tron Blockchain using Tronweb (Javascript)? blockchain Cryptocurrency Cryptocurrency javascript Smart Contract Tron Tron Blockchain

tron-blockchain

On Tron blockchain, the USDT, USDD or USDC token are TRC-20 contracts. You can use the following Javascript function to send over the USDT/USDD/USDC Token from one address to another on the Tron Blockchain. The network parameter can be “nile”, “shasta” test net, or the main net otherwise.

You also need to specify the amount, the private key, the APP key (which is optional in nile or shasta test net), and also the corresponding USDT/USDC/USDD TRC-20 contract address. The function will invoke the “transfer” method on the contract (USDT, USDC or USDD).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
async function sendTRC20Token(network, fromAddress, toAddress, amount, privateKey, AppKey, CONTRACT) {
  let url = null;
  if (network === "shasta") {
    url = "https://api.shasta.trongrid.io";
  } else if (network === "nile") {
    url = "https://nile.trongrid.io";
  } else {
    url = "https://api.trongrid.io";
  }
  const tronWeb = new TronWeb({
    fullHost: url,
    headers: { "TRON-PRO-API-KEY": AppKey },
    privateKey: privateKey,
  });
  const options = {
    feeLimit: 10000000,
    callValue: 0
  };
  const tx = await tronWeb.transactionBuilder.triggerSmartContract(
    CONTRACT, 'transfer(address,uint256)', options,
    [{
      type: 'address',
      value: toAddress
    }, {
      type: 'uint256',
      value: amount * 1000000
    }],
    tronWeb.address.toHex(fromAddress)
  );
  const signedTx = await tronWeb.trx.sign(tx.transaction);
  const broadcastTx = await tronWeb.trx.sendRawTransaction(signedTx);
  return broadcastTx;
}
async function sendTRC20Token(network, fromAddress, toAddress, amount, privateKey, AppKey, CONTRACT) {
  let url = null;
  if (network === "shasta") {
    url = "https://api.shasta.trongrid.io";
  } else if (network === "nile") {
    url = "https://nile.trongrid.io";
  } else {
    url = "https://api.trongrid.io";
  }
  const tronWeb = new TronWeb({
    fullHost: url,
    headers: { "TRON-PRO-API-KEY": AppKey },
    privateKey: privateKey,
  });
  const options = {
    feeLimit: 10000000,
    callValue: 0
  };
  const tx = await tronWeb.transactionBuilder.triggerSmartContract(
    CONTRACT, 'transfer(address,uint256)', options,
    [{
      type: 'address',
      value: toAddress
    }, {
      type: 'uint256',
      value: amount * 1000000
    }],
    tronWeb.address.toHex(fromAddress)
  );
  const signedTx = await tronWeb.trx.sign(tx.transaction);
  const broadcastTx = await tronWeb.trx.sendRawTransaction(signedTx);
  return broadcastTx;
}

Example usage:

1
2
3
4
5
6
7
8
9
10
(async function() {
    const fromAddress = "TL.....";
    const toAddress = "TN.....";
    const amount = 1.2;
    const privateKey = "key";
    const AppKey = "Tron Pro API Key - Optional";
    const usdt_contract = "USDT contract see below";
    const network = "shasta, nile or main";
    await sendTRC20Token(network, fromAddress, toAddress, amount, privateKey, AppKey, usdt_contract);
})();
(async function() {
    const fromAddress = "TL.....";
    const toAddress = "TN.....";
    const amount = 1.2;
    const privateKey = "key";
    const AppKey = "Tron Pro API Key - Optional";
    const usdt_contract = "USDT contract see below";
    const network = "shasta, nile or main";
    await sendTRC20Token(network, fromAddress, toAddress, amount, privateKey, AppKey, usdt_contract);
})();

You can use the TronWeb library to send USDT (TRC-20) smart tokens using NodeJS. TronWeb is a JavaScript library that allows you to interact with the Tron blockchain. It provides an easy-to-use API for developers to build decentralized applications (dApps) on the Tron network.

To send USDT (TRC-20) smart tokens using NodeJS, you will need to create a TronWeb instance, get the address of the recipient, and then use the sendToken() method to send the tokens. The sendToken() method takes the recipient address, the amount of tokens to be sent, and the token name as parameters.

For example, the following code snippet shows how to send 10 USDT (TRC-20) tokens to a recipient address:

1
2
3
4
5
6
7
8
9
10
11
12
// Create a TronWeb instance
const tronWeb = new TronWeb({
    fullHost: 'https://api.trongrid.io'
});
 
// Get the recipient address
const recipientAddress = 'Txxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
 
// Send 10 USDT (TRC-20) tokens
tronWeb.sendToken(recipientAddress, 10, 'USDT').then(result => {
    console.log(result);
});
// Create a TronWeb instance
const tronWeb = new TronWeb({
    fullHost: 'https://api.trongrid.io'
});

// Get the recipient address
const recipientAddress = 'Txxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

// Send 10 USDT (TRC-20) tokens
tronWeb.sendToken(recipientAddress, 10, 'USDT').then(result => {
    console.log(result);
});

Smart Contract Addresses (TRC-20 Token) on Tron Blockchain for Stable coins (USDD, USDC and USDT) can be found below:

USDT TRC-20 Contract Addresses

USDT is the official stablecoin issued by Tether on the TRON network.

USDD TRC-20 Contract Addresses

USDD is a fully decentralized over-collateralization stablecoin.

USDC TRC-20 Contract Addresses

USDC is a fully collateralized US Dollar stablecoin developed by CENTRE, the open source project with Circle being the first of several forthcoming issuers.

Send TRX instead?: Javascript Function to Send Trx on Tron Blockchain based on TronWeb

Tron Blockchain

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
1343 words
Last Post: Teaching Kids Programming - Recursive Depth First Search Graph Algorithm to Determine a Strongly Connected Component
Next Post: Teaching Kids Programming - Greatest English Letter in Upper and Lower Case

The Permanent URL is: How to Send/Transfer USDT/USDD/USDC Tokens on Tron Blockchain using Tronweb (Javascript)?

One Response

  1. Anand Patel

Leave a Reply