MCPcopy Index your code
hub / github.com/attestantio/go-eth2-client

github.com/attestantio/go-eth2-client @v0.28.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.28.1 ↗ · + Follow
3,277 symbols 9,913 edges 894 files 2,758 documented · 84%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

go-eth2-client

Tag License GoDoc Lint Go Report Card

Go library providing an abstraction to multiple Ethereum 2 beacon nodes. Its external API follows the official Ethereum beacon APIs specification.

This library is under development; expect APIs and data structures to change until it reaches version 1.0. In addition, clients' implementations of both their own and the standard API are themselves under development so implementation of the full API can be incomplete.

Between versions 0.18.0 and 0.19.0 the API has undergone a number of changes. Please see the detailed documentation regarding these changes.

Table of Contents

Install

go-eth2-client is a standard Go module which can be installed with:

go get github.com/attestantio/go-eth2-client

Support

go-eth2-client supports beacon nodes that comply with the standard beacon node API. To date it has been tested against the following beacon nodes:

Usage

Please read the Go documentation for this library for interface information.

Example

Below is a complete annotated example to access a beacon node.

package main

import (
    "context"
    "fmt"

    eth2client "github.com/attestantio/go-eth2-client"
    "github.com/attestantio/go-eth2-client/api"
    "github.com/attestantio/go-eth2-client/http"
    "github.com/rs/zerolog"
)

func main() {
    // Provide a cancellable context to the creation function.
    ctx, cancel := context.WithCancel(context.Background())
    client, err := http.New(ctx,
        // WithAddress supplies the address of the beacon node, as a URL.
        http.WithAddress("http://localhost:5052/"),
        // LogLevel supplies the level of logging to carry out.
        http.WithLogLevel(zerolog.WarnLevel),
    )
    if err != nil {
        panic(err)
    }

    fmt.Printf("Connected to %s\n", client.Name())

    // Client functions have their own interfaces.  Not all functions are
    // supported by all clients, so checks should be made for each function when
    // casting the service to the relevant interface.
    if provider, isProvider := client.(eth2client.GenesisProvider); isProvider {
        genesisResponse, err := provider.Genesis(ctx, &api.GenesisOpts{})
        if err != nil {
            // Errors may be API errors, in which case they will have more detail
            // about the failure.
            var apiErr *api.Error
            if errors.As(err, &apiErr) {
                switch apiErr.StatusCode {
                  case 404:
                    panic("genesis not found")
                  case 503:
                    panic("node is syncing")
                }
            }
            panic(err)
        }
        fmt.Printf("Genesis time is %v\n", genesisResponse.Data.GenesisTime)
    }

    // You can also access the struct directly if required.
    httpClient := client.(*http.Service)
    genesisResponse, err := httpClient.Genesis(ctx, &api.GenesisOpts{})
    if err != nil {
        panic(err)
    }
    fmt.Printf("Genesis validators root is %s\n", genesisResponse.Data.GenesisValidatorsRoot)

    // Cancelling the context passed to New() frees up resources held by the
    // client, closes connections, clears handlers, etc.
    cancel()
}

Maintainers

Chris Berry: @bez625.

Contribute

Contributions welcome. Please check out the issues.

License

Apache-2.0 © 2020, 2021 Attestant Limited

Extension points exported contracts — how you extend this code

ValidatorBalancesProvider (Interface)
ValidatorBalancesProvider is the interface for providing validator balances. [6 implementers]
service.go
Parameter (Interface)
Parameter is the interface for service parameters.
multi/parameters.go
HookFunc (FuncType)
HookFunc is a function called when a hook is triggered.
http/hooks.go
Parameter (Interface)
Parameter is the interface for service parameters.
http/parameters.go
Service (Interface)
Service is the generic metrics service.
metrics/service.go
EventHandlerFunc (FuncType)
EventHandlerFunc is the handler for generic events.
api/eventsopts.go
Parameter (Interface)
Parameter is the interface for service parameters.
mock/parameters.go
Parameter (Interface)
Parameter is the interface for service parameters.
auto/parameters.go

Core symbols most depended-on inside this repo

Index
called by 296
service.go
New
called by 166
mock/service.go
WithName
called by 150
mock/parameters.go
Address
called by 134
service.go
NewErroring
called by 111
testclients/erroring.go
HashTreeRoot
called by 80
api/v1/blobs_ssz.go
decodeJSONResponse
called by 72
http/json.go
Name
called by 70
service.go

Shape

Method 2,261
Struct 470
Function 416
Interface 75
TypeAlias 32
FuncType 23

Languages

Go100%

Modules by API surface

service.go143 symbols
testclients/erroring.go64 symbols
testclients/sleepy.go55 symbols
spec/versionedbeaconstate.go26 symbols
spec/versionedsignedbeaconblock.go25 symbols
api/v1/payloadattributesevent.go24 symbols
api/versionedproposal.go22 symbols
spec/versionedexecutionpayload.go20 symbols
http/parameters.go19 symbols
http/events.go19 symbols
api/eventsopts.go17 symbols
spec/versionedbeaconblock.go15 symbols

For agents

$ claude mcp add go-eth2-client \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page