Ethereum Transactions: Querying Blocks, Transactions, and Golang Development Guide (Part 2)

·

Introduction

This guide explores Ethereum blockchain interactions using Golang, covering block/transaction queries and ETH transfers. Updated for the latest Golang Ethereum protocol.


Querying Ethereum Blocks

// Connect to Ethereum testnet (local nodes may lack blocks)
client, _ := ethclient.Dial("https://cloudflare-eth.com")

// Fetch block header
HeadBlock, _ := client.BlockByNumber(context.Background(), nil)
logrus.Info("Query BlockHead:", HeadBlock.Number().String())

// Retrieve full block data
NewBlockNumber, _ := client.BlockByNumber(context.Background(), HeadBlock.Number())
logrus.Info("Block Address:", NewBlockNumber.Hash().Hex())

// Get transaction count
QueryCount, _ := client.TransactionCount(context.Background(), NewBlockNumber.Hash())
logrus.Info("Block TransactionCount:", QueryCount)

Key Terms: Block header, transaction count, block hash


Querying ETH Transactions

// Fetch transaction details
txHash := common.HexToHash("0x5d49fcaa394c97ec8a9c3e7bd9e8388d420fb050a52083ca52ff24b3b65bc9c2")
tx, isPending, _ := client.TransactionByHash(context.Background(), txHash)
logrus.Info(tx.Hash().String(), ", is pending:", isPending)

Common Pitfalls

  1. tx.AsMessage Deprecation:
    The method requires a Signer parameter. Use:

    types.LatestSignerForChainID(ChanID) // ChanID = network chain ID
  2. Alternative methods like LatestSigner require config parameters and are less flexible.

👉 Explore advanced transaction parsing


ETH Account Transfers

// Local node example
client, _ := ethclient.Dial("http://127.0.0.1:8545")

// Load private key (exclude 0x)
privateKey, _ := crypto.HexToECDSA("7c93ba33296e75fa18cb0ac13bab727273f0f213cd331ac0b16a7dd86e4a390d")
publicKeyECDSA := privateKey.Public().(*ecdsa.PublicKey)
PublicAddress := crypto.PubkeyToAddress(*publicKeyECDSA)

// Prepare transaction
Nonce, _ := client.PendingNonceAt(context.Background(), PublicAddress)
value := big.NewInt(1000000000000000000) // 1 ETH
gasLimit := uint64(21000)
gasPricer, _ := client.SuggestGasPrice(context.Background())
ToAddress := common.HexToAddress("0xE280029a7867BA5C9154434886c241775ea87e53")

// Create transaction (LegacyTx format)
tx := types.NewTx(&types.LegacyTx{
  Nonce:    Nonce,
  GasPrice: gasPricer,
  Gas:      gasLimit,
  To:       &ToAddress,
  Value:    value,
  Data:     nil,
})

// Sign and broadcast
ChanID, _ := client.NetworkID(context.Background())
SignedTx, _ := types.SignTx(tx, types.LatestSignerForChainID(ChanID), privateKey)
client.SendTransaction(context.Background(), SignedTx)
logrus.Info("Transaction Success:", SignedTx.Hash().Hex())

Note:


FAQ

1. How do I check if a transaction succeeded?

Query the receipt:

receipt, _ := client.TransactionReceipt(context.Background(), tx.Hash())
logrus.Info("Status:", receipt.Status == 1) // 1 = success

2. What’s the difference between BlockByNumber and BlockByHash?

3. Why use LatestSignerForChainID?

It supports EIP-155 replay protection and is backward-compatible with older transaction types.

👉 Master Ethereum development


Up next: Part 3 covering smart contract interactions and advanced queries.