2019-11-27 14:45:59 +00:00
|
|
|
package keeper
|
2019-11-25 19:46:02 +00:00
|
|
|
|
|
|
|
import (
|
2020-04-23 16:35:58 +00:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
2020-01-17 12:29:19 +00:00
|
|
|
|
2019-11-25 19:46:02 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-04-23 16:35:58 +00:00
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
2019-11-27 14:45:59 +00:00
|
|
|
|
|
|
|
"github.com/kava-labs/kava/x/pricefeed/types"
|
2019-11-25 19:46:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewQuerier is the module level router for state queries
|
2022-01-08 00:39:27 +00:00
|
|
|
func NewQuerier(keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) sdk.Querier {
|
2020-04-23 16:35:58 +00:00
|
|
|
return func(ctx sdk.Context, path []string, req abci.RequestQuery) (res []byte, err error) {
|
2019-11-25 19:46:02 +00:00
|
|
|
switch path[0] {
|
2020-02-02 16:06:33 +00:00
|
|
|
case types.QueryPrice:
|
2022-01-08 00:39:27 +00:00
|
|
|
return queryPrice(ctx, req, keeper, legacyQuerierCdc)
|
2021-01-21 17:36:51 +00:00
|
|
|
case types.QueryPrices:
|
2022-01-08 00:39:27 +00:00
|
|
|
return queryPrices(ctx, req, keeper, legacyQuerierCdc)
|
2019-11-27 14:45:59 +00:00
|
|
|
case types.QueryRawPrices:
|
2022-01-08 00:39:27 +00:00
|
|
|
return queryRawPrices(ctx, req, keeper, legacyQuerierCdc)
|
2020-02-02 16:06:33 +00:00
|
|
|
case types.QueryOracles:
|
2022-01-08 00:39:27 +00:00
|
|
|
return queryOracles(ctx, req, keeper, legacyQuerierCdc)
|
2020-01-17 12:29:19 +00:00
|
|
|
case types.QueryMarkets:
|
2022-01-08 00:39:27 +00:00
|
|
|
return queryMarkets(ctx, req, keeper, legacyQuerierCdc)
|
2020-01-17 12:55:17 +00:00
|
|
|
case types.QueryGetParams:
|
2022-01-08 00:39:27 +00:00
|
|
|
return queryGetParams(ctx, req, keeper, legacyQuerierCdc)
|
2019-11-25 19:46:02 +00:00
|
|
|
default:
|
2020-04-23 16:35:58 +00:00
|
|
|
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown %s query endpoint", types.ModuleName)
|
2019-11-25 19:46:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func queryPrice(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) (res []byte, sdkErr error) {
|
2020-02-02 16:06:33 +00:00
|
|
|
var requestParams types.QueryWithMarketIDParams
|
2022-01-08 00:39:27 +00:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, &requestParams)
|
2020-01-17 12:29:19 +00:00
|
|
|
if err != nil {
|
2020-04-23 16:35:58 +00:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2020-01-17 12:29:19 +00:00
|
|
|
}
|
|
|
|
_, found := keeper.GetMarket(ctx, requestParams.MarketID)
|
2019-11-25 19:46:02 +00:00
|
|
|
if !found {
|
2021-03-15 14:41:36 +00:00
|
|
|
return nil, sdkerrors.Wrap(types.ErrAssetNotFound, requestParams.MarketID)
|
2019-11-25 19:46:02 +00:00
|
|
|
}
|
2020-01-17 12:29:19 +00:00
|
|
|
currentPrice, sdkErr := keeper.GetCurrentPrice(ctx, requestParams.MarketID)
|
|
|
|
if sdkErr != nil {
|
|
|
|
return nil, sdkErr
|
2020-01-12 15:35:34 +00:00
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, currentPrice)
|
2020-01-17 12:29:19 +00:00
|
|
|
if err != nil {
|
2020-04-23 16:35:58 +00:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2019-11-25 19:46:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func queryPrices(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) (res []byte, sdkErr error) {
|
2021-01-21 17:36:51 +00:00
|
|
|
currentPrices := keeper.GetCurrentPrices(ctx)
|
2022-01-08 00:39:27 +00:00
|
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, currentPrices)
|
2021-01-21 17:36:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
|
|
|
}
|
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func queryRawPrices(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) (res []byte, sdkErr error) {
|
2020-02-02 16:06:33 +00:00
|
|
|
var requestParams types.QueryWithMarketIDParams
|
2022-01-08 00:39:27 +00:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, &requestParams)
|
2020-04-23 16:35:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2020-01-17 12:29:19 +00:00
|
|
|
}
|
|
|
|
_, found := keeper.GetMarket(ctx, requestParams.MarketID)
|
2019-11-25 19:46:02 +00:00
|
|
|
if !found {
|
2021-03-15 14:41:36 +00:00
|
|
|
return nil, sdkerrors.Wrap(types.ErrAssetNotFound, requestParams.MarketID)
|
2019-11-25 19:46:02 +00:00
|
|
|
}
|
2020-04-23 16:35:58 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
rawPrices := keeper.GetRawPrices(ctx, requestParams.MarketID)
|
2020-04-23 16:35:58 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, rawPrices)
|
2020-04-23 16:35:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2019-11-25 19:46:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func queryOracles(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) (res []byte, sdkErr error) {
|
2020-02-02 16:06:33 +00:00
|
|
|
var requestParams types.QueryWithMarketIDParams
|
2022-01-08 00:39:27 +00:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, &requestParams)
|
2020-02-02 16:06:33 +00:00
|
|
|
if err != nil {
|
2020-04-23 16:35:58 +00:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2020-02-02 16:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
oracles, err := keeper.GetOracles(ctx, requestParams.MarketID)
|
|
|
|
if err != nil {
|
2021-03-15 14:41:36 +00:00
|
|
|
return nil, sdkerrors.Wrap(types.ErrAssetNotFound, requestParams.MarketID)
|
2020-02-02 16:06:33 +00:00
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, oracles)
|
2020-02-02 16:06:33 +00:00
|
|
|
if err != nil {
|
2020-04-23 16:35:58 +00:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2020-02-02 16:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func queryMarkets(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) (res []byte, sdkErr error) {
|
2020-01-17 12:29:19 +00:00
|
|
|
markets := keeper.GetMarkets(ctx)
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, markets)
|
2020-01-17 12:29:19 +00:00
|
|
|
if err != nil {
|
2020-04-23 16:35:58 +00:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2019-11-25 19:46:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return bz, nil
|
2019-11-27 14:45:59 +00:00
|
|
|
}
|
2020-01-17 12:55:17 +00:00
|
|
|
|
2020-02-02 16:06:33 +00:00
|
|
|
// query params in the pricefeed store
|
2022-01-08 00:39:27 +00:00
|
|
|
func queryGetParams(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2020-01-17 12:55:17 +00:00
|
|
|
params := keeper.GetParams(ctx)
|
|
|
|
|
|
|
|
// Encode results
|
2022-01-08 00:39:27 +00:00
|
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, params)
|
2020-01-17 12:55:17 +00:00
|
|
|
if err != nil {
|
2020-04-23 16:35:58 +00:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2020-01-17 12:55:17 +00:00
|
|
|
}
|
|
|
|
return bz, nil
|
|
|
|
}
|