Pricefeed query fix (#1149)

* filter prices in querier

* filter prices in grpc querier

* test grpc querier
This commit is contained in:
Denali Marsh 2022-01-25 15:56:51 +01:00 committed by GitHub
parent 31b8ed2276
commit 03ab76bb2d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 2 deletions

View File

@ -56,8 +56,10 @@ func (s queryServer) Prices(c context.Context, req *types.QueryPricesRequest) (*
var currentPrices types.CurrentPriceResponses var currentPrices types.CurrentPriceResponses
for _, cp := range s.keeper.GetCurrentPrices(ctx) { for _, cp := range s.keeper.GetCurrentPrices(ctx) {
if cp.MarketID != "" {
currentPrices = append(currentPrices, types.CurrentPriceResponse(cp)) currentPrices = append(currentPrices, types.CurrentPriceResponse(cp))
} }
}
return &types.QueryPricesResponse{ return &types.QueryPricesResponse{
Prices: currentPrices, Prices: currentPrices,

View File

@ -117,6 +117,33 @@ func (suite *grpcQueryTestSuite) TestGrpcPrices() {
suite.Contains(prices.Prices, expectedPrice, "all prices should include the tstusd price") 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() { func (suite *grpcQueryTestSuite) TestGrpcRawPrices() {
suite.setTestParams() suite.setTestParams()
suite.setTstPrice() suite.setTstPrice()

View File

@ -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) { func queryPrices(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) (res []byte, sdkErr error) {
currentPrices := keeper.GetCurrentPrices(ctx) 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 { if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error()) return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
} }