ethclient

package module
v0.0.0-...-5e373e0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 31, 2023 License: MIT Imports: 14 Imported by: 0

README

failover-ethclient

ethclient but with a failover RPC endpoint + prometheus metrics

Note

Hobby project, failover criteria is immature.

Usage

Environment variables:

- ETHEREUM_RPCNAME=nodereal
- ETHEREUM_RPCURL=https://eth-mainnet.nodereal.io/v1/<omitted>
- ETHEREUM_FAILOVERRPCNAME=alchemy
- ETHEREUM_FAILOVERRPCURL=https://eth-mainnet.g.alchemy.com/v2/<omitted>

Code:

cfg := ethclient.ConfigFromEnvPrefix("ethereum")
ethClient, err := ethclient.New("my-app", "ethereum", cfg)
if err != nil {
    log.Fatal().Err(err).Msg("failed to init ethclient")
}

You'll then be able to query the following metrics:

rpc_request_total{app="my-app", success="true", chain="ethereum", client="nodereal"}
rpc_request_total{app="my-app", success="false", chain="ethereum",  client="nodereal"}
rpc_request_total{app="my-app", success="true", chain="ethereum", client="alchemy"}
rpc_request_total{app="my-app", success="false", chain="ethereum",  client="alchemy"}

Documentation

Index

Constants

View Source
const (
	DefaultEnvPrefix = "ethclient"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client interface {
	bind.ContractBackend
	ethereum.ChainReader
	ethereum.TransactionReader
	ethereum.ChainStateReader
	ethereum.ChainSyncReader
	ethereum.ContractCaller
	ethereum.LogFilterer
	ethereum.TransactionSender
	ethereum.GasPricer
	ethereum.PendingStateReader
	ethereum.PendingContractCaller
	ethereum.GasEstimator
}

func New

func New(
	appName string,
	chain string,
	cfg *Config,
) (Client, error)

type Config

type Config struct {
	EnablePrometheus bool   `default:"true"`
	RpcUrl           string `required:"true"`
	RpcName          string `required:"true"`
	FailoverRpcUrl   string `required:"true"`
	FailoverRpcName  string `required:"true"`
}

func ConfigFromEnv

func ConfigFromEnv() *Config

func ConfigFromEnvPrefix

func ConfigFromEnvPrefix(prefix string) *Config

func (*Config) Valid

func (c *Config) Valid() error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL