openethereum-node-status/pkg/rpc/client.go

43 lines
858 B
Go

package rpc
import (
"fmt"
"github.com/ybbus/jsonrpc/v2"
)
type RpcClient struct {
ethClient jsonrpc.RPCClient
}
func NewRpcClient(endpoint string) RpcClient {
return RpcClient{
ethClient: jsonrpc.NewClient(endpoint),
}
}
func (r *RpcClient) EthRpcCall(method string) (*jsonrpc.RPCResponse, error) {
response, err := r.ethClient.Call(method)
if err != nil {
return nil, err
}
if response.Error != nil {
return nil, fmt.Errorf("rpc client error: (code %d) %s", response.Error.Code, response.Error.Message)
}
return response, nil
}
func (r *RpcClient) EthBatchRpcCall(batchedMethods jsonrpc.RPCRequests) (map[int]*jsonrpc.RPCResponse, error) {
response, _ := r.ethClient.CallBatch(batchedMethods)
if response.HasError() {
return nil, fmt.Errorf("batch rpc request failed")
}
results := response.AsMap()
return results, nil
}