We can use Go to access the Ethereum blocks. The following shows the details of the current block and one with a specific ID:
Ethereum Block in Go |
Theory
package main import ( "fmt" "strconv" "os" "context" "math/big" "github.com/ethereum/go-ethereum/ethclient" "time" ) func main() { client, _ := ethclient.Dial("https://mainnet.infura.io") blk:="7651340" argCount := len(os.Args[1:]) if (argCount>0) {blk= os.Args[1]} header, _ := client.HeaderByNumber(context.Background(), nil) fmt.Printf("Current block: %s\n\n",header.Number.String()) b,_:=strconv.ParseInt(header.Number.String(), 10, 64) blockNumber := big.NewInt(b) block, _ := client.BlockByNumber(context.Background(), blockNumber) fmt.Printf("Block number:\t%d\n",block.Number().Uint64()) d:= time.Unix(int64(block.Time()),0) fmt.Printf("Date:\t%s\n",d) fmt.Printf("Difficulty: %d\n",block.Difficulty().Uint64()) fmt.Printf("Hash: %s\n",block.Hash().Hex()) fmt.Printf("Length: %d\n",len(block.Transactions())) b,_=strconv.ParseInt(blk, 10, 64) blockNumber = big.NewInt(b) block, _ = client.BlockByNumber(context.Background(), blockNumber) fmt.Printf("\n\nBlock number:\t%d\n",block.Number().Uint64()) d= time.Unix(int64(block.Time()),0) fmt.Printf("Date:\t%s\n",d) fmt.Printf("Difficulty: %d\n",block.Difficulty().Uint64()) fmt.Printf("Hash: %s\n",block.Hash().Hex()) fmt.Printf("Length: %d\n",len(block.Transactions())) }
A sample run is:
Current block: 8001026 Block number: 8001026 Difficulty: 2054659623790051 Hash: 0x791f18baa2f20d9b4ea6b52e391bb68bc3b0ef0c4d19095952fe3f49d37343ed Length: 152 Block number: 8001007 Difficulty: 2065740442592117 Hash: 0x19c7e42ccf7415e1162e3edd98793b4f6c9a2a0db5f6a010f7d9d80189244514 Length: 189