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

43 lines
858 B
Go
Raw Normal View History

2021-12-14 12:24:18 +01:00
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 {
2021-12-14 15:50:07 +01:00
return nil, err
2021-12-14 12:24:18 +01:00
}
if response.Error != nil {
2021-12-14 15:50:07 +01:00
return nil, fmt.Errorf("rpc client error: (code %d) %s", response.Error.Code, response.Error.Message)
2021-12-14 12:24:18 +01:00
}
return response, nil
}
2021-12-14 15:50:07 +01:00
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
}