[C#] 分頁查詢 TRON 區塊鏈交易,直到找到指定 txid 為止

2025-02-20

上一篇文章 追蹤 TRON 區塊鏈某合約的轉帳記錄 說到如何取得 某合約其中的所有交易

其實主要目的在於我們要去監測 TRC20 某合約在區塊上面的交易 ,但是上篇文章有一個小問題,就是一次只有 20 筆

今天我們繼續改造,我會做給予一個 txid 然後他會一直利用 Response  的 next 繼續找一直找到你指定的 txid 為止


1. 這裡面處理 http 的問題 我都是使用  RestSharp 

2. 因為一次查詢只有 20 個所以就是只能一直判斷有沒有找到,繼續往前( 往更舊的時間去查找) 

C# code:

public class ContractTransactionResponse { public List<Datum> data { get; set; } public bool success { get; set; } public Meta meta { get; set; } public class Datum { public List<Ret> ret { get; set; } public List<string> signature { get; set; } public string txID { get; set; } public int net_usage { get; set; } public string raw_data_hex { get; set; } public int net_fee { get; set; } public int energy_usage { get; set; } public string block_timestamp { get; set; } public string blockNumber { get; set; } public int energy_fee { get; set; } public int energy_usage_total { get; set; } public RawData raw_data { get; set; } public List<object> internal_transactions { get; set; } public class Ret { public string contractRet { get; set; } } public class RawData { 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 long fee_limit { get; set; } public object timestamp { get; set; } public class Contract { public Parameter parameter { get; set; } public string type { get; set; } public class Parameter { public Value value { get; set; } public string type_url { get; set; } public class Value { public string data { get; set; } public string owner_address { get; set; } public string contract_address { get; set; } } } } } } public class Meta { public long at { get; set; } public string fingerprint { get; set; } public Links links { get; set; } public int page_size { get; set; } public class Links { public string next { get; set; } } } } //call GetContractTransactionDataByTxId from USDT contract and find txid: 63958d9a93c206567123f8c5120742f0d024d5ffe45a6dc700f6cc169552b38b var res = GetContractTransactionDataByTxId(&quot;TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t&quot;, &quot;63958d9a93c206567123f8c5120742f0d024d5ffe45a6dc700f6cc169552b38b&quot;); Console.Write(JsonConvert.SerializeObject(res)); /// &lt;summary&gt; /// 在某合約中的交易查詢 尋找某 txid 交易資訊 /// &lt;/summary&gt; /// &lt;param name=&quot;contractAddress&quot;&gt;contract address&lt;/param&gt; /// &lt;param name=&quot;targetTxId&quot;&gt;target txid&lt;/param&gt; static ContractTransactionResponse.Datum GetContractTransactionDataByTxId(string contractAddress, string targetTxId) { var apiUri = &quot;https://api.trongrid.io/v1/contracts/&quot; + contractAddress + &quot;/transactions&quot;; var count = 0; while (!string.IsNullOrEmpty(apiUri)) { var client = new RestClient(apiUri); var request = new RestRequest(&quot;&quot;, Method.Get); request.AddHeader(&quot;accept&quot;, &quot;application/json&quot;); var response = client.Execute(request); var contractTransObj = JsonConvert.DeserializeObject&lt;ContractTransactionResponse&gt;(response.Content); foreach (var t in contractTransObj.data) { var amount = BigInteger.Parse(t.raw_data.contract[0].parameter.value.data.Substring(t.raw_data.contract[0].parameter.value.data.Length - 64, 64), NumberStyles.AllowHexSpecifier); var to = ConvertHexToBase58Check(&quot;41&quot; + t.raw_data.contract[0].parameter.value.data.Substring(32, 40)); var from = ConvertHexToBase58Check(t.raw_data.contract[0].parameter.value.owner_address); Console.WriteLine($&quot;Block: {t.blockNumber}, txid: {t.txID}, from: {from}, to: {to}, amount: {amount}&quot;); if (t.txID == targetTxId) { Console.WriteLine(&quot;找到目標交易&#65281;&quot;); Console.Write(&quot;搜尋過&quot; + count + &quot;筆資料&quot;); return t; break; } count++; } apiUri = contractTransObj.meta?.links?.next; } return null; // Console.WriteLine(response.Content); }



result:


請注意,如果真得要找到很久以前的可能要找非常久,因為畢竟這是20筆一次 request 回來的結果去比對的



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