How to Get Balance of TRX or USDT/USDD/USDC (TRC-20) Smart Contract Tokens on TRON Blockchain?


tron-blockchain How to Get Balance of TRX or USDT/USDD/USDC (TRC-20) Smart Contract Tokens on TRON Blockchain? blockchain Cryptocurrency Cryptocurrency nodejs Smart Contract Tron Blockchain

tron-blockchain

Get TRX Balance using Node Javascript

On Tron Blockchain, we can use the TronWeb to Get a Balance of 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
25
function getBalanceTRX(network, address, appKey) {
  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";
  }
  let headers = {};
  if (appKey) {
    headers["TRON-PRO-API-KEY"] = appKey;
  }
  const tronWeb = new TronWeb({
    fullHost: url,
    headers: headers
  });
  return new Promise((resolve, reject) => {
    tronWeb.trx.getBalance(address).then(
      result => {
          resolve(result / 1000000);
      }
    );
  });
}
function getBalanceTRX(network, address, appKey) {
  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";
  }
  let headers = {};
  if (appKey) {
    headers["TRON-PRO-API-KEY"] = appKey;
  }
  const tronWeb = new TronWeb({
    fullHost: url,
    headers: headers
  });
  return new Promise((resolve, reject) => {
    tronWeb.trx.getBalance(address).then(
      result => {
          resolve(result / 1000000);
      }
    );
  });
}

AppKey is optional but recommended. On Test Nets (Shasta or Nile), the API requests are un-limited. But on Main Net, the Tron Grid has rate limited the requests and it is preferrable to apply for a API key to be used for Tron Web / Tron Grid.

Get USDT/USDD/USDC Smart Contract Token Balance using Node Javascript

USDD and USDT and USDC are all TRC-20 Smart Contract coins on TRON blockchain. We can invoke the corresponding balance query method e.g. “balanceOf” on the contract. See below Node/Javascript code to query the balance on a Tron Wallet for a specified TRC-20 Smart Contract Tokens/Coins.

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
async function getBalance(network, address, CONTRACT, privateKey, appKey) {
    const trc20ContractAddress = 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";
    }
    let headers = {};
    if (appKey) {
      headers["TRON-PRO-API-KEY"] = appKey;
    }
    const tronWeb = new TronWeb({
      fullHost: url,
      headers: headers,
      privateKey: privateKey,
    });
    try {
        let contract = await tronWeb.contract().at(trc20ContractAddress);
        const result = await contract.balanceOf(address).call();
        return result / 1000000;
    } catch(error) {
        console.log(error);
        return null;
    }
}
async function getBalance(network, address, CONTRACT, privateKey, appKey) {
    const trc20ContractAddress = 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";
    }
    let headers = {};
    if (appKey) {
      headers["TRON-PRO-API-KEY"] = appKey;
    }
    const tronWeb = new TronWeb({
      fullHost: url,
      headers: headers,
      privateKey: privateKey,
    });
    try {
        let contract = await tronWeb.contract().at(trc20ContractAddress);
        const result = await contract.balanceOf(address).call();
        return result / 1000000;
    } catch(error) {
        console.log(error);
        return null;
    }
}

You might want to adjust the precision (number of decimals) accordingly for the stable coins. For example, the USDD has 18 decimal places, and thus you might want to divide by 10^18 instead of 1000000 in above code.

This method requires the private key (password) of your own TRON account wallet address.

Tron Blockchain

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
901 words
Last Post: Function to Return the USDT/USDD/USDC Contract Address on Tron Blockchain (Main Net, Nile, Shasta)
Next Post: Teaching Kids Programming - Valid Square Algorithm by Four Points in Cartesian Coordinate System

The Permanent URL is: How to Get Balance of TRX or USDT/USDD/USDC (TRC-20) Smart Contract Tokens on TRON Blockchain?

Leave a Reply