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 response, err
	}

	if response.Error != nil {
		return response, fmt.Errorf("rpc client error: (code %d) %s", response.Error.Code, response.Error.Message)
	}

	return response, nil
}