mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-26 08:15:19 +00:00
Pricefeed query fix (#1149)
* filter prices in querier * filter prices in grpc querier * test grpc querier
This commit is contained in:
parent
31b8ed2276
commit
03ab76bb2d
@ -56,7 +56,9 @@ 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) {
|
||||||
currentPrices = append(currentPrices, types.CurrentPriceResponse(cp))
|
if cp.MarketID != "" {
|
||||||
|
currentPrices = append(currentPrices, types.CurrentPriceResponse(cp))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &types.QueryPricesResponse{
|
return &types.QueryPricesResponse{
|
||||||
|
@ -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()
|
||||||
|
@ -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())
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user