Skip to content

Instantly share code, notes, and snippets.

@donma
Last active February 20, 2025 09:11
Show Gist options
  • Select an option

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

Select an option

Save donma/b1c59b1cf8dbaf4418ebea6a34012fca to your computer and use it in GitHub Desktop.
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("TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t", "63958d9a93c206567123f8c5120742f0d024d5ffe45a6dc700f6cc169552b38b");
Console.Write(JsonConvert.SerializeObject(res));
/// <summary>
/// 在某合約中的交易查詢 尋找某 txid 交易資訊
/// </summary>
/// <param name="contractAddress">contract address</param>
/// <param name="targetTxId">target txid</param>
static ContractTransactionResponse.Datum GetContractTransactionDataByTxId(string contractAddress, string targetTxId)
{
var apiUri = "https://api.trongrid.io/v1/contracts/" + contractAddress + "/transactions";
var count = 0;
while (!string.IsNullOrEmpty(apiUri))
{
var client = new RestClient(apiUri);
var request = new RestRequest("", Method.Get);
request.AddHeader("accept", "application/json");
var response = client.Execute(request);
var contractTransObj = JsonConvert.DeserializeObject<ContractTransactionResponse>(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("41" + 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($"Block: {t.blockNumber}, txid: {t.txID}, from: {from}, to: {to}, amount: {amount}");
if (t.txID == targetTxId)
{
Console.WriteLine("找到目標交易!");
Console.Write("搜尋過" + count + "筆資料");
return t;
break;
}
count++;
}
apiUri = contractTransObj.meta?.links?.next;
}
return null;
// Console.WriteLine(response.Content);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment