Javascript Function to Send Trx on Tron Blockchain based on TronWeb


tron-blockchain Javascript Function to Send Trx on Tron Blockchain based on TronWeb blockchain Cryptocurrency Cryptocurrency javascript Smart Contract Tron Tron Blockchain TRX

tron-blockchain

Sending TRX coin on TRON blockchain is easy. We can use the TronWeb library and here is the Javascript function to send Trx to a Tron wallet address:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
async function sendTrx(fromAddress, toAddress, amount, privateKey, AppKey) {
  let headers = null;
  if (AppKey) {
      headers = { "TRON-PRO-API-KEY": AppKey };
  }
  const tronWeb = new TronWeb({
      fullHost: 'https://api.shasta.trongrid.io',
      headers: headers,   
      privateKey: privateKey,
  });
  const tradeobj = await tronWeb.transactionBuilder.sendTrx(
        tronWeb.address.toHex(toAddress),
        amount * 1000 * 1000,
        tronWeb.address.toHex(fromAddress)
  );
  const signedtxn = await tronWeb.trx.sign(
        tradeobj,
        privateKey
  );
  const receipt = await tronWeb.trx.sendRawTransaction(
        signedtxn
  );
  return receipt;
}
async function sendTrx(fromAddress, toAddress, amount, privateKey, AppKey) {
  let headers = null;
  if (AppKey) {
      headers = { "TRON-PRO-API-KEY": AppKey };
  }
  const tronWeb = new TronWeb({
      fullHost: 'https://api.shasta.trongrid.io',
      headers: headers,   
      privateKey: privateKey,
  });
  const tradeobj = await tronWeb.transactionBuilder.sendTrx(
        tronWeb.address.toHex(toAddress),
        amount * 1000 * 1000,
        tronWeb.address.toHex(fromAddress)
  );
  const signedtxn = await tronWeb.trx.sign(
        tradeobj,
        privateKey
  );
  const receipt = await tronWeb.trx.sendRawTransaction(
        signedtxn
  );
  return receipt;
}

Example usage:

1
2
3
4
5
6
7
8
(async function() {
    const fromAddress = "TL.....";
    const toAddress = "TN.....";
    const amount = 1.2;
    const privateKey = "key";
    const AppKey = "Tron Pro API Key - Optional";
    await sendTrx(fromAddress, toAddress, amount, privateKey, AppKey);
})();
(async function() {
    const fromAddress = "TL.....";
    const toAddress = "TN.....";
    const amount = 1.2;
    const privateKey = "key";
    const AppKey = "Tron Pro API Key - Optional";
    await sendTrx(fromAddress, toAddress, amount, privateKey, AppKey);
})();

You can sign up on TronGrid and get 3 free api key if you need more quota of API usage. For testnets such as Shasta or Nile, you don’t need the Tron Pro API key.

Send USDT TRC-20 Token instead? How to Send/Transfer USDT on Tron Blockchain using Tronweb?

Tron Blockchain

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
762 words
Last Post: Teaching Kids Programming - Permutation of K out of N Visible Blocks via Top Down Dynamic Programming Algorithm
Next Post: Teaching Kids Programming - Largest Unique Number via Hash Table, Bucket Sorting and GroupBy Algorithms

The Permanent URL is: Javascript Function to Send Trx on Tron Blockchain based on TronWeb

Leave a Reply