Skip to content

Instantly share code, notes, and snippets.

@donma
Last active February 7, 2025 07:52
Show Gist options
  • Select an option

  • Save donma/55940c81a56791df8b7fb125ca86efd9 to your computer and use it in GitHub Desktop.

Select an option

Save donma/55940c81a56791df8b7fb125ca86efd9 to your computer and use it in GitHub Desktop.
public class BlockData
{
public List<Datum> data { get; set; }
public bool success { get; set; }
public Meta meta { get; set; }
public class Datum
{
public int block_number { get; set; }
public object block_timestamp { get; set; }
public string caller_contract_address { get; set; }
public string contract_address { get; set; }
public int event_index { get; set; }
public string event_name { get; set; }
public Result result { get; set; }
public ResultType result_type { get; set; }
public string @event { get; set; }
public string transaction_id { get; set; }
public class Result
{
[JsonProperty("0")]
public string _0 { get; set; }
[JsonProperty("1")]
public string _1 { get; set; }
[JsonProperty("2")]
public string _2 { get; set; }
public string from { get; set; }
public string to { get; set; }
public string value { get; set; }
}
public class ResultType
{
public string from { get; set; }
public string to { get; set; }
public string value { 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; }
}
}
public static string GetBase58CheckAddress(string ethAddress)
{
string fixaddress = "0x41" + TronNet.ByteArrary.RemoveHexPrefix(ethAddress);
byte[] addressBytes = TronNet.ByteArrary.HexToByteArray(fixaddress);
byte[] hash0 = ToSHA256(addressBytes);
byte[] hash1 = ToSHA256(hash0);
var checkSum = hash1.Take(4).ToArray();
return Base58Encoding.Encode(addressBytes.Concat(checkSum).ToArray());
}
public static byte[] ToSHA256(byte[] data)
{
using (var alg = SHA256.Create())
{
var hashValue = alg.ComputeHash(data);
return hashValue;
}
}
/// <summary>
/// 透過 trongrid 取得 block 中的 transaction
/// </summary>
/// <param name="blockNum">Block Number </param>
static void GetBlockData(string blockNum)
{
string baseUrl = $"https://api.trongrid.io/v1/blocks/{blockNum}/events";
var allTransactions = new List<BlockData.Datum>();
using (HttpClient client = new HttpClient())
{
string nextPage = baseUrl;
while (!string.IsNullOrEmpty(nextPage))
{
var response = client.GetStringAsync(nextPage).Result;
var blockData = JsonConvert.DeserializeObject<BlockData>(response);
foreach (var d in blockData.data) {
if (d.result.from != null && d.result.to != null) {
Console.WriteLine("txid:" + d.transaction_id + ", from:" + GetBase58CheckAddress(d.result.from) + " , to:" + GetBase58CheckAddress(d.result.to));
}
}
// 暫存交易資料
if (blockData.data != null)
{
allTransactions.AddRange(blockData.data);
}
if (blockData.meta.links != null)
{
nextPage = blockData.meta != null ? blockData.meta.fingerprint : "";
if (!string.IsNullOrEmpty(blockData.meta.links.next))
{
nextPage = blockData.meta.links.next;
}
}
else
{
break;
}
}
}
Console.WriteLine($"獲取到 {allTransactions.Count} 筆交易");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment