Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: hard-keeper-bot client updates #73

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/kava-labs/go-tools

go 1.21.9
go 1.23

require (
github.com/cosmos/cosmos-sdk v0.44.5
Expand Down
134 changes: 134 additions & 0 deletions hard-keeper-bot/grpc_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package main

import (
"context"
"crypto/tls"
"fmt"
"net/url"
"strconv"

"github.com/cosmos/cosmos-sdk/client/grpc/tmservice"
sdk "github.com/cosmos/cosmos-sdk/types"
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
hardtypes "github.com/kava-labs/kava/x/hard/types"
pricefeedtypes "github.com/kava-labs/kava/x/pricefeed/types"

"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/metadata"
)

type GrpcClient struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as a team, we want to create fewer of these grpc client. we currently have many of them and they increase our maintenance overhead. overtime, they become slightly different copy-pastas of each other. can you update your code to use the canonical client from the kava repo?
https://github.com/Kava-Labs/kava/tree/master/client/grpc

i believe all of the methods included here already exist on that client. updating to use that also makes upgrading kava versions on these types of services much simpler 😄

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried updating to use kavaGrpc "github.com/kava-labs/kava/client/grpc" but am getting a plethora of dependency issues as a result. Will continue to investigate whats going on here

Conn *grpc.ClientConn
TmClient tmservice.ServiceClient
HardClient hardtypes.QueryClient
PricefeedClient pricefeedtypes.QueryClient
}

var _ LiquidationClient = (*GrpcClient)(nil)

func ctxAtHeight(height int64) context.Context {
heightStr := strconv.FormatInt(height, 10)
return metadata.AppendToOutgoingContext(context.Background(), grpctypes.GRPCBlockHeightHeader, heightStr)
}

func NewGrpcClient(target string) (*GrpcClient, error) {
grpcURL, err := url.Parse(target)
if err != nil {
return nil, fmt.Errorf("invalid URL: %w", err)
}

var dialOptions grpc.DialOption
switch grpcURL.Scheme {
case "http":
dialOptions = grpc.WithInsecure()
case "https":
dialOptions = grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{}))
default:
return nil, fmt.Errorf("unsupported scheme: %s", grpcURL.Scheme)
}

conn, err := grpc.Dial(grpcURL.Host, dialOptions)
if err != nil {
return nil, fmt.Errorf("failed to connect: %w", err)
}

return &GrpcClient{
Conn: conn,
TmClient: tmservice.NewServiceClient(conn),
HardClient: hardtypes.NewQueryClient(conn),
PricefeedClient: pricefeedtypes.NewQueryClient(conn),
}, nil
}

func (c *GrpcClient) GetInfo() (*InfoResponse, error) {
latestBlock, err := c.TmClient.GetLatestBlock(context.Background(), &tmservice.GetLatestBlockRequest{})
if err != nil {
return nil, fmt.Errorf("failed to fetch latest block: %w", err)
}

return &InfoResponse{
ChainId: latestBlock.Block.Header.ChainID,
LatestHeight: latestBlock.Block.Header.Height,
}, nil
}

func (c *GrpcClient) GetPrices(height int64) (pricefeedtypes.CurrentPrices, error) {
pricesRes, err := c.PricefeedClient.Prices(ctxAtHeight(height), &pricefeedtypes.QueryPricesRequest{})
if err != nil {
return nil, fmt.Errorf("failed to fetch prices: %w", err)
}

prices := make([]pricefeedtypes.CurrentPrice, len(pricesRes.Prices))
for i, response := range pricesRes.Prices {
prices[i] = pricefeedtypes.CurrentPrice{
MarketID: response.MarketID,
Price: response.Price,
}
}

return prices, nil
}

func (c *GrpcClient) GetMarkets(height int64) (hardtypes.MoneyMarkets, error) {
paramsRes, err := c.HardClient.Params(ctxAtHeight(height), &hardtypes.QueryParamsRequest{})
if err != nil {
return nil, fmt.Errorf("failed to fetch money markets: %w", err)
}

return paramsRes.Params.MoneyMarkets, nil
}

