[ Log On ]
  • Home
  • Tst
  • Cha
  • Enc
  • Code
  • IP
  • Fun
  • Sub
  • DigF
  • Cis
  • Com
  • Db
  • About
  • Netsim
  • Big Data

Ethereum Block in Go

[Back] We can use Go to access the Ethereum blocks. The following shows the details of the first five transactions in a given bloc:

Parameters

Ethereum Block Number:

Samples

Here is Block 1 [link].

Here is Block 8001575 [link].

Examples

  • Try "Genesis block (1)". Try!,
  • Try "Block 100". Try!,
  • Try "Block 101". Try!,
  • Try "Block 8001575". Try!,

Theory

package main

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

func main() {


    argCount := len(os.Args[1:])


    client, _ := ethclient.Dial("https://mainnet.infura.io")


    header, _ := client.HeaderByNumber(context.Background(), nil)
    fmt.Printf("Current block: %s\n\n",header.Number.String())
    
    blk:="1"
    b,_:=strconv.ParseInt(blk, 10, 64)
    

        if argCount>0 { 
        blk=os.Args[1] 

        b,_=strconv.ParseInt(blk, 10, 64)

    }
    if argCount==0 { 
        b,_=strconv.ParseInt(header.Number.String(), 10, 64) 


    }
    
    fmt.Printf("Target block: %d\n",b)

    blockNumber := big.NewInt(b)
    block, _ := client.BlockByNumber(context.Background(), blockNumber)

    i:=0

    fmt.Printf("Listing the first five transactions ...\n\n")
    for _, tx := range block.Transactions() {
        fmt.Printf("Hash: %s\n",tx.Hash().Hex())        
        fmt.Printf("Value: %s\n",tx.Value().String())    
        fmt.Printf("Gas: %d\n",tx.Gas())               
        fmt.Printf("Gas price: %d\n",tx.GasPrice().Uint64()) 
        fmt.Printf("Nonce: %d\n",tx.Nonce())             
        fmt.Printf("Tx: %s\n",tx.Data())              
        fmt.Printf("Tx: %s\n\n",tx.To().Hex())  
        i++
        if (i>5) { break }        


    }


}

A sample run is:

Current block: 8001550

Target block: 8001550
Listing the first five transactions ...

Hash: 0x0af855730aefbe90b29c22c9baa3deededdde56e008ce91b5c90b6dcea221f98
Value: 0
Gas: 120000
Gas price: 50000000000
Nonce: 31
Tx: ©œ»������������U±˜ˆ¾ÌɧÞEœOq».æ����������������������åš©KŒD7¨�
Tx: 0x8290333ceF9e6D528dD5618Fb97a76f268f3EDD4

Hash: 0x804d77148a44e41cd612033456afee631b9c178d4738b30279be37c3a8ddce40
Value: 0
Gas: 4300000
Gas price: 42000000000
Nonce: 24325
Tx: ©œ»������������Ï”ÀÉÖãa´­d™0Ic�‚¨í����������������������������Í)À
Tx: 0xd0929d411954c47438dc1d871dd6081F5C5e149c

Hash: 0x6e475b1b7336aebc28abd9ab15dc91838849aa2faf79b74b89caf08a45aa316f
Value: 24488163890000000000
Gas: 90000
Gas price: 40000000000
Nonce: 120921
Tx: 
Tx: 0x104C42fDe4b9DF8761CDabEb2bd890C650c2bC24

Hash: 0x9ff35bf9ecb8a3f323f6011b022fa96dcb3303a3171fd3e63e07822e6b28d23e
Value: 0
Gas: 106294
Gas price: 40000000000
Nonce: 1500977
Tx: ©œ»������������§Åñ)íC¡YÍÙò\o”ŸÀ]p�����������������������‘
÷%W÷€�
Tx: 0x1D287CC25dAD7cCaF76a26bc660c5F7C8E2a05BD

Hash: 0x04651e0e2632f00b99731b6ee4e0bff872b86f3a1fb9c783d601f93e6ecd1bdc
Value: 2595000000000000000
Gas: 90000
Gas price: 22900000000
Nonce: 80290
Tx: 
Tx: 0x52C9869e491AB3345480bd720382F31bF9dACbbC

Hash: 0x72c4cd980f57c8d1090a978705870f7fed5159bff3b318d7ede46fce94165f3f
Value: 2503252000000000000
Gas: 90000
Gas price: 22900000000
Nonce: 403902
Tx: 
Tx: 0xbd8D2dB142e1b34D944aC578693BE0d890e314a8