From 03ab76bb2db62b8c49dd88c8e18edb0a96438e47 Mon Sep 17 00:00:00 2001 From: Denali Marsh Date: Tue, 25 Jan 2022 15:56:51 +0100 Subject: [PATCH] Pricefeed query fix (#1149) * filter prices in querier * filter prices in grpc querier * test grpc querier --- x/pricefeed/keeper/grpc_query.go | 4 +++- x/pricefeed/keeper/grpc_query_test.go | 27 +++++++++++++++++++++++++++ x/pricefeed/keeper/querier.go | 11 ++++++++++- 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/x/pricefeed/keeper/grpc_query.go b/x/pricefeed/keeper/grpc_query.go index 7ce626ea..48a92bae 100644 --- a/x/pricefeed/keeper/grpc_query.go +++ b/x/pricefeed/keeper/grpc_query.go @@ -56,7 +56,9 @@ func (s queryServer) Prices(c context.Context, req *types.QueryPricesRequest) (* var currentPrices types.CurrentPriceResponses for _, cp := range s.keeper.GetCurrentPrices(ctx) { - currentPrices = append(currentPrices, types.CurrentPriceResponse(cp)) + if cp.MarketID != "" { + currentPrices = append(currentPrices, types.CurrentPriceResponse(cp)) + } } return &types.QueryPricesResponse{ diff --git a/x/pricefeed/keeper/grpc_query_test.go b/x/pricefeed/keeper/grpc_query_test.go index b30239a5..372a10e4 100644 --- a/x/pricefeed/keeper/grpc_query_test.go +++ b/x/pricefeed/keeper/grpc_query_test.go @@ -117,6 +117,33 @@ func (suite *grpcQueryTestSuite) TestGrpcPrices() { suite.Contains(prices.Prices, expectedPrice, "all prices should include the tstusd price") } +func (suite *grpcQueryTestSuite) TestGrpcPrices_NoPriceSet() { + params := types.NewParams([]types.Market{ + {MarketID: "tst:usd", BaseAsset: "tst", QuoteAsset: "usd", Oracles: []sdk.AccAddress{}, Active: true}, + {MarketID: "other:usd", BaseAsset: "other", QuoteAsset: "usd", Oracles: []sdk.AccAddress{}, Active: true}, + }) + suite.keeper.SetParams(suite.ctx, params) + + _, err := suite.keeper.SetPrice( + suite.ctx, suite.addrs[2], "tst:usd", + sdk.MustNewDecFromStr("0.34"), + suite.now.Add(time.Hour*1)) + suite.NoError(err) + + err = suite.keeper.SetCurrentPrices(suite.ctx, "tst:usd") + suite.NoError(err) + + // Set current price of "other:usd" with no individual prices in store + _ = suite.keeper.SetCurrentPrices(suite.ctx, "other:usd") + + expectedPrice := types.NewCurrentPriceResponse("tst:usd", sdk.MustNewDecFromStr("0.34")) + prices, err := suite.queryServer.Prices(sdk.WrapSDKContext(suite.ctx), &types.QueryPricesRequest{}) + suite.NoError(err) + + suite.Equal(len(prices.Prices), 1) + suite.Equal(prices.Prices, types.CurrentPriceResponses{expectedPrice}, "should only contain tst:usd price") +} + func (suite *grpcQueryTestSuite) TestGrpcRawPrices() { suite.setTestParams() suite.setTstPrice() diff --git a/x/pricefeed/keeper/querier.go b/x/pricefeed/keeper/querier.go index 55676c74..755eb440 100644 --- a/x/pricefeed/keeper/querier.go +++ b/x/pricefeed/keeper/querier.go @@ -57,7 +57,16 @@ func queryPrice(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQue func queryPrices(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) (res []byte, sdkErr error) { currentPrices := keeper.GetCurrentPrices(ctx) - bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, currentPrices) + + // Filter out invalid markets without a price + var validCurrentPrices types.CurrentPrices + for _, cp := range currentPrices { + if cp.MarketID != "" { + validCurrentPrices = append(validCurrentPrices, types.CurrentPrice(cp)) + } + } + + bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, validCurrentPrices) if err != nil { return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error()) }