Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
luke-kava committed Oct 8, 2024
1 parent 243180d commit 79344a2
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 55 deletions.
91 changes: 43 additions & 48 deletions hard-keeper-bot/grpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"crypto/tls"
"fmt"
"log"
"net/url"
"strconv"

Expand All @@ -20,10 +19,10 @@ import (
)

type GrpcClient struct {
GrpcClientConn *grpc.ClientConn
Tm tmservice.ServiceClient
Hard hardtypes.QueryClient
Pricefeed pricefeedtypes.QueryClient
Conn *grpc.ClientConn
TmClient tmservice.ServiceClient
HardClient hardtypes.QueryClient
PricefeedClient pricefeedtypes.QueryClient
}

var _ LiquidationClient = (*GrpcClient)(nil)
Expand All @@ -33,38 +32,37 @@ func ctxAtHeight(height int64) context.Context {
return metadata.AppendToOutgoingContext(context.Background(), grpctypes.GRPCBlockHeightHeader, heightStr)
}

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

var secureOpt grpc.DialOption
switch grpcUrl.Scheme {
var dialOptions grpc.DialOption
switch grpcURL.Scheme {
case "http":
secureOpt = grpc.WithInsecure()
dialOptions = grpc.WithInsecure()
case "https":
creds := credentials.NewTLS(&tls.Config{})
secureOpt = grpc.WithTransportCredentials(creds)
dialOptions = grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{}))
default:
log.Fatalf("unknown rpc url scheme %s\n", grpcUrl.Scheme)
return nil, fmt.Errorf("unsupported scheme: %s", grpcURL.Scheme)
}

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

return GrpcClient{
GrpcClientConn: grpcConn,
Tm: tmservice.NewServiceClient(grpcConn),
Hard: hardtypes.NewQueryClient(grpcConn),
Pricefeed: pricefeedtypes.NewQueryClient(grpcConn),
}
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.Tm.GetLatestBlock(context.Background(), &tmservice.GetLatestBlockRequest{})
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)
}
Expand All @@ -75,64 +73,61 @@ func (c GrpcClient) GetInfo() (*InfoResponse, error) {
}, nil
}

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

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

return prices, nil
}

func (c GrpcClient) GetMarkets(height int64) (hardtypes.MoneyMarkets, error) {
paramsRes, err := c.Hard.Params(ctxAtHeight(height), &hardtypes.QueryParamsRequest{})
func (c *GrpcClient) GetMarkets(height int64) (hardtypes.MoneyMarkets, error) {
paramsRes, err := c.HardClient.Params(ctxAtHeight(height), &hardtypes.QueryParamsRequest{})
if err != nil {
return nil, err
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.Hard.Borrows(ctxAtHeight(height), &hardtypes.QueryBorrowsRequest{})
func (c *GrpcClient) GetBorrows(height int64) (hardtypes.Borrows, error) {
borrowRes, err := c.HardClient.Borrows(ctxAtHeight(height), &hardtypes.QueryBorrowsRequest{})
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to fetch borrows: %w", err)
}

var borrows []hardtypes.Borrow
for _, response := range borrowRes.Borrows {
borrow := hardtypes.Borrow{
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,
}
borrows = append(borrows, borrow)
}

return borrows, nil
}

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

var deposits []hardtypes.Deposit
for _, response := range depositRes.Deposits {
deposit := hardtypes.Deposit{
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,
}
deposits = append(deposits, deposit)
}

return deposits, nil
Expand Down
14 changes: 8 additions & 6 deletions hard-keeper-bot/grpc_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
"github.com/stretchr/testify/require"
)

var grpcClient GrpcClient
var latestHeight int64
var grpcClient *GrpcClient

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

os.Exit(m.Run())
}
Expand All @@ -24,14 +25,15 @@ func TestHardKeeperGetInfo(t *testing.T) {
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(12076815)
res, err := grpcClient.GetPrices(latestHeight)
require.NoError(t, err)
require.NotEmpty(t, res)
require.Equal(t, len(res), 29)
Expand All @@ -42,7 +44,7 @@ func TestHardKeeperGetMarkets(t *testing.T) {
t.Skip("skipping test in short mode")
}

res, err := grpcClient.GetMarkets(12076815)
res, err := grpcClient.GetMarkets(latestHeight)
require.NoError(t, err)
require.NotEmpty(t, res)
require.Equal(t, len(res), 16)
Expand All @@ -53,7 +55,7 @@ func TestHardKeeperGetBorrows(t *testing.T) {
t.Skip("skipping test in short mode")
}

res, err := grpcClient.GetBorrows(12076815)
res, err := grpcClient.GetBorrows(latestHeight)
require.NoError(t, err)
require.NotEmpty(t, res)
require.Equal(t, len(res), 100)
Expand All @@ -64,7 +66,7 @@ func TestHardKeeperGetDeposits(t *testing.T) {
t.Skip("skipping test in short mode")
}

res, err := grpcClient.GetDeposits(12076815)
res, err := grpcClient.GetDeposits(latestHeight)
require.NoError(t, err)
require.NotEmpty(t, res)
require.Equal(t, len(res), 100)
Expand Down
5 changes: 4 additions & 1 deletion hard-keeper-bot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ func main() {
log.Fatalf("unknown rpc url scheme %s\n", grpcUrl.Scheme)
}

liquidationClient := NewGrpcClient(config.KavaGrpcUrl)
liquidationClient, err := NewGrpcClient(config.KavaGrpcUrl)
if err != nil {
logger.Fatal().Err(err).Send()
}

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

0 comments on commit 79344a2

Please sign in to comment.