0g-chain/x/pricefeed/keeper/querier.go

130 lines
4.4 KiB
Go
Raw Normal View History

2019-11-27 14:45:59 +00:00
package keeper
2019-11-25 19:46:02 +00:00
import (
abci "github.com/tendermint/tendermint/abci/types"
2019-11-25 19:46:02 +00:00
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
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
func NewQuerier(keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) sdk.Querier {
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] {
case types.QueryPrice:
return queryPrice(ctx, req, keeper, legacyQuerierCdc)
case types.QueryPrices:
return queryPrices(ctx, req, keeper, legacyQuerierCdc)
2019-11-27 14:45:59 +00:00
case types.QueryRawPrices:
return queryRawPrices(ctx, req, keeper, legacyQuerierCdc)
case types.QueryOracles:
return queryOracles(ctx, req, keeper, legacyQuerierCdc)
case types.QueryMarkets:
return queryMarkets(ctx, req, keeper, legacyQuerierCdc)
case types.QueryGetParams:
return queryGetParams(ctx, req, keeper, legacyQuerierCdc)
2019-11-25 19:46:02 +00:00
default:
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown %s query endpoint", types.ModuleName)
2019-11-25 19:46:02 +00:00
}
}
}
func queryPrice(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) (res []byte, sdkErr error) {
var requestParams types.QueryWithMarketIDParams
err := legacyQuerierCdc.UnmarshalJSON(req.Data, &requestParams)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
_, found := keeper.GetMarket(ctx, requestParams.MarketID)
2019-11-25 19:46:02 +00:00
if !found {
return nil, sdkerrors.Wrap(types.ErrAssetNotFound, requestParams.MarketID)
2019-11-25 19:46:02 +00:00
}
currentPrice, sdkErr := keeper.GetCurrentPrice(ctx, requestParams.MarketID)
if sdkErr != nil {
return nil, sdkErr
}
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, currentPrice)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
2019-11-25 19:46:02 +00:00
}
return bz, nil
}
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)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
func queryRawPrices(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) (res []byte, sdkErr error) {
var requestParams types.QueryWithMarketIDParams
err := legacyQuerierCdc.UnmarshalJSON(req.Data, &requestParams)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
_, found := keeper.GetMarket(ctx, requestParams.MarketID)
2019-11-25 19:46:02 +00:00
if !found {
return nil, sdkerrors.Wrap(types.ErrAssetNotFound, requestParams.MarketID)
2019-11-25 19:46:02 +00:00
}
rawPrices := keeper.GetRawPrices(ctx, requestParams.MarketID)
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, rawPrices)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
2019-11-25 19:46:02 +00:00
}
return bz, nil
}
func queryOracles(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) (res []byte, sdkErr error) {
var requestParams types.QueryWithMarketIDParams
err := legacyQuerierCdc.UnmarshalJSON(req.Data, &requestParams)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
oracles, err := keeper.GetOracles(ctx, requestParams.MarketID)
if err != nil {
return nil, sdkerrors.Wrap(types.ErrAssetNotFound, requestParams.MarketID)
}
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, oracles)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
func queryMarkets(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) (res []byte, sdkErr error) {
markets := keeper.GetMarkets(ctx)
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, markets)
if err != nil {
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
}
// query params in the pricefeed store
func queryGetParams(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
params := keeper.GetParams(ctx)
// Encode results
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, params)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}