diff --git a/alerts/Dockerfile b/alerts/Dockerfile index c4f2894..8cc93a7 100644 --- a/alerts/Dockerfile +++ b/alerts/Dockerfile @@ -1,12 +1,14 @@ -FROM golang:1.20-bullseye as build-env +FROM golang:1.21.9-bullseye AS build-env ADD . /src -RUN cd /src && go build -o /alerts +RUN --mount=type=cache,target=/root/.cache/go-build \ + --mount=type=cache,target=/go/pkg/mod \ + cd /src && go build -o /alerts FROM debian:bullseye RUN apt-get update \ - && apt-get install -y ca-certificates \ + && apt-get install -y curl ca-certificates \ && rm -rf /var/lib/apt/lists/* \ && update-ca-certificates diff --git a/alerts/auctions/client.go b/alerts/auctions/client.go deleted file mode 100644 index e634c02..0000000 --- a/alerts/auctions/client.go +++ /dev/null @@ -1,180 +0,0 @@ -package auctions - -import ( - "context" - "errors" - "github.com/cometbft/cometbft/libs/bytes" - rpcclient "github.com/cometbft/cometbft/rpc/client" - "github.com/cosmos/cosmos-sdk/codec" - auctiontypes "github.com/kava-labs/kava/x/auction/types" - cdptypes "github.com/kava-labs/kava/x/cdp/types" - hardtypes "github.com/kava-labs/kava/x/hard/types" - pricefeedtypes "github.com/kava-labs/kava/x/pricefeed/types" -) - -const ( - DefaultPageLimit = 1000 -) - -// InfoResponse defines the ID and latest height for a specific chain -type InfoResponse struct { - ChainId string `json:"chain_id" yaml:"chain_id"` - LatestHeight int64 `json:"latest_height" yaml:"latest_height"` -} - -// RpcAuctionClient defines a client for interacting with auctions via rpc -type RpcAuctionClient struct { - rpc RpcClient - cdc codec.LegacyAmino - PageLimit int -} - -var _ AuctionClient = (*RpcAuctionClient)(nil) - -// NewRpcAuctionClient returns a new RpcAuctionClient -func NewRpcAuctionClient(rpc RpcClient, cdc codec.LegacyAmino) *RpcAuctionClient { - return &RpcAuctionClient{ - rpc: rpc, - cdc: cdc, - PageLimit: DefaultPageLimit, - } -} - -// GetInfo returns the current chain info -func (c *RpcAuctionClient) GetInfo() (*InfoResponse, error) { - result, err := c.rpc.Status(context.Background()) - if err != nil { - return nil, err - } - - return &InfoResponse{ - ChainId: result.NodeInfo.Network, - LatestHeight: result.SyncInfo.LatestBlockHeight, - }, nil -} - -// GetPrices gets the current prices for markets -func (c *RpcAuctionClient) GetPrices(height int64) (pricefeedtypes.CurrentPriceResponses, error) { - //path := fmt.Sprintf("custom/%s/%s", pricefeedtypes.QuerierRoute, pricefeedtypes.QueryPrices) - path := "" - - data, err := c.abciQuery(path, bytes.HexBytes{}, height) - if err != nil { - return nil, err - } - - var currentPrices pricefeedtypes.CurrentPriceResponses - err = c.cdc.UnmarshalJSON(data, ¤tPrices) - if err != nil { - return nil, err - } - - return currentPrices, nil -} - -// GetMarkets gets an array of collateral params for each collateral type -func (c *RpcAuctionClient) GetMarkets(height int64) (cdptypes.CollateralParams, error) { - //path := fmt.Sprintf("custom/%s/%s", cdptypes.QuerierRoute, cdptypes.QueryGetParams) - path := "" - - data, err := c.abciQuery(path, bytes.HexBytes{}, height) - if err != nil { - return nil, err - } - - var params cdptypes.Params - err = c.cdc.UnmarshalJSON(data, ¶ms) - if err != nil { - return nil, err - } - - return params.CollateralParams, nil -} - -// GetMoneyMarkets gets an array of money markets for each asset -func (c *RpcAuctionClient) GetMoneyMarkets(height int64) (hardtypes.MoneyMarkets, error) { - //path := fmt.Sprintf("custom/%s/%s", hardtypes.QuerierRoute, hardtypes.QueryGetParams) - path := "" - // https://rpc.app.infra.kava.io/custom/hard/params?height=11624954 - // https://rpc.app.infra.kava.io/kava/hard/v1beta1/params?height=11624954 - // https://rpc.app.infra.kava.io/cosmos/base/tendermint/v1beta1/abci_query?path=%22custom/hard/params%22&data=null&height=11624954 - - data, err := c.abciQuery(path, bytes.HexBytes{}, height) - if err != nil { - return nil, err - } - - var params hardtypes.Params - err = c.cdc.UnmarshalJSON(data, ¶ms) - if err != nil { - return nil, err - } - return params.MoneyMarkets, nil -} - -// GetAuctions gets all the currently running auctions -func (c *RpcAuctionClient) GetAuctions(height int64) ([]auctiontypes.Auction, error) { - //path := fmt.Sprintf("custom/%s/%s", auctiontypes.QuerierRoute, auctiontypes.QueryGetAuctions) - path := "" - // https://rpc.app.infra.kava.io/custom/hard/params?height=11624954 - - page := 1 - var auctions []auctiontypes.Auction - - for { - //params := auctiontypes.NewQueryAllAuctionParams(page, c.PageLimit, "", "", "", sdk.AccAddress{}) - params := "" - bz, err := c.cdc.MarshalJSON(¶ms) - - if err != nil { - return nil, err - } - - data, err := c.abciQuery(path, bz, height) - if err != nil { - return nil, err - } - - var pagedAuctions []auctiontypes.Auction - err = c.cdc.UnmarshalJSON(data, &pagedAuctions) - if err != nil { - return nil, err - } - - if len(pagedAuctions) > 0 { - auctions = append(auctions, pagedAuctions...) - } - - if len(pagedAuctions) < c.PageLimit { - return auctions, nil - } - - page++ - } -} - -func (c *RpcAuctionClient) abciQuery( - path string, - data bytes.HexBytes, - height int64) ([]byte, error) { - opts := rpcclient.ABCIQueryOptions{Height: height, Prove: false} - - result, err := c.rpc.ABCIQueryWithOptions(context.Background(), path, data, opts) - if err != nil { - return []byte{}, err - } - - resp := result.Response - if !resp.IsOK() { - return []byte{}, errors.New(resp.Log) - } - - // TODO: why do we check length here? - value := result.Response.GetValue() - // TODO: untested logic case - if len(value) == 0 { - return []byte{}, nil - } - - return value, nil -} diff --git a/alerts/auctions/grpc_client.go b/alerts/auctions/grpc_client.go index bf3f370..7205b06 100644 --- a/alerts/auctions/grpc_client.go +++ b/alerts/auctions/grpc_client.go @@ -15,6 +15,16 @@ import ( "google.golang.org/grpc" ) +const ( + DefaultPageLimit = 1000 +) + +// InfoResponse defines the ID and latest height for a specific chain +type InfoResponse struct { + ChainId string `json:"chain_id" yaml:"chain_id"` + LatestHeight int64 `json:"latest_height" yaml:"latest_height"` +} + // AuctionClient defines the expected client interface for interacting with auctions type AuctionClient interface { GetInfo() (*InfoResponse, error) diff --git a/alerts/cmd/auctions.go b/alerts/cmd/auctions.go index c92cfd7..ada42dd 100644 --- a/alerts/cmd/auctions.go +++ b/alerts/cmd/auctions.go @@ -2,7 +2,6 @@ package cmd import ( "fmt" - kavagrpc "github.com/kava-labs/kava/client/grpc" "os" "strings" "time" @@ -14,6 +13,7 @@ import ( "github.com/kava-labs/go-tools/alerts/config" "github.com/kava-labs/go-tools/alerts/persistence" kava "github.com/kava-labs/kava/app" + kavagrpc "github.com/kava-labs/kava/client/grpc" "github.com/spf13/cobra" ) @@ -69,16 +69,11 @@ var runAuctionsCmd = &cobra.Command{ "AlertFrequency", config.AlertFrequency.String(), ).Info("config loaded") - // Bootstrap rpc http clent - //http, err := rpchttpclient.New(config.GrpcConfig, "/websocket") - //if err != nil { - // return err - //} - //http.Logger = logger - // Create codec for messages encodingConfig := kava.MakeEncodingConfig() + // Bootstrap grpc http client + grpcClient, err := kavagrpc.NewClient(config.KavaGrpcUrl) if err != nil { return fmt.Errorf("failed to create grpc client: %v", err) @@ -86,7 +81,7 @@ var runAuctionsCmd = &cobra.Command{ // Create rpc client for fetching data logger.Info("creating rpc client") - auctionClient := auctions.NewGrpcAuctionClient(grpcClient, *encodingConfig.Amino) + auctionClient := auctions.NewGrpcAuctionClient(grpcClient, encodingConfig.Marshaler) firstIteration := true