func (c *GrpcClient) GetBorrows(height int64) (hardtypes.Borrows, error) {
borrowRes, err := c.HardClient.Borrows(ctxAtHeight(height), &hardtypes.QueryBorrowsRequest{})
if err != nil {
return nil, fmt.Errorf("failed to fetch borrows: %w", err)
}

borrows := make([]hardtypes.Borrow, len(borrowRes.Borrows))
for i, response := range borrowRes.Borrows {
borrows[i] = hardtypes.Borrow{
Borrower: sdk.AccAddress(response.Borrower),
Amount: response.Amount,
}
}

return borrows, nil
}

func (c *GrpcClient) GetDeposits(height int64) (hardtypes.Deposits, error) {
depositRes, err := c.HardClient.Deposits(ctxAtHeight(height), &hardtypes.QueryDepositsRequest{})
if err != nil {
return nil, fmt.Errorf("failed to fetch deposits: %w", err)
}

deposits := make([]hardtypes.Deposit, len(depositRes.Deposits))
for i, response := range depositRes.Deposits {
deposits[i] = hardtypes.Deposit{
Depositor: sdk.AccAddress(response.Depositor),
Amount: response.Amount,
}
}

return deposits, nil
}
115 changes: 115 additions & 0 deletions hard-keeper-bot/grpc_client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package main

import (
"os"
"testing"

"github.com/stretchr/testify/require"
)

var invalidHeight int64 = 100
var latestHeight int64
var grpcClient *GrpcClient

func TestMain(m *testing.M) {
grpcClient, _ = NewGrpcClient("https://grpc.kava.io:443")

os.Exit(m.Run())
}

func TestGrpcClientInvalidUrl(t *testing.T) {
_, err := NewGrpcClient("invalid-url")
require.Error(t, err)
}

func TestHardKeeperGetInfo(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode")
}

res, err := grpcClient.GetInfo()
require.NoError(t, err)
require.Greater(t, res.LatestHeight, int64(11000000))
require.Equal(t, "kava_2222-10", res.ChainId)
latestHeight = res.LatestHeight
}

func TestHardKeeperGetPrices(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode")
}

res, err := grpcClient.GetPrices(latestHeight)
require.NoError(t, err)
require.NotEmpty(t, res)
require.Equal(t, len(res), 29)
}

func TestHardKeeperGetPricesInvalidHeight(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode")
}

_, err := grpcClient.GetPrices(invalidHeight)
require.Error(t, err)
}

func TestHardKeeperGetMarkets(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode")
}

res, err := grpcClient.GetMarkets(latestHeight)
require.NoError(t, err)
require.NotEmpty(t, res)
require.Equal(t, len(res), 16)
}

func TestHardKeeperGetMarketsInvalidHeight(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode")
}

_, err := grpcClient.GetMarkets(invalidHeight)
require.Error(t, err)
}

func TestHardKeeperGetBorrows(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode")
}

res, err := grpcClient.GetBorrows(latestHeight)
require.NoError(t, err)
require.NotEmpty(t, res)
require.Equal(t, len(res), 100)
}

func TestHardKeeperGetBorrowsInvalidHeight(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode")
}

_, err := grpcClient.GetBorrows(invalidHeight)
require.Error(t, err)
}

func TestHardKeeperGetDeposits(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode")
}

res, err := grpcClient.GetDeposits(latestHeight)
require.NoError(t, err)
require.NotEmpty(t, res)
require.Equal(t, len(res), 100)
}

func TestHardKeeperGetDepositsInvalidHeight(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode")
}

_, err := grpcClient.GetDeposits(invalidHeight)
require.Error(t, err)
}
4 changes: 1 addition & 3 deletions hard-keeper-bot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/kava-labs/go-tools/signing"
"github.com/kava-labs/kava/app"
"github.com/rs/zerolog"
rpchttpclient "github.com/tendermint/tendermint/rpc/client/http"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
Expand Down Expand Up @@ -49,11 +48,10 @@ func main() {
log.Fatalf("unknown rpc url scheme %s\n", grpcUrl.Scheme)
}

http, err := rpchttpclient.New(config.KavaRpcUrl, "/websocket")
liquidationClient, err := NewGrpcClient(config.KavaGrpcUrl)
if err != nil {
logger.Fatal().Err(err).Send()
}
liquidationClient := NewRpcLiquidationClient(http, encodingConfig.Amino)

conn, err := grpc.Dial(grpcUrl.Host, secureOpt)
if err != nil {
Expand Down
Loading