On TRON blockchain, we can staked TRX (freeze) to earn energy or bandwidth. We can obtain the frozen balance via the tron web library. The tronWeb.trx.getAccount returns a JSON object that contains the account information. The frozen bandwidth is at .frozen and the energy is at account_resource.frozen_balance_for_energy.
We can process these two data and then return a JSON object that computes the total staked TRX for energy and bandwidth respectively. We need to convert the SUN to TRX using the tronWeb.fromSun function. The SUN is the smallest unit on TRON blockchain (1 TRX = 1,000,000 SUN)
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 34 35 36 37 38 | function getFrozonBalance(network, address) { network = network.toLowerCase(); let url = null; if (network === "mainnet") { url = "https://api.trongrid.io"; } else if (network === "shasta") { url = "https://api.shasta.trongrid.io"; } else if (network === "nile") { url = "https://nile.trongrid.io"; } const tronWeb = new TronWeb({ fullHost: url }); return new Promise((resolve, reject) => { tronWeb.trx.getAccount(address).then( result => { let data = { "bandwidth": 0, "energy": 0 } if (!result || typeof result === "undefined") { resolve(data); } if (result.frozen != null && typeof result.frozen !== "undefined") { for (var i = 0; i < result.frozen.length; ++ i) { data["bandwidth"] += parseFloat(tronWeb.fromSun(result.frozen[i].frozen_balance)); } } if (result.account_resource != null && typeof result.account_resource !== "undefined") { if (result.account_resource.frozen_balance_for_energy != null && typeof result.account_resource.frozen_balance_for_energy !== "undefined") { data["energy"] += parseFloat(tronWeb.fromSun(result.account_resource.frozen_balance_for_energy.frozen_balance)); } } resolve(data); } ); }); } |
function getFrozonBalance(network, address) { network = network.toLowerCase(); let url = null; if (network === "mainnet") { url = "https://api.trongrid.io"; } else if (network === "shasta") { url = "https://api.shasta.trongrid.io"; } else if (network === "nile") { url = "https://nile.trongrid.io"; } const tronWeb = new TronWeb({ fullHost: url }); return new Promise((resolve, reject) => { tronWeb.trx.getAccount(address).then( result => { let data = { "bandwidth": 0, "energy": 0 } if (!result || typeof result === "undefined") { resolve(data); } if (result.frozen != null && typeof result.frozen !== "undefined") { for (var i = 0; i < result.frozen.length; ++ i) { data["bandwidth"] += parseFloat(tronWeb.fromSun(result.frozen[i].frozen_balance)); } } if (result.account_resource != null && typeof result.account_resource !== "undefined") { if (result.account_resource.frozen_balance_for_energy != null && typeof result.account_resource.frozen_balance_for_energy !== "undefined") { data["energy"] += parseFloat(tronWeb.fromSun(result.account_resource.frozen_balance_for_energy.frozen_balance)); } } resolve(data); } ); }); }
We can use the tronWeb to return the balance for TRX and TRC-20 smart contract tokens: How to Get Balance of TRX or USDT/USDD/USDC (TRC-20) Smart Contract Tokens on TRON Blockchain?
Tron Blockchain
- 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) —
loading...
Last Post: Teaching Kids Programming - Largest Perimeter Triangle (Sorting + Greedy Algorithm)
Next Post: BASH Script to Query and Monitor the Crypto Prices (Exchange Rate to Fiat)