mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-11-10 18:15:19 +00:00
4599caca07
* query get price implemented - /pricefeed/price/xrp:usd * query rawprices implemented - /pricefeed/rawprices/xrp:usd * refactored to QueryWithMarketIDParams, added rest logic for QueryOracles * new query get oracles implemented for cli and rest - /pricefeed/oracles/xrp:usd * tx postprice implemented - /pricefeed/postprice/{MsgPostPrice} * updated contrib with post-price examples and added to README * added cliCtx.WithHeight(height) and removed import comment
119 lines
3.4 KiB
Go
119 lines
3.4 KiB
Go
package keeper
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
"github.com/kava-labs/kava/x/pricefeed/types"
|
|
)
|
|
|
|
// 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] {
|
|
case types.QueryPrice:
|
|
return queryPrice(ctx, req, keeper)
|
|
case types.QueryRawPrices:
|
|
return queryRawPrices(ctx, req, keeper)
|
|
case types.QueryOracles:
|
|
return queryOracles(ctx, req, keeper)
|
|
case types.QueryMarkets:
|
|
return queryMarkets(ctx, req, keeper)
|
|
case types.QueryGetParams:
|
|
return queryGetParams(ctx, req, keeper)
|
|
default:
|
|
return nil, sdk.ErrUnknownRequest("unknown pricefeed query endpoint")
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
func queryPrice(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))
|
|
}
|
|
_, found := keeper.GetMarket(ctx, requestParams.MarketID)
|
|
if !found {
|
|
return []byte{}, sdk.ErrUnknownRequest("asset not found")
|
|
}
|
|
currentPrice, sdkErr := keeper.GetCurrentPrice(ctx, requestParams.MarketID)
|
|
if sdkErr != nil {
|
|
return nil, sdkErr
|
|
}
|
|
bz, err := codec.MarshalJSONIndent(keeper.cdc, currentPrice)
|
|
if err != nil {
|
|
panic("could not marshal result to JSON")
|
|
}
|
|
|
|
return bz, nil
|
|
}
|
|
|
|
func queryRawPrices(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))
|
|
}
|
|
_, found := keeper.GetMarket(ctx, requestParams.MarketID)
|
|
if !found {
|
|
return []byte{}, sdk.ErrUnknownRequest("asset not found")
|
|
}
|
|
rawPrices := keeper.GetRawPrices(ctx, requestParams.MarketID)
|
|
|
|
bz, err := codec.MarshalJSONIndent(keeper.cdc, rawPrices)
|
|
if err != nil {
|
|
panic("could not marshal result to JSON")
|
|
}
|
|
|
|
return bz, nil
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
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 {
|
|
panic("could not marshal result to JSON")
|
|
}
|
|
|
|
return bz, nil
|
|
}
|
|
|
|
// query params in the pricefeed store
|
|
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
|
|
}
|