获取波场(Tron)钱包TRX、USDT余额和剩余带宽、能量 - 笔记
随手记录常用接口的实现,方便以后复制。
在线获取 TRX 和 USDT 余额
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 == null) return tuple;
if ((bool)responseObject.success != true) return tuple;
if (responseObject.data == null || responseObject.data.Count == 0) return tuple;
var obj = responseObject.data[0];
if (obj == null) return tuple;
var trxBalance = new decimal(0); // TRX 余额
var balance = obj.balance;
if (balance != null) trxBalance = (long)balance / new decimal(1000000);
var etherBalance = new decimal(0); // USDT 余额
var trc20Tokens = obj.trc20;
if (trc20Tokens != null) {
foreach (var trc20Token in trc20Tokens) {
var tokenBalance = trc20Token.TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t; // USDT 合约地址
if (tokenBalance != null) etherBalance = (long)tokenBalance / new decimal(1000000);
}
}
return new Tuple<decimal, decimal>(trxBalance, etherBalance);
}
在线获取带宽和能量
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;
var freeNetLimit = 0L; //免费带宽数量
if (responseObject.freeNetLimit != null) freeNetLimit = Convert.ToInt64(responseObject.freeNetLimit);
var freeNetUsed = 0L; //已使用免费带宽数量
if (responseObject.freeNetUsed != null) freeNetUsed = Convert.ToInt64(responseObject.freeNetUsed);
var netLimit = 0L; //质押获取的带宽数量
if (responseObject.NetLimit != null) netLimit = Convert.ToInt64(responseObject.NetLimit);
var netUsed = 0L; //已使用质押获取的带宽数量
if (responseObject.NetUsed != null) netUsed = Convert.ToInt64(responseObject.NetUsed);
var energyLimit = 0L; //质押获取的能量数量
if (responseObject.EnergyLimit != null) energyLimit = Convert.ToInt64(responseObject.EnergyLimit);
var energyUsed = 0L; //已使用质押获取的能量数量
if (responseObject.EnergyUsed != null) energyUsed = Convert.ToInt64(responseObject.EnergyUsed);
return new Tuple<long, long>(netLimit + freeNetLimit - netUsed - freeNetUsed, energyLimit - energyUsed);
}
HttpClientHelper 类 封装
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();
}
}
其他
波场(Tron)钱包设置多签
波场(Tron)网页版(本地)钱包开源
波场(Tron)项目常用工具分享
波场(Tron)离线签名、广播交易笔记
波场(Tron)离线生成私钥和地址笔记