-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path13queryFullBlock.go
38 lines (33 loc) · 1009 Bytes
/
13queryFullBlock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package main
import (
"context"
"fmt"
"github.com/ethereum/go-ethereum/ethclient"
"log"
"math/big"
)
func main() {
client, err := ethclient.Dial("https://cloudflare-eth.com")
if err != nil {
log.Fatal(err)
}
blockNumber := big.NewInt(14883178)
// 第二个参数nil,表示返回最新的区块信息
block, err := client.BlockByNumber(context.Background(), blockNumber)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Block Number: %s\n", block.Number())
fmt.Printf("Block Time: %d\n", block.Time())
fmt.Printf("Block Difficulty: %d\n", block.Difficulty())
fmt.Printf("Block GasUsed: %d\n", block.GasUsed())
fmt.Printf("Block GasLimit: %d\n", block.GasLimit())
// 查询区块上交易的数目
fmt.Printf("Block Transactions Count: %d\n", len(block.Transactions()))
// 另一种查询区块交易数目的方法
count, err := client.TransactionCount(context.Background(), block.Hash())
if err != nil {
log.Fatal(err)
}
fmt.Printf("Block Transactions Count: %d\n", count)
}