This guide provides step-by-step methods to programmatically retrieve TRON wallet information, including TRX/USDT balances and remaining bandwidth/energy resources.
Key Features to Check in a TRON Wallet
- TRX Balance: Native cryptocurrency of the TRON network
- USDT (TRC20) Balance: Stablecoin transactions on TRON
- Bandwidth: Network resource for regular transactions
- Energy: Computational resource for smart contracts
๐ Need a reliable TRON wallet? Check this secure option
Retrieving TRX and USDT Balances Online
private static Tuple<decimal, decimal> GetBalanceByAddressByOnline(string address)
{
var tuple = new Tuple<decimal, decimal>(0, 0);
var responseString = HttpClientHelper.Get($"https://api.trongrid.io/v1/accounts/{address}");
if (string.IsNullOrEmpty(responseString)) return tuple;
var responseObject = JsonConvert.DeserializeObject<dynamic>(responseString);
if (responseObject?.success != true || responseObject?.data == null) return tuple;
var obj = responseObject.data[0];
var trxBalance = obj?.balance != null ? (long)obj.balance / 1000000m : 0m;
decimal usdtBalance = 0m;
if (obj?.trc20 != null)
{
foreach (var token in obj.trc20)
{
var balance = token["TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t"];
if (balance != null) usdtBalance = (long)balance / 1000000m;
}
}
return new Tuple<decimal, decimal>(trxBalance, usdtBalance);
}Checking Bandwidth and Energy Resources
private static Tuple<long, long> GetAccountResource(string address)
{
var requestObj = new { address = address, visible = true };
var responseString = HttpClientHelper.Post(
"https://api.trongrid.io/wallet/getaccountresource",
JsonConvert.SerializeObject(requestObj),
Encoding.UTF8
);
if (string.IsNullOrEmpty(responseString))
return new Tuple<long, long>(0, 0);
var responseObject = JsonConvert.DeserializeObject<dynamic>(responseString);
long CalculateRemaining(long limit, long used) =>
(limit != null ? Convert.ToInt64(limit) : 0) -
(used != null ? Convert.ToInt64(used) : 0);
var bandwidth = CalculateRemaining(responseObject?.freeNetLimit, responseObject?.freeNetUsed) +
CalculateRemaining(responseObject?.NetLimit, responseObject?.NetUsed);
var energy = CalculateRemaining(responseObject?.EnergyLimit, responseObject?.EnergyUsed);
return new Tuple<long, long>(bandwidth, energy);
}Essential HttpClient Helper Class
public static class HttpClientHelper
{
public static string Get(string url, int timeout = 12000)
{
var request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout = timeout;
request.Headers.Add("TRON-PRO-API-KEY", "your-api-key");
using var response = (HttpWebResponse)request.GetResponse();
using var stream = response.GetResponseStream();
using var reader = new StreamReader(stream);
return reader.ReadToEnd();
}
public static string Post(string url, string requestBody, Encoding encoding, int timeout = 12000)
{
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/json";
request.Timeout = timeout;
request.Headers.Add("TRON-PRO-API-KEY", "your-api-key");
var bytes = encoding.GetBytes(requestBody);
using (var stream = request.GetRequestStream())
{
stream.Write(bytes, 0, bytes.Length);
}
using var response = (HttpWebResponse)request.GetResponse();
using var responseStream = response.GetResponseStream();
using var reader = new StreamReader(responseStream);
return reader.ReadToEnd();
}
}๐ Looking for advanced TRON development tools? Explore options here
FAQ: TRON Wallet Resources
Q: What's the difference between bandwidth and energy in TRON?
A: Bandwidth covers regular transactions while energy is required for smart contract executions.
Q: How often should I check my wallet balance programmatically?
A: For most applications, checking every 5-10 minutes is sufficient unless you need real-time updates.
Q: Can I use these methods for production applications?
A: Yes, but implement proper error handling and consider rate limits when calling the TRON Grid API.
Q: Why is my USDT balance showing zero when I know I have funds?
A: Ensure you're checking the correct TRC20 contract address (TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t).
Q: How can I optimize my bandwidth usage?
A: Consolidate transactions and stake TRX to earn additional bandwidth resources.
Q: What happens if I run out of energy?
A: Smart contract calls will fail - you'll need to either wait for energy to replenish or freeze TRX to get more.
Best Practices for TRON Developers
- Always cache API responses when possible
- Implement proper error handling for network issues
- Consider using WebSocket connections for real-time updates
- Monitor your API key usage to avoid rate limits
- Keep your TRON Grid API key secure
Remember to test all wallet interactions thoroughly before deploying to production environments.