Develop blockchain applications from scratch (13)--Ethereum block query


As we have seen, you can query block information in two ways.

1. Query block information

1.1 Obtain block information based on block height

Call the client's BlockByNumber method to get the full block. You can read all the content and metadata of the block, such as block number, block timestamp, block summary, block difficulty, and transaction list, etc.

// GetBlockByNumber 根据区块高度获取区块信息,isFullTx 全交易
func (eth *Http) GetBlockByNumber(height string, isFullTx bool) (interface{}, error) {
	args = []interface{}{height, isFullTx}
	params := NewHttpParams("eth_getBlockByNumber", args)
	resBody, err := eth.rpc.HttpRequest(params)
	if err != nil {
		return nil, err
	}
	return eth.ParseJsonRPCResponse(resBody)
}

1.2 Obtain block information based on block hash

Call the client's BlockByHash method to get the full block. You can read all the content and metadata of the block, such as block number, block timestamp, block summary, block difficulty, and transaction list, etc.

// GetBlockByHash 根据哈希获取区块信息,isFullTx 全交易
func (eth *Http) GetBlockByHash(hash string, isFullTx bool) (interface{}, error) {
	args = []interface{}{hash, isFullTx}
	params := NewHttpParams("eth_getBlockByHash", args)
	resBody, err := eth.rpc.HttpRequest(params)
	if err != nil {
		return nil, err
	}
	return eth.ParseJsonRPCResponse(resBody)
}

1.3 Complete code

package main

import (
    "context"
    "fmt"
    "log"
    "math/big"

    "github.com/ethereum/go-ethereum/ethclient"
)

func main() {
    client, err := ethclient.Dial("https://mainnet.infura.io")
    if err != nil {
        log.Fatal(err)
    }

    header, err := client.HeaderByNumber(context.Background(), nil)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(header.Number.String()) // 5671744

    blockNumber := big.NewInt(5671744)
    block, err := client.BlockByNumber(context.Background(), blockNumber)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(block.Number().Uint64())     // 5671744
    fmt.Println(block.Time().Uint64())       // 1527211625
    fmt.Println(block.Difficulty().Uint64()) // 3217000136609065
    fmt.Println(block.Hash().Hex())          // 0x9e8751ebb5069389b855bba72d94902cc385042661498a415979b7b6ee9ba4b9
    fmt.Println(len(block.Transactions()))   // 144
    
    fmt.Println(count) // 144
}

2. The latest block query

2.1 Get the latest block height

GetLatestBlockNumber Get the latest block height

func (eth *Http) GetLatestBlockNumber() (string, error) {
	args = []interface{}{}
	params := NewHttpParams("eth_blockNumber", args)
	resBody, err := eth.rpc.HttpRequest(params)
	if err != nil {
		return "0x0", err
	}
	res, err := eth.ParseJsonRPCResponse(resBody)
	if err != nil {
		return "0x0", err
	}
	return res.(string), nil
}

2.2 Get the latest block information

GetLatestBlock Get the latest block information, isFullTx full transaction

func (eth *Http) GetLatestBlock(isFullTx bool) (interface{}, error) {
	args = []interface{}{"latest", isFullTx}
	params := NewHttpParams("eth_getBlockByNumber", args)
	resBody, err := eth.rpc.HttpRequest(params)
	if err != nil {
		return nil, err
	}
	return eth.ParseJsonRPCResponse(resBody)
}

Guess you like

Origin blog.csdn.net/cljdsc/article/details/122724371