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

77 lines
2.2 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 (
"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] {
2019-11-27 14:45:59 +00:00
case types.QueryCurrentPrice:
2019-11-25 19:46:02 +00:00
return queryCurrentPrice(ctx, path[1:], req, keeper)
2019-11-27 14:45:59 +00:00
case types.QueryRawPrices:
2019-11-25 19:46:02 +00:00
return queryRawPrices(ctx, path[1:], req, keeper)
2019-11-27 14:45:59 +00:00
case types.QueryAssets:
2019-11-25 19:46:02 +00:00
return queryAssets(ctx, req, keeper)
default:
return nil, sdk.ErrUnknownRequest("unknown pricefeed query endpoint")
}
}
}
func queryCurrentPrice(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) (res []byte, err sdk.Error) {
2019-12-04 16:32:08 +00:00
marketID := path[0]
_, found := keeper.GetMarket(ctx, marketID)
2019-11-25 19:46:02 +00:00
if !found {
return []byte{}, sdk.ErrUnknownRequest("asset not found")
}
2019-12-04 16:32:08 +00:00
currentPrice := keeper.GetCurrentPrice(ctx, marketID)
2019-11-25 19:46:02 +00:00
bz, err2 := codec.MarshalJSONIndent(keeper.cdc, currentPrice)
if err2 != nil {
panic("could not marshal result to JSON")
}
return bz, nil
}
func queryRawPrices(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) (res []byte, err sdk.Error) {
2019-11-27 14:45:59 +00:00
var priceList types.QueryRawPricesResp
2019-12-04 16:32:08 +00:00
marketID := path[0]
_, found := keeper.GetMarket(ctx, marketID)
2019-11-25 19:46:02 +00:00
if !found {
return []byte{}, sdk.ErrUnknownRequest("asset not found")
}
2019-12-04 16:32:08 +00:00
rawPrices := keeper.GetRawPrices(ctx, marketID)
2019-11-25 19:46:02 +00:00
for _, price := range rawPrices {
priceList = append(priceList, price.String())
}
bz, err2 := codec.MarshalJSONIndent(keeper.cdc, priceList)
if err2 != nil {
panic("could not marshal result to JSON")
}
return bz, nil
}
func queryAssets(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) (res []byte, err sdk.Error) {
2019-11-27 14:45:59 +00:00
var assetList types.QueryAssetsResp
2019-12-04 16:32:08 +00:00
assets := keeper.GetMarketParams(ctx)
2019-11-25 19:46:02 +00:00
for _, asset := range assets {
assetList = append(assetList, asset.String())
}
bz, err2 := codec.MarshalJSONIndent(keeper.cdc, assetList)
if err2 != nil {
panic("could not marshal result to JSON")
}
return bz, nil
2019-11-27 14:45:59 +00:00
}