From 8e29b2b1b034666d807bf2848a0d97a86acf38e5 Mon Sep 17 00:00:00 2001 From: Alex Gartner Date: Wed, 28 Aug 2024 13:16:11 -0700 Subject: [PATCH] feat: add common rpc package --- pkg/rpc/rpc.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 pkg/rpc/rpc.go diff --git a/pkg/rpc/rpc.go b/pkg/rpc/rpc.go new file mode 100644 index 0000000000..6e7d2b84f6 --- /dev/null +++ b/pkg/rpc/rpc.go @@ -0,0 +1,61 @@ +package rpc + +import ( + "fmt" + + rpcclient "github.com/cometbft/cometbft/rpc/client/http" + "github.com/cosmos/cosmos-sdk/client" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + "google.golang.org/grpc" + + authoritytypes "github.com/zeta-chain/zetacore/x/authority/types" + crosschaintypes "github.com/zeta-chain/zetacore/x/crosschain/types" + fungibletypes "github.com/zeta-chain/zetacore/x/fungible/types" + lightclienttypes "github.com/zeta-chain/zetacore/x/lightclient/types" + observertypes "github.com/zeta-chain/zetacore/x/observer/types" +) + +// Clients contains RPC client interfaces to interact with zetacored +type Clients struct { + AuthorityClient authoritytypes.QueryClient + CctxClient crosschaintypes.QueryClient + FungibleClient fungibletypes.QueryClient + AuthClient authtypes.QueryClient + BankClient banktypes.QueryClient + ObserverClient observertypes.QueryClient + LightClient lightclienttypes.QueryClient +} + +func newClients(ctx client.Context) (Clients, error) { + return Clients{ + AuthorityClient: authoritytypes.NewQueryClient(ctx), + CctxClient: crosschaintypes.NewQueryClient(ctx), + FungibleClient: fungibletypes.NewQueryClient(ctx), + AuthClient: authtypes.NewQueryClient(ctx), + BankClient: banktypes.NewQueryClient(ctx), + ObserverClient: observertypes.NewQueryClient(ctx), + LightClient: lightclienttypes.NewQueryClient(ctx), + }, nil +} + +// NewCometBFTClients creates a Clients which uses cometbft abci_query as the transport +func NewCometBFTClients(url string) (Clients, error) { + cometRPCClient, err := rpcclient.New(url, "/websocket") + if err != nil { + return Clients{}, fmt.Errorf("create cometbft rpc client: %w", err) + } + clientCtx := client.Context{}.WithClient(cometRPCClient) + + return newClients(clientCtx) +} + +// NewGRPCClient creates a Clients which uses gRPC as the transport +func NewGRPClients(url string, opts ...grpc.DialOption) (Clients, error) { + grpcConn, err := grpc.Dial(url, opts...) + if err != nil { + return Clients{}, err + } + clientCtx := client.Context{}.WithGRPCClient(grpcConn) + return newClients(clientCtx) +}