Introduction
Working with the Tron blockchain requires understanding several key metrics about your wallet and network resources. This guide demonstrates how to programmatically retrieve:
- TRX and USDT balances
- Available bandwidth and energy
- Implementation details using TronGrid API
Retrieving TRX and USDT Balances Online
Here's the optimized C# implementation to check cryptocurrency balances:
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];
if (obj == null) return tuple;
// Calculate TRX balance
var trxBalance = obj.balance != null
? (long)obj.balance / 1000000m
: 0m;
// Calculate USDT balance
var usdtBalance = 0m;
if (obj.trc20 != null)
{
foreach (var trc20Token in obj.trc20)
{
var tokenBalance = trc20Token.TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t;
if (tokenBalance != null)
usdtBalance = (long)tokenBalance / 1000000m;
}
}
return new Tuple<decimal, decimal>(trxBalance, usdtBalance);
}Checking Bandwidth and Energy Resources
Monitor your network resources with this method:
private static Tuple<long, long> GetAccountResource(string address)
{
var tuple = new Tuple<long, long>(0, 0);
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 tuple;
var responseObject = JsonConvert.DeserializeObject<dynamic>(responseString);
if (responseObject == null) return tuple;
// Calculate available bandwidth
var freeNetLimit = responseObject.freeNetLimit != null ? Convert.ToInt64(responseObject.freeNetLimit) : 0L;
var freeNetUsed = responseObject.freeNetUsed != null ? Convert.ToInt64(responseObject.freeNetUsed) : 0L;
var netLimit = responseObject.NetLimit != null ? Convert.ToInt64(responseObject.NetLimit) : 0L;
var netUsed = responseObject.NetUsed != null ? Convert.ToInt64(responseObject.NetUsed) : 0L;
var availableBandwidth = netLimit + freeNetLimit - netUsed - freeNetUsed;
// Calculate available energy
var energyLimit = responseObject.EnergyLimit != null ? Convert.ToInt64(responseObject.EnergyLimit) : 0L;
var energyUsed = responseObject.EnergyUsed != null ? Convert.ToInt64(responseObject.EnergyUsed) : 0L;
var availableEnergy = energyLimit - energyUsed;
return new Tuple<long, long>(availableBandwidth, availableEnergy);
}HTTP Client Helper Class
Our optimized HTTP client implementation:
public static class HttpClientHelper
{
public static string Post(string url, string requestBody, Encoding encoding, int timeout = 12000)
{
var httpWebResponse = Post((HttpWebRequest)WebRequest.Create(url), requestBody, encoding, timeout);
using var stream = httpWebResponse.GetResponseStream();
using var streamReader = new StreamReader(stream);
return streamReader.ReadToEnd();
}
private static HttpWebResponse Post(HttpWebRequest httpWebRequest, string requestBody, Encoding encoding, int timeout = 12000)
{
var bytes = encoding.GetBytes(requestBody);
httpWebRequest.Method = "POST";
httpWebRequest.ContentType = "application/json";
httpWebRequest.ContentLength = bytes.Length;
httpWebRequest.Timeout = timeout;
httpWebRequest.Accept = "application/json";
httpWebRequest.Headers.Set("TRON-PRO-API-KEY", "80a8b20f-a917-43a9-a2f1-809fe6eec0d6");
using (var stream = httpWebRequest.GetRequestStream())
{
stream.Write(bytes, 0, bytes.Length);
}
return (HttpWebResponse)httpWebRequest.GetResponse();
}
}๐ Learn more about Tron network development
Frequently Asked Questions
How often should I check my Tron wallet balance?
For active wallets, checking balances every 15-30 minutes is sufficient. For monitoring transactions in real-time, consider using Tron's event subscription system.
What's the difference between bandwidth and energy?
Bandwidth measures basic transaction capacity (like TRX transfers), while energy powers smart contract executions. Both can be obtained by staking TRX.
Why is my USDT balance showing zero?
Ensure you're checking the correct TRC20 contract address (TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t). Also confirm the tokens were actually sent to your Tron address, not an Ethereum address.
How can I increase my bandwidth?
You can increase bandwidth by:
- Staking more TRX
- Waiting for daily free bandwidth allocation
- Using energy to pay for transactions instead
What happens if I run out of bandwidth?
You'll need to pay transaction fees in TRX instead. Each transaction without sufficient bandwidth burns about 0.1 TRX.
๐ Explore Tron staking options
Key Technical Considerations
- API Rate Limits: TronGrid has rate limits (currently 2000 requests/minute)
- Error Handling: Always implement proper error handling for network issues
- Decimal Precision: TRX and USDT use 6 decimal places
- Authentication: Secure your API keys properly
- Asynchronous Operations: Consider async/await for better performance
Conclusion
This guide provides robust methods to programmatically monitor your Tron wallet's cryptocurrency balances and network resources. The implementations follow best practices for API integration, error handling, and resource management.
For production applications, consider:
- Adding caching for frequent balance checks
- Implementing webhook-based notifications
- Creating comprehensive monitoring dashboards