-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: switch the client for alerts from rpc to grpc for updated kava …
…version
- Loading branch information
Showing
17 changed files
with
1,509 additions
and
588 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package auctions | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice" | ||
"github.com/cosmos/cosmos-sdk/codec" | ||
query "github.com/cosmos/cosmos-sdk/types/query" | ||
kavagrpc "github.com/kava-labs/kava/client/grpc" | ||
kavagrpcutil "github.com/kava-labs/kava/client/grpc/util" | ||
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" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
// AuctionClient defines the expected client interface for interacting with auctions | ||
type AuctionClient interface { | ||
GetInfo() (*InfoResponse, error) | ||
GetPrices(height int64) (pricefeedtypes.CurrentPriceResponses, error) | ||
GetAuctions(height int64) ([]auctiontypes.Auction, error) | ||
GetMarkets(height int64) (cdptypes.CollateralParams, error) | ||
GetMoneyMarkets(height int64) (hardtypes.MoneyMarkets, error) | ||
} | ||
|
||
// GrpcAuctionClient defines a client for interacting with auctions via rpc | ||
type GrpcAuctionClient struct { | ||
conn *grpc.ClientConn | ||
grpcClient *kavagrpc.KavaGrpcClient | ||
util *kavagrpcutil.Util | ||
cdc codec.Codec | ||
PageLimit uint64 | ||
} | ||
|
||
var _ AuctionClient = (*GrpcAuctionClient)(nil) | ||
|
||
// NewGrpcAuctionClient NewRpcAuctionClient returns a new RpcAuctionClient | ||
func NewGrpcAuctionClient( | ||
grpcClient *kavagrpc.KavaGrpcClient, | ||
cdc codec.Codec, | ||
) *GrpcAuctionClient { | ||
return &GrpcAuctionClient{ | ||
grpcClient: grpcClient, | ||
cdc: cdc, | ||
util: kavagrpcutil.NewUtil(nil), // we need util to create context for particular height | ||
PageLimit: DefaultPageLimit, | ||
} | ||
} | ||
|
||
// GetInfo returns the current chain info | ||
func (c *GrpcAuctionClient) GetInfo() (*InfoResponse, error) { | ||
resultLatestBlock, err := c.grpcClient.Query.Tm.GetLatestBlock(context.Background(), &tmservice.GetLatestBlockRequest{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resultNodeInfo, err := c.grpcClient.Query.Tm.GetNodeInfo(context.Background(), &tmservice.GetNodeInfoRequest{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &InfoResponse{ | ||
ChainId: resultNodeInfo.GetDefaultNodeInfo().Network, | ||
LatestHeight: resultLatestBlock.GetBlock().Header.Height, | ||
}, nil | ||
} | ||
|
||
// GetPrices gets the current prices for markets | ||
func (c *GrpcAuctionClient) GetPrices(height int64) (pricefeedtypes.CurrentPriceResponses, error) { | ||
heightCtx := c.util.CtxAtHeight(height) | ||
prices, err := c.grpcClient.Query.Pricefeed.Prices( | ||
heightCtx, | ||
&pricefeedtypes.QueryPricesRequest{}, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return prices.Prices, nil | ||
} | ||
|
||
// GetMarkets gets an array of collateral params for each collateral type | ||
func (c *GrpcAuctionClient) GetMarkets(height int64) (cdptypes.CollateralParams, error) { | ||
heightCtx := c.util.CtxAtHeight(height) | ||
params, err := c.grpcClient.Query.Cdp.Params(heightCtx, &cdptypes.QueryParamsRequest{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return params.Params.CollateralParams, nil | ||
} | ||
|
||
// GetMoneyMarkets gets an array of money markets for each asset | ||
func (c *GrpcAuctionClient) GetMoneyMarkets(height int64) (hardtypes.MoneyMarkets, error) { | ||
heightCtx := c.util.CtxAtHeight(height) | ||
params, err := c.grpcClient.Query.Hard.Params(heightCtx, &hardtypes.QueryParamsRequest{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return params.Params.MoneyMarkets, nil | ||
} | ||
|
||
// GetAuctions gets all the currently running auctions | ||
func (c *GrpcAuctionClient) GetAuctions(height int64) ([]auctiontypes.Auction, error) { | ||
var ( | ||
auctions []auctiontypes.Auction | ||
key []byte | ||
) | ||
|
||
for { | ||
auctionsResponse, err := c.grpcClient.Query.Auction.Auctions(context.Background(), &auctiontypes.QueryAuctionsRequest{ | ||
Pagination: &query.PageRequest{ | ||
Key: key, | ||
Limit: c.PageLimit, | ||
}, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, anyAuction := range auctionsResponse.GetAuctions() { | ||
var auction auctiontypes.Auction | ||
if err = c.cdc.UnpackAny(anyAuction, &auction); err != nil { | ||
return nil, fmt.Errorf("failed to unpack auction: %w", err) | ||
} | ||
|
||
auctions = append(auctions, auction) | ||
} | ||
|
||
if auctionsResponse.Pagination == nil || auctionsResponse.Pagination.NextKey == nil { | ||
break | ||
} | ||
} | ||
|
||
return auctions, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package auctions | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"os" | ||
"testing" | ||
|
||
"github.com/kava-labs/kava/app" | ||
kava "github.com/kava-labs/kava/app" | ||
kavagrpc "github.com/kava-labs/kava/client/grpc" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// pruning node stores only latest data and doesn't store historical data. | ||
// It is much more performant and can be used for transaction search, as on archive node it is very slow. | ||
var pruningQueryClient AuctionClient | ||
|
||
// we should use dataQueryClient only for historical data, as pruning node doesn't contain historical data | ||
var dataQueryClient AuctionClient | ||
|
||
func TestMain(m *testing.M) { | ||
app.SetSDKConfig() | ||
|
||
// Create codec for messages | ||
encodingConfig := kava.MakeEncodingConfig() | ||
|
||
pruningGRPCClient, err := kavagrpc.NewClient("https://grpc.kava.io:443") | ||
if err != nil { | ||
log.Fatalf("grpc client failed to connect %s", err) | ||
} | ||
|
||
dataGRPCClient, err := kavagrpc.NewClient("https://grpc.data.infra.kava.io:443") | ||
if err != nil { | ||
log.Fatalf("grpc client failed to connect %s", err) | ||
} | ||
|
||
pruningQueryClient = NewGrpcAuctionClient(pruningGRPCClient, encodingConfig.Marshaler) | ||
dataQueryClient = NewGrpcAuctionClient(dataGRPCClient, encodingConfig.Marshaler) | ||
|
||
os.Exit(m.Run()) | ||
} | ||
|
||
func TestGrpcGetInfo(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip("skipping test in short mode") | ||
} | ||
|
||
info, err := pruningQueryClient.GetInfo() | ||
require.NoError(t, err) | ||
require.Greater(t, info.LatestHeight, int64(11000000)) | ||
require.Equal(t, "kava_2222-10", info.ChainId) | ||
} | ||
|
||
func TestGrpcGetPrices(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip("skipping test in short mode") | ||
} | ||
|
||
prices, err := dataQueryClient.GetPrices(11000000) | ||
require.NoError(t, err) | ||
require.Len(t, prices, 29) | ||
for _, price := range prices { | ||
require.NotEmpty(t, price.MarketID) | ||
require.NotEmpty(t, price.Price) | ||
} | ||
} | ||
|
||
func TestGrpcGetAuctions(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip("skipping test in short mode") | ||
} | ||
|
||
auctions, err := pruningQueryClient.GetAuctions(11000000) | ||
require.NoError(t, err) | ||
require.Len(t, auctions, 10) | ||
for _, auction := range auctions { | ||
require.NotEmpty(t, auction.GetID()) | ||
require.NotEmpty(t, auction.GetInitiator()) | ||
require.NotEmpty(t, auction.GetLot()) | ||
require.NotEmpty(t, auction.GetBid()) | ||
require.NotEmpty(t, auction.GetEndTime()) | ||
} | ||
} | ||
|
||
func TestGrpcGetMarkets(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip("skipping test in short mode") | ||
} | ||
|
||
markets, err := dataQueryClient.GetMarkets(11000000) | ||
require.NoError(t, err) | ||
jsonMarkets, err := json.Marshal(markets) | ||
fmt.Println(len(markets)) | ||
fmt.Println(string(jsonMarkets)) | ||
require.Len(t, markets, 10) | ||
} | ||
|
||
func TestGrpcGetMoneyMarkets(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip("skipping test in short mode") | ||
} | ||
|
||
moneyMarkets, err := dataQueryClient.GetMoneyMarkets(11000000) | ||
require.NoError(t, err) | ||
require.Len(t, moneyMarkets, 29) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.