go-ethereum

Connect to 256 Blocks using the official Go Ethereum client

go-ethereum (geth) is the official Go implementation of the Ethereum protocol. The ethclient package provides a client for interacting with Ethereum nodes via JSON-RPC.

Installation

go get github.com/ethereum/go-ethereum

Quick Start

Connect to 256 Blocks using the X-API-Key header with a custom HTTP transport:

package main
 
import (
    "context"
    "fmt"
    "log"
    "net/http"
 
    "github.com/ethereum/go-ethereum/ethclient"
    "github.com/ethereum/go-ethereum/rpc"
)
 
// Custom HTTP transport that adds the API key header
type apiKeyTransport struct {
    apiKey string
    base   http.RoundTripper
}
 
func (t *apiKeyTransport) RoundTrip(req *http.Request) (*http.Response, error) {
    req.Header.Set("X-API-Key", t.apiKey)
    return t.base.RoundTrip(req)
}
 
func main() {
    // Create custom HTTP client with API key header
    httpClient := &http.Client{
        Transport: &apiKeyTransport{
            apiKey: "your-api-key",
            base:   http.DefaultTransport,
        },
    }
 
    // Create RPC client with custom HTTP client
    rpcClient, err := rpc.DialOptions(
        context.Background(),
        "https://rpc.256blocks.com/ethereum",
        rpc.WithHTTPClient(httpClient),
    )
    if err != nil {
        log.Fatal(err)
    }
 
    // Create ethclient from RPC client
    client := ethclient.NewClient(rpcClient)
 
    // Get the latest block number
    blockNumber, err := client.BlockNumber(context.Background())
    if err != nil {
        log.Fatal(err)
    }
 
    fmt.Printf("Latest block: %d\n", blockNumber)
}

Reading Account Balance

package main
 
import (
    "context"
    "fmt"
    "log"
    "math/big"
    "net/http"
 
    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/ethclient"
    "github.com/ethereum/go-ethereum/rpc"
)
 
type apiKeyTransport struct {
    apiKey string
    base   http.RoundTripper
}
 
func (t *apiKeyTransport) RoundTrip(req *http.Request) (*http.Response, error) {
    req.Header.Set("X-API-Key", t.apiKey)
    return t.base.RoundTrip(req)
}
 
func main() {
    httpClient := &http.Client{
        Transport: &apiKeyTransport{
            apiKey: "your-api-key",
            base:   http.DefaultTransport,
        },
    }
 
    rpcClient, err := rpc.DialOptions(
        context.Background(),
        "https://rpc.256blocks.com/ethereum",
        rpc.WithHTTPClient(httpClient),
    )
    if err != nil {
        log.Fatal(err)
    }
 
    client := ethclient.NewClient(rpcClient)
 
    // Get account balance
    address := common.HexToAddress("0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb")
    balance, err := client.BalanceAt(context.Background(), address, nil)
    if err != nil {
        log.Fatal(err)
    }
 
    // Convert wei to ether
    fbalance := new(big.Float).SetInt(balance)
    ethValue := new(big.Float).Quo(fbalance, big.NewFloat(1e18))
 
    fmt.Printf("Balance: %s ETH\n", ethValue.String())
}

Reading Contract Data

package main
 
import (
    "context"
    "fmt"
    "log"
    "math/big"
    "net/http"
    "strings"
 
    "github.com/ethereum/go-ethereum"
    "github.com/ethereum/go-ethereum/accounts/abi"
    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/ethclient"
    "github.com/ethereum/go-ethereum/rpc"
)
 
type apiKeyTransport struct {
    apiKey string
    base   http.RoundTripper
}
 
func (t *apiKeyTransport) RoundTrip(req *http.Request) (*http.Response, error) {
    req.Header.Set("X-API-Key", t.apiKey)
    return t.base.RoundTrip(req)
}
 
// ERC-20 ABI (minimal)
const erc20ABI = `[{"constant":true,"inputs":[{"name":"account","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"type":"function"}]`
 
func main() {
    httpClient := &http.Client{
        Transport: &apiKeyTransport{
            apiKey: "your-api-key",
            base:   http.DefaultTransport,
        },
    }
 
    rpcClient, err := rpc.DialOptions(
        context.Background(),
        "https://rpc.256blocks.com/ethereum",
        rpc.WithHTTPClient(httpClient),
    )
    if err != nil {
        log.Fatal(err)
    }
 
    client := ethclient.NewClient(rpcClient)
 
    // Parse the ABI
    parsedABI, err := abi.JSON(strings.NewReader(erc20ABI))
    if err != nil {
        log.Fatal(err)
    }
 
    // USDC contract on Ethereum
    contractAddress := common.HexToAddress("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48")
    accountAddress := common.HexToAddress("0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb")
 
    // Pack the function call data
    data, err := parsedABI.Pack("balanceOf", accountAddress)
    if err != nil {
        log.Fatal(err)
    }
 
    // Make the call
    result, err := client.CallContract(context.Background(), ethereum.CallMsg{
        To:   &contractAddress,
        Data: data,
    }, nil)
    if err != nil {
        log.Fatal(err)
    }
 
    // Unpack the result
    balance := new(big.Int).SetBytes(result)
    fmt.Printf("USDC Balance: %s\n", balance.String())
}

Multi-Chain Support

Connect to different chains by changing the endpoint:

package main
 
import (
    "context"
    "net/http"
 
    "github.com/ethereum/go-ethereum/ethclient"
    "github.com/ethereum/go-ethereum/rpc"
)
 
type apiKeyTransport struct {
    apiKey string
    base   http.RoundTripper
}
 
func (t *apiKeyTransport) RoundTrip(req *http.Request) (*http.Response, error) {
    req.Header.Set("X-API-Key", t.apiKey)
    return t.base.RoundTrip(req)
}
 
func createClient(chain, apiKey string) (*ethclient.Client, error) {
    httpClient := &http.Client{
        Transport: &apiKeyTransport{
            apiKey: apiKey,
            base:   http.DefaultTransport,
        },
    }
 
    rpcClient, err := rpc.DialOptions(
        context.Background(),
        "https://rpc.256blocks.com/"+chain,
        rpc.WithHTTPClient(httpClient),
    )
    if err != nil {
        return nil, err
    }
 
    return ethclient.NewClient(rpcClient), nil
}
 
func main() {
    // Create clients for different chains
    ethClient, _ := createClient("ethereum", "your-api-key")
    baseClient, _ := createClient("base", "your-api-key")
    arbClient, _ := createClient("arbitrum", "your-api-key")
    polygonClient, _ := createClient("polygon", "your-api-key")
 
    _ = ethClient
    _ = baseClient
    _ = arbClient
    _ = polygonClient
}

Resources

go-ethereum | 256 Blocks