4_get_block

command
v0.0.0-...-6e38dd8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 12, 2023 License: MIT Imports: 5 Imported by: 0

README

查询区块

对区块链数据的查询,除了之前提到的ChainID和当前的BlockNumber之外,对区块头(Header)的查询也比较常见,甚至有时候需要查询整个区块(Block)。

查询区块头和完整区块信息都有两种方法,按Number或者按Hash

bn := big.NewInt(18382246)
header, err := client.HeaderByNumber(ctx, bn)
if err != nil {
    log.Fatalf("get header error: %s", err)
}
log.Printf("block %s, hash: %s, gas used: %d, base fee: %s", bn.String(), header.Hash(), header.GasUsed, header.BaseFee.String())

一般情况下,重要的信息在Header里都已经有了,比如BaseFee等,不太需要检索整个区块。如果请求整个区块的话,数据量比较大,尽量要少用。

block, err := client.BlockByHash(ctx, header.Hash())
if err != nil {
    log.Fatalf("get block error: %s", err)
}
log.Printf("block %s, time: %d, txs: %d", block.Hash(), block.Time(), len(block.Transactions()))

完整代码

package main

import (
	"context"
	"github.com/ethereum/go-ethereum/ethclient"
	"log"
	"math/big"
	"os"
)

func main() {
	apiKey := os.Getenv("INFURA_API_KEY")
	url := "https://mainnet.infura.io/v3/" + apiKey
	client, err := ethclient.Dial(url)
	if err != nil {
		log.Fatalf("Could not connect to Infura with ethclient: %s", err)
	}
	ctx := context.Background()

	bn := big.NewInt(18382246)
	header, err := client.HeaderByNumber(ctx, bn)
	if err != nil {
		log.Fatalf("get header error: %s", err)
	}
	log.Printf("block %s, hash: %s, gas used: %d, base fee: %s", bn.String(), header.Hash(), header.GasUsed, header.BaseFee.String())

	block, err := client.BlockByHash(ctx, header.Hash())
	if err != nil {
		log.Fatalf("get block error: %s", err)
	}
	log.Printf("block %s, time: %d, txs: %d", block.Hash(), block.Time(), len(block.Transactions()))
}

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL