### Installing go-eth2-client using go get Source: https://github.com/attestantio/go-eth2-client/blob/master/README.md This command installs the go-eth2-client library using the go get command. It fetches the module and its dependencies, making it available for use in Go projects. ```Shell go get github.com/attestantio/go-eth2-client ``` -------------------------------- ### Accessing a Beacon Node with go-eth2-client Source: https://github.com/attestantio/go-eth2-client/blob/master/README.md This example demonstrates how to connect to a beacon node, retrieve genesis information, and properly close the client connection. It showcases the use of the http package for creating a client, setting options like address and log level, and handling potential API errors. ```Go 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() } ``` -------------------------------- ### Fetching validators with new API style in Go Source: https://github.com/attestantio/go-eth2-client/blob/master/docs/0.19.0-changes.md This code snippet demonstrates the new way of fetching validators using the go-eth2-client, which involves passing parameters within a struct. This approach improves clarity and maintainability as the API evolves. ```go validatorsProvider.Validators(ctx, &api.ValidatorsOpts{ State: "head", }) ``` -------------------------------- ### Fetching validators with old API style in Go Source: https://github.com/attestantio/go-eth2-client/blob/master/docs/0.19.0-changes.md This code snippet demonstrates the old way of fetching validators using the go-eth2-client, which involves passing parameters directly, including nil values. This approach can be unclear and difficult to maintain as the API evolves. ```go validatorsProvider.Validators(ctx, "head", nil) ``` -------------------------------- ### Handling errors with old API style in Go Source: https://github.com/attestantio/go-eth2-client/blob/master/docs/0.19.0-changes.md This code snippet demonstrates the old way of handling errors in the go-eth2-client, which involves checking for both a nil error and a nil result. This approach can be cumbersome and error-prone. ```go block, err := beaconBlockProvider.SignedBeaconBlock(ctx, "999999999999") if err != nil { panic(err) } if block == nil { panic("no block for this slot") } ... ``` -------------------------------- ### Handling errors with new API style in Go Source: https://github.com/attestantio/go-eth2-client/blob/master/docs/0.19.0-changes.md This code snippet demonstrates the new way of handling errors in the go-eth2-client, which involves checking for an error and then inspecting the error type. This approach provides better control of both successful and failed calls. ```go response, err := beaconBlockProvider.SignedBeaconBlock(ctx, &api.SignedBeaconBlockOpts{ Block: "999999999999", }) if err != nil { var apiErr *api.Error if errors.As(err, &apiErr) { switch apiErr.StatusCode { case 404: // No block found. case 503: // Node is syncing. } } panic(err) } // At this point response will definitely not be nil. ... ``` -------------------------------- ### Accessing metadata from response in Go Source: https://github.com/attestantio/go-eth2-client/blob/master/docs/0.19.0-changes.md This code snippet demonstrates how to access metadata from the response struct in the go-eth2-client. The metadata provides additional information about the response, such as whether a block is finalized. ```go response, _ := beaconBlockProvider.SignedBeaconBlock(ctx, &api.SignedBeaconBlockOpts{ Block: "123", }) finalized, exists := response.Metadata[metadata.Finalized] if exists { fmt.Printf("Is block finalized? %b\n",finalized) } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.