golang 遍历区块信息时怎么解析出USDT转账信息?
// 查询交易 func Transactions() { client, err := ethclient.Dial("https://eth-sepolia.g.alchemy.com/v2/") if err != nil { log.Fatal(err) }
blockNumber := big.NewInt(5577438)
block, err := client.BlockByNumber(context.Background(), blockNumber)
if err != nil {
log.Fatal(err)
}
for _, tx := range block.Transactions() {
fmt.Println("Hash:" + tx.Hash().Hex()) //哈希 0x5d49fcaa394c97ec8a9c3e7bd9e8388d420fb050a52083ca52ff24b3b65bc9c2
fmt.Println("tx value:" + tx.Value().String()) //交易数量 10000000000000000
fmt.Println(tx.Gas()) // 105000
fmt.Println(tx.GasPrice().Uint64()) // 102000000000
fmt.Println(tx.Nonce()) // 110644
fmt.Println(tx.Data()) // []
toAddress := tx.To()
if toAddress != nil {
fmt.Println("toAddress:" + toAddress.String()) //接受地址
}
chainID, err := client.NetworkID(context.Background())
if err != nil {
log.Fatal(err)
}
// 发送者地址
if from, err := types.Sender(types.NewLondonSigner(chainID), tx); err == nil {
fmt.Println("fromAddress:" + from.Hex()) // 0x0fD081e3Bb178dc45c0cb23202069ddA57064258
}
receipt, err := client.TransactionReceipt(context.Background(), tx.Hash())
if err != nil {
log.Fatal(err)
}
fmt.Println(receipt.Status) // 1
fmt.Println(receipt.Logs)
}
//blockHash := common.HexToHash("0x9e8751ebb5069389b855bba72d94902cc385042661498a415979b7b6ee9ba4b9")
//count, err := client.TransactionCount(context.Background(), blockHash)
//if err != nil {
// log.Fatal(err)
//}
//
//for idx := uint(0); idx < count; idx++ {
// tx, err := client.TransactionInBlock(context.Background(), blockHash, idx)
// if err != nil {
// log.Fatal(err)
// }
//
// fmt.Println(tx.Hash().Hex()) // 0x5d49fcaa394c97ec8a9c3e7bd9e8388d420fb050a52083ca52ff24b3b65bc9c2
//}
//
//txHash := common.HexToHash("0x5d49fcaa394c97ec8a9c3e7bd9e8388d420fb050a52083ca52ff24b3b65bc9c2")
//tx, isPending, err := client.TransactionByHash(context.Background(), txHash)
//if err != nil {
// log.Fatal(err)
//}
//
//fmt.Println(tx.Hash().Hex()) // 0x5d49fcaa394c97ec8a9c3e7bd9e8388d420fb050a52083ca52ff24b3b65bc9c2
//fmt.Println(isPending) // false
}