[C#] 透過 Trongrid 準確獲取 Tron 區塊內的完整交易資料

2024-12-10

前陣子分享了兩篇文章,透過 Trongrid 取得 Tron 一個指定區塊中的交易資料透過 Trongrid 取得 Tron 區塊內 USDT 交易資料

但是一直有一個疑問,我撈取的資料,為何跟 Tronscan 上面的區塊數字不同,就以 https://tronscan.org/#/block/64991111

來看明明就有 388 筆資料,但是我之前卻只抓到 159 ...


後來我找一下找到這個 https://developers.tron.network/reference/getblockbynum 可以透過撈取該 Block 的資料

只好實際來測試一下

1. 這裡面我都透過 RestSharp 處理 關於 http 的相關事情

2. 接下來就是抓取的 C# Code

2.1 這邊我規劃一隻 function 直接呼叫 getblockbynum 並且,直接轉成 BlockNumResponse 物件

var blockInfo = GetTrc20BlockByNum("64991111"); foreach (var transaction in blockInfo.transactions) { var owner_address = ConvertHexToBase58Check(transaction.raw_data.contract[0].parameter.value.owner_address); Console.WriteLine("txid:" + transaction.txID + ", from:" + owner_address); } Console.WriteLine("Block Data Count:" + blockInfo.transactions.Count); /// <summary> /// Block num /// </summary> /// <param name="blockNumber"></param> /// <returns></returns> private static BlockNumResponse GetTrc20BlockByNum(string blockNumber) { var client = new RestClient("https://api.trongrid.io/walletsolidity/getblockbynum"); var request = new RestRequest("?num=" + blockNumber, Method.Get); var response = client.Execute(request).Content; var result = JsonConvert.DeserializeObject<BlockNumResponse>(response); return result; }


2.2 BlockNumResponse 物件

public class BlockNumResponse { public string blockID { get; set; } public BlockHeader block_header { get; set; } public List<Transaction> transactions { get; set; } public class BlockHeader { public RawData raw_data { get; set; } public string witness_signature { get; set; } } public class Transaction { public List<Ret> ret { get; set; } public List<string> signature { get; set; } public string txID { get; set; } public class Ret { public string contractRet { get; set; } } public RawData raw_data { get; set; } public string raw_data_hex { get; set; } } public class RawData { public int number { get; set; } public string txTrieRoot { get; set; } public string witness_address { get; set; } public string parentHash { get; set; } public int version { get; set; } public long timestamp { get; set; } public List<Contract> contract { get; set; } public string ref_block_bytes { get; set; } public string ref_block_hash { get; set; } public object expiration { get; set; } public int? fee_limit { get; set; } public class Contract { public Parameter parameter { get; set; } public string type { get; set; } public int Permission_id { get; set; } public class Parameter { public Value value { get; set; } public string type_url { get; set; } public class Value { public object balance { get; set; } public string resource { get; set; } public string receiver_address { get; set; } public string owner_address { get; set; } public string data { get; set; } public string contract_address { get; set; } public long? amount { get; set; } public string to_address { get; set; } public string asset_name { get; set; } public int? call_value { get; set; } } } } } }


這裡面的 transaction 物件大概是長這樣




2.3 因為其中 owner_address 並不是用 trc20 標準錢包的顯示,所以有一些轉換的 functions 這邊之前有人反應過

這邊我提供不需要 TronNet 的轉換版本

//C static string ConvertHexToBase58Check(string hexAddress) { byte[] addressBytes = HexToBytes(hexAddress); // 計算雙重 SHA-256 校驗碼 using (SHA256 sha256 = SHA256.Create()) { byte[] hash1 = sha256.ComputeHash(addressBytes); byte[] hash2 = sha256.ComputeHash(hash1); // 取前 4 位作為校驗碼 byte[] checksum = hash2.Take(4).ToArray(); // 拼接地址與校驗碼 byte[] addressWithChecksum = addressBytes.Concat(checksum).ToArray(); // 轉換為 Base58 return EncodeBase58(addressWithChecksum); } } static byte[] HexToBytes(string hex) { return Enumerable.Range(0, hex.Length / 2) .Select(x => Convert.ToByte(hex.Substring(x * 2, 2), 16)) .ToArray(); } static string EncodeBase58(byte[] input) { var Base58Chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; var intData = new BigInteger(input.Reverse().Concat(new byte[] { 0 }).ToArray()); string result = ""; while (intData > 0) { int remainder = (int)(intData % 58); intData /= 58; result = Base58Chars[remainder] + result; } // 保留前導 0 foreach (byte b in input) { if (b == 0) result = '1' + result; else break; } return result; }


result :

看起來結果事一樣的,這時候你就問我了他似乎給的資訊不多,那怎麼辦看來就是要用 txid 在去 trongrid 查詢該交易的詳細狀況


記錄到這邊希望有幫助到你..



當麻許的碎念筆記 2014 | Donma Hsu Design.