go getch 查询交易
import (
"context"
"fmt"
"log"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
)
func main() {
var url string = "http://localhost:8545"
client, err := ethclient.Dial(url)
if err != nil {
log.Fatal(err)
}
// 依据区块查询交易,设置区块编码
blockNumber := big.NewInt(137)
block, err := client.BlockByNumber(context.Background(), blockNumber)
if err != nil {
log.Fatal(err)
}
// 遍历区块下的交易
for _, tx := range block.Transactions() {
fmt.Println(tx.Hash().Hex()) //
fmt.Println("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()) // []
// 查询事务数据 1 成功 0失败
receipt, err := client.TransactionReceipt(context.Background(), tx.Hash())
if err != nil {
log.Fatal("receipt failed,", err)
}
fmt.Println("receipt.Status,", receipt.Status)
}
txHash := common.HexToHash("0x2d470cde76abba4ea10a2d973dfb89f60d49064ab801cba7389af5954f01aa03")
tx, isPending, err := client.TransactionByHash(context.Background(), txHash)
if err != nil {
log.Fatal("isPending error", err)
}
fmt.Println("sss", tx.Hash().Hex()) //
fmt.Println("sss22", isPending) // false
}
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!