ethrpc

package module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2024 License: MIT Imports: 11 Imported by: 0

README

GoDoc

ethrpc

Simple go lib to make RPC calls to Ethereum-like nodes easy

Example use

    target := ethrpc.New("https://cloudflare-eth.com")
    currentBlockNo, err := ethrpc.ReadUint64(target.Do("eth_blockNumber"))

TODO

  • Support websocket

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoAvailableServer = errors.New("no available server")
)

Functions

func ReadBigInt added in v0.1.1

func ReadBigInt(v json.RawMessage, e error) (*big.Int, error)

ReadBigInt can decode a json-encoded bigint in various ways, including if it is a number literal or a string.

func ReadString

func ReadString(v json.RawMessage, e error) (string, error)

ReadString decodes the return value as a string and returns it

func ReadTo

func ReadTo(target any) func(v json.RawMessage, e error) error

ReadTo returns a setter function that will return an error if an error happens. This is a bit convoluted because of limitation in Go's syntax, but this could be used as:

err = ReadTo(&block)(target.Do("eth_getBlockByNumber", "0x1b4", true))

func ReadUint64

func ReadUint64(v json.RawMessage, e error) (uint64, error)

ReadUint64 decodes the return value and passes it as a uint64.

This can be used as: res, err := ReadUint64(target.Do("eth_blockNumber"))

Types

type Api added in v0.1.4

type Api struct {
	Handler
}

func (*Api) BlockNumber added in v0.1.4

func (a *Api) BlockNumber(ctx context.Context) (uint64, error)

func (*Api) ChainId added in v0.1.4

func (a *Api) ChainId(ctx context.Context) (uint64, error)

func (*Api) Do added in v0.1.4

func (a *Api) Do(method string, args ...any) (json.RawMessage, error)

func (*Api) To added in v0.1.4

func (a *Api) To(target any, method string, args ...any) error

func (*Api) ToCtx added in v0.1.4

func (a *Api) ToCtx(ctx context.Context, target any, method string, args ...any) error

type Handler added in v0.1.4

type Handler interface {
	DoCtx(ctx context.Context, method string, args ...any) (json.RawMessage, error)
}

func Evaluate added in v0.1.4

func Evaluate(ctx context.Context, servers ...string) (Handler, error)

Evaluate will call the various servers in the list and return a list of servers that work (if any)

This will send a eth_blockNumber request to all the servers and measure the response time

type RPC

type RPC struct {
	// contains filtered or unexported fields
}

TODO support ws protocol

func New

func New(h string) *RPC

New returns a new instance of RPC to perform requests to the given RPC endpoint

func (*RPC) Do

func (r *RPC) Do(method string, args ...any) (json.RawMessage, error)

Do performs a RPC request

func (*RPC) DoCtx

func (r *RPC) DoCtx(ctx context.Context, method string, args ...any) (json.RawMessage, error)

DoCtx performs a RPC request, taking an optional context that can be cancelled to stop the request

func (*RPC) To

func (r *RPC) To(target any, method string, args ...any) error

type RPCList added in v0.1.4

type RPCList []*RPC

func (RPCList) DoCtx added in v0.1.4

func (r RPCList) DoCtx(ctx context.Context, method string, args ...any) (json.RawMessage, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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