Skip to content

Commit

Permalink
feat: add common rpc package
Browse files Browse the repository at this point in the history
  • Loading branch information
gartnera committed Aug 28, 2024
1 parent 26c4fb1 commit 8e29b2b
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions pkg/rpc/rpc.go
Original file line number Diff line number Diff line change
@@ -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

Check warning on line 39 in pkg/rpc/rpc.go

View check run for this annotation

Codecov / codecov/patch

pkg/rpc/rpc.go#L30-L39

Added lines #L30 - L39 were not covered by tests
}

// 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)

Check warning on line 46 in pkg/rpc/rpc.go

View check run for this annotation

Codecov / codecov/patch

pkg/rpc/rpc.go#L43-L46

Added lines #L43 - L46 were not covered by tests
}
clientCtx := client.Context{}.WithClient(cometRPCClient)

Check warning on line 48 in pkg/rpc/rpc.go

View check run for this annotation

Codecov / codecov/patch

pkg/rpc/rpc.go#L48

Added line #L48 was not covered by tests

return newClients(clientCtx)

Check warning on line 50 in pkg/rpc/rpc.go

View check run for this annotation

Codecov / codecov/patch

pkg/rpc/rpc.go#L50

Added line #L50 was not covered by tests
}

// 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

Check warning on line 57 in pkg/rpc/rpc.go

View check run for this annotation

Codecov / codecov/patch

pkg/rpc/rpc.go#L54-L57

Added lines #L54 - L57 were not covered by tests
}
clientCtx := client.Context{}.WithGRPCClient(grpcConn)
return newClients(clientCtx)

Check warning on line 60 in pkg/rpc/rpc.go

View check run for this annotation

Codecov / codecov/patch

pkg/rpc/rpc.go#L59-L60

Added lines #L59 - L60 were not covered by tests
}

0 comments on commit 8e29b2b

Please sign in to comment.