Ethereum Block Scanning: How to Retrieve Transaction Records from Blocks

·

Initializing the Client

Initializing an Ethereum client in Go is the foundational step for interacting with the blockchain. Start by importing the ethclient package from go-ethereum and initialize it using the Dial method, which requires the URL of a blockchain service provider.

If you don't have an existing Ethereum client, you can connect to a public node like Infura:

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

Complete Initialization Code

package main

import (
    "fmt"
    "log"
    "github.com/ethereum/go-ethereum/ethclient"
)

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

Retrieving the Latest Block Height

To fetch the latest block height, use HeaderByNumber:

header, err := client.HeaderByNumber(context.Background(), nil)
if err != nil {
    log.Fatal(err)
}
fmt.Println(header.Number.String()) // Latest block number

Fetching a Complete Block

Call BlockByNumber to retrieve a full block, including metadata such as:

blockNumber := big.NewInt(5671744)
block, err := client.BlockByNumber(context.Background(), blockNumber)
if err != nil {
    log.Fatal(err)
}
fmt.Println(block.Number().Uint64())         // Block number (e.g., 5671744)
fmt.Println(block.Time().Uint64())          // Block timestamp
fmt.Println(block.Difficulty().Uint64())    // Mining difficulty
fmt.Println(block.Hash().Hex())             // Block hash
fmt.Println(len(block.Transactions()))      // Number of transactions (e.g., 144)

Extracting Transaction Details

Loop through the transactions in a block to retrieve details such as:

for _, tx := range block.Transactions() {
    fmt.Println(tx.Hash().Hex())        // Transaction hash
    fmt.Println(tx.Value().String())    // Amount transferred (in Wei)
    fmt.Println(tx.Gas())               // Gas limit
    fmt.Println(tx.GasPrice().Uint64()) // Gas price (in Wei)
    fmt.Println(tx.Nonce())            // Nonce
    fmt.Println(tx.To().Hex())          // Recipient address
}

Retrieving Sender Addresses

To obtain the sender address (from), use AsMessage with an EIP-155 signer:

chainID, err := client.NetworkID(context.Background())
if err != nil {
    log.Fatal(err)
}
if msg, err := tx.AsMessage(types.NewEIP155Signer(chainID)); err != nil {
    fmt.Println(msg.From().Hex()) // Sender address
}

👉 Explore Ethereum transaction scanning tools

FAQs

How do I scan multiple blocks efficiently?

Use a loop to iterate through a range of block numbers and store transaction data in batches. Optimize by using goroutines for concurrent processing.

Can I filter transactions by sender or recipient?

Yes, parse each transaction and apply conditional checks to match specific sender/recipient addresses.

What’s the best way to store scanned transactions?

Use a database like PostgreSQL or MongoDB for structured storage. Batch inserts improve performance.

👉 Learn advanced blockchain data techniques