[C#] Etherscan 簡單串接,取得 Gas , From , To , blockNumber , Confirmations

2018-11-09

最近,因為專案需求,必須要知道一個 Txid 的 confirm 數字,所以我看了一下,應該 etherscan 是最穩定而且是最大的

image


首先,說一下範例,主要你要去這邊 申請一個 API Keys https://etherscan.io/myapikey,接下來第二步,你持有一個 txid  在範例中我使用的 txid 是 一個 0xe9156fcd7ea1122bd97fd2cc58784b83e66385da41333753f0e1d584002aaf2c

接下來,到這裡下載我寫得簡單的 函式庫 : https://github.com/donma/EtherscanSimpleSDK

取得相關資料 Sample code :

var service = new EtherscanParse.TrasactionAgent(YOUR_TOKEN); var info = service.GetTrasactionInfo(TXID); Console.WriteLine("Gas Limit:" + Convert.ToInt32(info.result.gas, 16).ToString()); Console.WriteLine("Gas Price:" + ((decimal)Int64.Parse(info.result.gasPrice.Replace("0x", ""), NumberStyles.AllowHexSpecifier) / 1000000000000000000).ToString("")); Console.WriteLine("From:"+info.result.from); Console.WriteLine("To:" + info.result.to); Console.WriteLine("hash(TXID):" + info.result.hash); Console.WriteLine("value:" + ((decimal)Int64.Parse(info.result.value.Replace("0x", ""), NumberStyles.AllowHexSpecifier) / 1000000000000000000).ToString("")); Console.WriteLine("blockHash:" + info.result.blockHash); Console.WriteLine("blockNumber:" + Convert.ToInt32(info.result.blockNumber, 16).ToString());

result:
image

透過上面的 資料我們取得 他的 Block 編號是 6653217 ,之後我們可以透過 BlockNumber 再加上你要查詢的錢包位置,去找出 confirm 數字

var brInfo= service.GetBlockrewardWalletInfo(Convert.ToInt32(info.result.blockNumber, 16).ToString(), TEST_WALLET_ADDRESS); Console.WriteLine("\r\n Wallet Data : \r\n"); Console.WriteLine("\r\n-----------------------------------------"); foreach (var wt in brInfo.result) { Console.WriteLine("Source :"+JsonConvert.SerializeObject(wt)); Console.WriteLine(""); Console.WriteLine("TimeStamp:"+ ConvertTimestampToDateTime(double.Parse(wt.timeStamp)).ToString("yyyy-MM-dd HH:mm:ss")); Console.WriteLine("Confirmations:" + wt.confirmations); Console.WriteLine(""); }


result:
image

這sample code  我放在 https://github.com/donma/EtherscanSimpleSDK 可以去這裡下載看看。


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