2019-11-27 14:45:59 +00:00
|
|
|
package keeper
|
2019-11-25 19:46:02 +00:00
|
|
|
|
|
|
|
import (
|
2020-01-17 12:29:19 +00:00
|
|
|
"fmt"
|
|
|
|
|
2019-11-25 19:46:02 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-11-27 14:45:59 +00:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
|
|
|
|
"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) sdk.Querier {
|
|
|
|
return func(ctx sdk.Context, path []string, req abci.RequestQuery) (res []byte, err sdk.Error) {
|
|
|
|
switch path[0] {
|
2020-02-02 16:06:33 +00:00
|
|
|
case types.QueryPrice:
|
|
|
|
return queryPrice(ctx, req, keeper)
|
2019-11-27 14:45:59 +00:00
|
|
|
case types.QueryRawPrices:
|
2020-01-17 12:29:19 +00:00
|
|
|
return queryRawPrices(ctx, req, keeper)
|
2020-02-02 16:06:33 +00:00
|
|
|
case types.QueryOracles:
|
|
|
|
return queryOracles(ctx, req, keeper)
|
2020-01-17 12:29:19 +00:00
|
|
|
case types.QueryMarkets:
|
|
|
|
return queryMarkets(ctx, req, keeper)
|
2020-01-17 12:55:17 +00:00
|
|
|
case types.QueryGetParams:
|
|
|
|
return queryGetParams(ctx, req, keeper)
|
2019-11-25 19:46:02 +00:00
|
|
|
default:
|
|
|
|
return nil, sdk.ErrUnknownRequest("unknown pricefeed query endpoint")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-02-02 16:06:33 +00:00
|
|
|
func queryPrice(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) (res []byte, sdkErr sdk.Error) {
|
|
|
|
var requestParams types.QueryWithMarketIDParams
|
2020-01-17 12:29:19 +00:00
|
|
|
err := keeper.cdc.UnmarshalJSON(req.Data, &requestParams)
|
|
|
|
if err != nil {
|
|
|
|
return nil, sdk.ErrInternal(fmt.Sprintf("failed to parse params: %s", err))
|
|
|
|
}
|
|
|
|
_, found := keeper.GetMarket(ctx, requestParams.MarketID)
|
2019-11-25 19:46:02 +00:00
|
|
|
if !found {
|
|
|
|
return []byte{}, sdk.ErrUnknownRequest("asset not found")
|
|
|
|
}
|
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
|
|
|
}
|
2020-01-17 12:29:19 +00:00
|
|
|
bz, err := codec.MarshalJSONIndent(keeper.cdc, currentPrice)
|
|
|
|
if err != nil {
|
2019-11-25 19:46:02 +00:00
|
|
|
panic("could not marshal result to JSON")
|
|
|
|
}
|
|
|
|
|
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
2020-01-17 12:29:19 +00:00
|
|
|
func queryRawPrices(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) (res []byte, sdkErr sdk.Error) {
|
2020-02-02 16:06:33 +00:00
|
|
|
var requestParams types.QueryWithMarketIDParams
|
2020-04-13 18:08:14 +00:00
|
|
|
err2 := keeper.cdc.UnmarshalJSON(req.Data, &requestParams)
|
|
|
|
if err2 != nil {
|
|
|
|
return nil, sdk.ErrInternal(fmt.Sprintf("failed to parse params: %s", err2))
|
2020-01-17 12:29:19 +00:00
|
|
|
}
|
|
|
|
_, found := keeper.GetMarket(ctx, requestParams.MarketID)
|
2019-11-25 19:46:02 +00:00
|
|
|
if !found {
|
|
|
|
return []byte{}, sdk.ErrUnknownRequest("asset not found")
|
|
|
|
}
|
2020-04-13 18:08:14 +00:00
|
|
|
rawPrices, err := keeper.GetRawPrices(ctx, requestParams.MarketID)
|
2020-01-17 12:29:19 +00:00
|
|
|
if err != nil {
|
2020-04-13 18:08:14 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
bz, err2 := codec.MarshalJSONIndent(keeper.cdc, rawPrices)
|
|
|
|
if err2 != nil {
|
2019-11-25 19:46:02 +00:00
|
|
|
panic("could not marshal result to JSON")
|
|
|
|
}
|
|
|
|
|
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
2020-02-02 16:06:33 +00:00
|
|
|
func queryOracles(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) (res []byte, sdkErr sdk.Error) {
|
|
|
|
var requestParams types.QueryWithMarketIDParams
|
|
|
|
err := keeper.cdc.UnmarshalJSON(req.Data, &requestParams)
|
|
|
|
if err != nil {
|
|
|
|
return nil, sdk.ErrInternal(fmt.Sprintf("failed to parse params: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
oracles, err := keeper.GetOracles(ctx, requestParams.MarketID)
|
|
|
|
if err != nil {
|
|
|
|
return []byte{}, sdk.ErrUnknownRequest("market not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
bz, err := codec.MarshalJSONIndent(keeper.cdc, oracles)
|
|
|
|
if err != nil {
|
|
|
|
panic("could not marshal result to JSON")
|
|
|
|
}
|
|
|
|
|
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
2020-01-17 12:29:19 +00:00
|
|
|
func queryMarkets(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) (res []byte, sdkErr sdk.Error) {
|
|
|
|
markets := keeper.GetMarkets(ctx)
|
|
|
|
|
|
|
|
bz, err := codec.MarshalJSONIndent(keeper.cdc, markets)
|
|
|
|
if err != nil {
|
2019-11-25 19:46:02 +00:00
|
|
|
panic("could not marshal result to JSON")
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2020-01-17 12:55:17 +00:00
|
|
|
func queryGetParams(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte, sdk.Error) {
|
|
|
|
params := keeper.GetParams(ctx)
|
|
|
|
|
|
|
|
// Encode results
|
|
|
|
bz, err := codec.MarshalJSONIndent(keeper.cdc, params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", err.Error()))
|
|
|
|
}
|
|
|
|
return bz, nil
|
|
|
|
}
|