rpcbackend

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Backend

type Backend interface {
	BatchRPC
	SyncRequest(ctx context.Context, rpcReq *RPCRequest) (rpcRes *RPCResponse, err error)
}

Backend performs communication with a backend

func NewRPCClient

func NewRPCClient(client *resty.Client) Backend

NewRPCClient Constructor

func NewRPCClientWithOption

func NewRPCClientWithOption(client *resty.Client, options RPCClientOptions) Backend

NewRPCClientWithOption Constructor

type BatchRPC

type BatchRPC interface {
	RPC
	CallRPCBatch(ctx context.Context, ops ...*RPCBatchOp) []*RPCError
}

type RPC

type RPC interface {
	CallRPC(ctx context.Context, result interface{}, method string, params ...interface{}) *RPCError
}

type RPCBatchOp

type RPCBatchOp struct {
	Result interface{}
	Method string
	Params []interface{}
}

RPCBatchOp holds the method, params, and result destination for one call within a batch.

type RPCBatchOpTyped

type RPCBatchOpTyped[T any] struct {
	Result *T
	Method string
	Params []interface{}
}

RPCBatchOpTyped is the typed equivalent of RPCBatchOp for use with CallRPCBatchTyped. All ops in a typed batch must share the same result type T.

type RPCClient

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

func (*RPCClient) CallRPC

func (rc *RPCClient) CallRPC(ctx context.Context, result interface{}, method string, params ...interface{}) *RPCError

func (*RPCClient) CallRPCBatch

func (rc *RPCClient) CallRPCBatch(ctx context.Context, ops ...*RPCBatchOp) []*RPCError

func (*RPCClient) SyncRequest

func (rc *RPCClient) SyncRequest(ctx context.Context, rpcReq *RPCRequest) (*RPCResponse, error)

SyncRequest sends an individual RPC request to the backend (always over HTTP currently), and waits synchronously for the response, or an error.

In all return paths *including error paths* the RPCResponse is populated so the caller has an RPC structure to send back to the front-end caller.

type RPCClientOptions

type RPCClientOptions struct {
	MaxConcurrentRequest int64
}

type RPCCode

type RPCCode int64
const (
	RPCCodeParseError     RPCCode = -32700
	RPCCodeInvalidRequest RPCCode = -32600
	RPCCodeInternalError  RPCCode = -32603
)

type RPCError

type RPCError struct {
	Code    int64           `json:"code"`
	Message string          `json:"message"`
	Data    fftypes.JSONAny `json:"data,omitempty"`
}

func CallRPCBatchTyped

func CallRPCBatchTyped[T any](ctx context.Context, rc *RPCClient, ops ...*RPCBatchOpTyped[T]) []*RPCError

CallRPCBatchTyped sends a JSON-RPC batch where every operation decodes its result directly into T, bypassing the fftypes.JSONAny intermediate used by CallRPCBatch. All ops in the batch must share the same result type.

func CallRPCTyped

func CallRPCTyped[T any](ctx context.Context, rc *RPCClient, result *T, method string, params ...interface{}) *RPCError

CallRPCTyped performs a single JSON-RPC call and decodes the result directly into T. More efficient than parsing into generic JSON bytes result structure first.

func NewRPCError

func NewRPCError(ctx context.Context, code RPCCode, msg i18n.ErrorMessageKey, inserts ...interface{}) *RPCError

func (*RPCError) Error

func (e *RPCError) Error() error

func (*RPCError) String

func (e *RPCError) String() string

type RPCRequest

type RPCRequest struct {
	JSONRpc string             `json:"jsonrpc"`
	ID      *fftypes.JSONAny   `json:"id"`
	Method  string             `json:"method"`
	Params  []*fftypes.JSONAny `json:"params,omitempty"`
}

type RPCResponse

type RPCResponse = RPCResponseTyped[*fftypes.JSONAny]

RPCResponse is the standard JSON-RPC response type, where Result is captured as raw bytes in a fftypes.JSONAny for a second json.Unmarshal by the caller.

func RPCErrorResponse

func RPCErrorResponse(err error, id *fftypes.JSONAny, code RPCCode) *RPCResponse

type RPCResponseTyped

type RPCResponseTyped[T any] struct {
	JSONRpc string           `json:"jsonrpc"`
	ID      *fftypes.JSONAny `json:"id"`
	Result  T                `json:"result,omitempty"`
	Error   *RPCError        `json:"error,omitempty"`
	// Only for subscription notifications
	Method string           `json:"method,omitempty"`
	Params *fftypes.JSONAny `json:"params,omitempty"`
}

RPCResponseTyped is a JSON-RPC response envelope that decodes Result directly into T, bypassing the fftypes.JSONAny intermediate used by RPCResponse.

func (*RPCResponseTyped[T]) Message

func (r *RPCResponseTyped[T]) Message() string

type RPCSubscriptionNotification

type RPCSubscriptionNotification struct {
	CurrentSubID string // will change on each reconnect
	Result       *fftypes.JSONAny
}

type Subscription

type Subscription interface {
	LocalID() *fftypes.UUID // does not change through reconnects
	Notifications() chan *RPCSubscriptionNotification
	Unsubscribe(ctx context.Context) *RPCError
}

type WebSocketRPCClient

type WebSocketRPCClient interface {
	RPC
	Subscribe(ctx context.Context, params ...interface{}) (sub Subscription, error *RPCError)
	Subscriptions() []Subscription
	UnsubscribeAll(ctx context.Context) (error *RPCError)
	Connect(ctx context.Context) error
	Close()
}

WebSocketRPCClient performs communication over a websocket with an Ethereum JSON/RPC endpoint

- Manages websocket connect/reconnect with keepalive etc. - Manages subscriptions with a local ID, so they re-established automatically after reconnect - Allows synchronous exchange over the WebSocket so you don't have to maintain a separate HTTP connection too

func NewWSRPCClient

func NewWSRPCClient(wsConf *wsclient.WSConfig) WebSocketRPCClient

NewRPCClient Constructor

Jump to

Keyboard shortcuts

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