[C#] TRON(波場) 取得目前區塊高度

  • 1308
  • 0
  • 2025-11-18

最近有在處理一些區塊鏈的東西,基本上是基於 Tron ,因為一些需求所以我透過程式知道目前的區塊高度

想說就筆記一下,主要知道高度是有助於送出一筆交易後,之後的高度必須要筆交易該區塊的高度高上一個數值

基本上才會是公認為已經是穩定的區塊了。

1. 很簡單如果是基於 Nile 測試鏈,其實你只要透過 GET 這個網址就可以拿到了

Nile 測試鏈: https://nile.trongrid.io/wallet/getnowblock

其中有一個 number 就可以拿到現在高度

這你就自己 Parse 了這也不多說了

正式鏈的位置 : https://api.trongrid.io//walletsolidity/getnowblock

 

2. 透過 TronNet 這邊就是可以透過 C# 的部分,不過我看過原始碼,他採取的作法也就是透過 RPC 去呼叫的

其實我覺得跟做法一是差不多,只是透過 有人包好的 library 呼叫而已

 
C# Code :

        public static long  GetCurrentBlock()
        {
            IServiceCollection services = new ServiceCollection();

            IServiceCollection _TronServiceNile = new ServiceCollection();

            _TronServiceNile.AddTronNet(x =>
            {
                x.Network = TronNetwork.MainNet;
                x.Channel = new GrpcChannelOption { Host = "47.252.19.181", Port = 50051 };
                x.SolidityChannel = new GrpcChannelOption { Host = "47.252.19.181", Port = 50052 };
                x.ApiKey = "apikey";
            });


            services = Startup._TronServiceNile;
      
            services.AddLogging();
            var service = services.BuildServiceProvider();
      
            var newestBlock = service.GetService().GetProtocol().GetNowBlock(new TronNet.Protocol.EmptyMessage());

            return newestBlock.BlockHeader.RawData.Number;
           
        }

今天就紀錄到這,有機會在繼續記錄一下有碰到的問題

--

本文原文首發於我的個人部落格:TRON(波場) 取得目前區塊高度

reference:

https://developers.tron.network/reference/getnowblock

https://github.com/donma/TronNetDotNet6

https://github.com/stoway/TronNet

 

---

Yesterday I wrote down the code. I bet I could be your hero. I am a mighty little programmer.