add: batch rpc calls

This commit is contained in:
2021-12-14 17:50:07 +03:00
parent 32ad2d84aa
commit 92797c4ea5
3 changed files with 73 additions and 34 deletions

View File

@@ -19,12 +19,24 @@ func NewRpcClient(endpoint string) RpcClient {
func (r *RpcClient) EthRpcCall(method string) (*jsonrpc.RPCResponse, error) {
response, err := r.ethClient.Call(method)
if err != nil {
return response, err
return nil, err
}
if response.Error != nil {
return response, fmt.Errorf("rpc client error: (code %d) %s", response.Error.Code, response.Error.Message)
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
}