mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-11-10 10:05:18 +00:00
3375484f79
* Use cosmossdk.io/errors for deprecated error methods * Update error registration with cosmossdk.io/errors * Use cosmossdk.io/math for deprecated sdk.Int alias * Fix modified proto file * Update sdk.Int usage in swap hooks * Update e2e test deprecated method usage
139 lines
4.6 KiB
Go
139 lines
4.6 KiB
Go
package keeper
|
|
|
|
import (
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
errorsmod "cosmossdk.io/errors"
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
|
|
"github.com/kava-labs/kava/x/pricefeed/types"
|
|
)
|
|
|
|
// 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) {
|
|
switch path[0] {
|
|
case types.QueryPrice:
|
|
return queryPrice(ctx, req, keeper, legacyQuerierCdc)
|
|
case types.QueryPrices:
|
|
return queryPrices(ctx, req, keeper, legacyQuerierCdc)
|
|
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)
|
|
default:
|
|
return nil, errorsmod.Wrapf(sdkerrors.ErrUnknownRequest, "unknown %s query endpoint", types.ModuleName)
|
|
}
|
|
}
|
|
}
|
|
|
|
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, errorsmod.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
|
}
|
|
_, found := keeper.GetMarket(ctx, requestParams.MarketID)
|
|
if !found {
|
|
return nil, errorsmod.Wrap(types.ErrAssetNotFound, requestParams.MarketID)
|
|
}
|
|
currentPrice, sdkErr := keeper.GetCurrentPrice(ctx, requestParams.MarketID)
|
|
if sdkErr != nil {
|
|
return nil, sdkErr
|
|
}
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, currentPrice)
|
|
if err != nil {
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
|
}
|
|
|
|
return bz, nil
|
|
}
|
|
|
|
func queryPrices(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) (res []byte, sdkErr error) {
|
|
currentPrices := keeper.GetCurrentPrices(ctx)
|
|
|
|
// 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 {
|
|
return nil, errorsmod.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, errorsmod.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
|
}
|
|
_, found := keeper.GetMarket(ctx, requestParams.MarketID)
|
|
if !found {
|
|
return nil, errorsmod.Wrap(types.ErrAssetNotFound, requestParams.MarketID)
|
|
}
|
|
|
|
rawPrices := keeper.GetRawPrices(ctx, requestParams.MarketID)
|
|
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, rawPrices)
|
|
if err != nil {
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
|
}
|
|
|
|
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, errorsmod.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
|
}
|
|
|
|
oracles, err := keeper.GetOracles(ctx, requestParams.MarketID)
|
|
if err != nil {
|
|
return nil, errorsmod.Wrap(types.ErrAssetNotFound, requestParams.MarketID)
|
|
}
|
|
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, oracles)
|
|
if err != nil {
|
|
return nil, errorsmod.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, errorsmod.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
|
}
|
|
|
|
return bz, nil
|
|
}
|
|
|
|
// 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, errorsmod.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
|
}
|
|
return bz, nil
|
|
}
|