隨手記錄常用介面的實現,方便以後複製。
線上獲取 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)離線生成私鑰和地址筆記