Tron Blockchain: Javascript (NodeJs) Function to Get the Frozen Balance (Staked TRX) for Energy and Bandwidth | ninjasquad
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?
–EOF (The Ultimate Computing & Technology Blog) —
GD Star Rating
a WordPress rating system
445 words
Last Post: Teaching Kids Programming – Largest Perimeter Triangle (Sorting + Greedy Algorithm)
Source: Internet