From 79344a2d40376bb1adbff492103f09f3ae6aa97d Mon Sep 17 00:00:00 2001 From: Luke Bayas Date: Tue, 8 Oct 2024 17:17:52 -0400 Subject: [PATCH] cleanup --- hard-keeper-bot/grpc_client.go | 91 ++++++++++++++--------------- hard-keeper-bot/grpc_client_test.go | 14 +++-- hard-keeper-bot/main.go | 5 +- 3 files changed, 55 insertions(+), 55 deletions(-) diff --git a/hard-keeper-bot/grpc_client.go b/hard-keeper-bot/grpc_client.go index e99e697..3a2caa4 100644 --- a/hard-keeper-bot/grpc_client.go +++ b/hard-keeper-bot/grpc_client.go @@ -4,7 +4,6 @@ import ( "context" "crypto/tls" "fmt" - "log" "net/url" "strconv" @@ -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) @@ -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) } @@ -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 diff --git a/hard-keeper-bot/grpc_client_test.go b/hard-keeper-bot/grpc_client_test.go index 70a6629..5cccf68 100644 --- a/hard-keeper-bot/grpc_client_test.go +++ b/hard-keeper-bot/grpc_client_test.go @@ -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()) } @@ -24,6 +25,7 @@ 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) { @@ -31,7 +33,7 @@ func TestHardKeeperGetPrices(t *testing.T) { 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) @@ -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) @@ -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) @@ -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) diff --git a/hard-keeper-bot/main.go b/hard-keeper-bot/main.go index ccf25f1..86d0602 100644 --- a/hard-keeper-bot/main.go +++ b/hard-keeper-bot/main.go @@ -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 {