2019-11-25 19:46:02 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
2020-01-21 17:41:37 +00:00
|
|
|
|
|
|
|
"github.com/kava-labs/kava/x/auction/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] {
|
|
|
|
case types.QueryGetAuction:
|
2020-01-21 17:41:37 +00:00
|
|
|
return queryAuction(ctx, req, keeper)
|
|
|
|
case types.QueryGetAuctions:
|
2019-11-25 19:46:02 +00:00
|
|
|
return queryAuctions(ctx, req, keeper)
|
2020-01-16 17:52:29 +00:00
|
|
|
case types.QueryGetParams:
|
|
|
|
return queryGetParams(ctx, req, keeper)
|
2019-11-25 19:46:02 +00:00
|
|
|
default:
|
|
|
|
return nil, sdk.ErrUnknownRequest("unknown auction query endpoint")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-21 17:41:37 +00:00
|
|
|
func queryAuction(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte, sdk.Error) {
|
|
|
|
// Decode request
|
|
|
|
var requestParams types.QueryAuctionParams
|
|
|
|
err := keeper.cdc.UnmarshalJSON(req.Data, &requestParams)
|
|
|
|
if err != nil {
|
|
|
|
return nil, sdk.ErrUnknownRequest(sdk.AppendMsgToErr("incorrectly formatted request data", err.Error()))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup auction
|
|
|
|
auction, found := keeper.GetAuction(ctx, requestParams.AuctionID)
|
|
|
|
if !found {
|
|
|
|
return nil, types.ErrAuctionNotFound(types.DefaultCodespace, requestParams.AuctionID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode results
|
|
|
|
bz, err := codec.MarshalJSONIndent(keeper.cdc, auction)
|
|
|
|
if err != nil {
|
|
|
|
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", err.Error()))
|
|
|
|
}
|
|
|
|
|
|
|
|
return bz, nil
|
|
|
|
}
|
2019-11-25 19:46:02 +00:00
|
|
|
|
2020-01-21 17:41:37 +00:00
|
|
|
func queryAuctions(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte, sdk.Error) {
|
|
|
|
// Get all auctions
|
|
|
|
auctionsList := types.Auctions{}
|
2020-01-12 15:12:22 +00:00
|
|
|
keeper.IterateAuctions(ctx, func(a types.Auction) bool {
|
2020-01-14 15:04:47 +00:00
|
|
|
auctionsList = append(auctionsList, a)
|
2020-01-12 15:12:22 +00:00
|
|
|
return false
|
|
|
|
})
|
2019-11-25 19:46:02 +00:00
|
|
|
|
2020-01-21 17:41:37 +00:00
|
|
|
// Encode Results
|
|
|
|
bz, err := codec.MarshalJSONIndent(keeper.cdc, auctionsList)
|
|
|
|
if err != nil {
|
|
|
|
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", err.Error()))
|
2019-11-25 19:46:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return bz, nil
|
|
|
|
}
|
2020-01-16 17:52:29 +00:00
|
|
|
|
|
|
|
// query params in the auction store
|
|
|
|
func queryGetParams(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte, sdk.Error) {
|
|
|
|
// Get params
|
|
|
|
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()))
|
|
|
|
}
|
2020-01-21 17:41:37 +00:00
|
|
|
|
2020-01-16 17:52:29 +00:00
|
|
|
return bz, nil
|
|
|
|
}
|