2019-11-25 19:46:02 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
2020-05-25 02:27:11 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2019-11-25 19:46:02 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-04-23 16:35:58 +00:00
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
2020-09-17 00:45:10 +00:00
|
|
|
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 {
|
2020-04-23 16:35:58 +00:00
|
|
|
return func(ctx sdk.Context, path []string, req abci.RequestQuery) (res []byte, err error) {
|
2019-11-25 19:46:02 +00:00
|
|
|
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)
|
2020-09-16 18:58:11 +00:00
|
|
|
case types.QueryNextAuctionID:
|
|
|
|
return queryNextAuctionID(ctx, req, keeper)
|
2019-11-25 19:46:02 +00:00
|
|
|
default:
|
2020-04-23 16:35:58 +00:00
|
|
|
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown %s query endpoint", types.ModuleName)
|
2019-11-25 19:46:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 16:35:58 +00:00
|
|
|
func queryAuction(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte, error) {
|
2020-01-21 17:41:37 +00:00
|
|
|
// Decode request
|
|
|
|
var requestParams types.QueryAuctionParams
|
2020-04-23 16:35:58 +00:00
|
|
|
err := types.ModuleCdc.UnmarshalJSON(req.Data, &requestParams)
|
2020-01-21 17:41:37 +00:00
|
|
|
if err != nil {
|
2020-04-23 16:35:58 +00:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2020-01-21 17:41:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup auction
|
|
|
|
auction, found := keeper.GetAuction(ctx, requestParams.AuctionID)
|
|
|
|
if !found {
|
2020-04-23 16:35:58 +00:00
|
|
|
return nil, sdkerrors.Wrapf(types.ErrAuctionNotFound, "%d", requestParams.AuctionID)
|
2020-01-21 17:41:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Encode results
|
|
|
|
bz, err := codec.MarshalJSONIndent(keeper.cdc, auction)
|
|
|
|
if err != nil {
|
2020-04-23 16:35:58 +00:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2020-01-21 17:41:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return bz, nil
|
|
|
|
}
|
2019-11-25 19:46:02 +00:00
|
|
|
|
2020-04-23 16:35:58 +00:00
|
|
|
func queryAuctions(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte, error) {
|
2020-05-25 02:27:11 +00:00
|
|
|
var params types.QueryAllAuctionParams
|
|
|
|
err := types.ModuleCdc.UnmarshalJSON(req.Data, ¶ms)
|
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
unfilteredAuctions := keeper.GetAllAuctions(ctx)
|
|
|
|
auctions := filterAuctions(ctx, unfilteredAuctions, params)
|
|
|
|
if auctions == nil {
|
|
|
|
auctions = types.Auctions{}
|
|
|
|
}
|
|
|
|
|
|
|
|
bz, err := codec.MarshalJSONIndent(keeper.cdc, auctions)
|
2020-01-21 17:41:37 +00:00
|
|
|
if err != nil {
|
2020-04-23 16:35:58 +00:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, 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
|
2020-04-23 16:35:58 +00:00
|
|
|
func queryGetParams(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte, error) {
|
2020-01-16 17:52:29 +00:00
|
|
|
// Get params
|
|
|
|
params := keeper.GetParams(ctx)
|
|
|
|
|
|
|
|
// Encode results
|
|
|
|
bz, err := codec.MarshalJSONIndent(keeper.cdc, params)
|
|
|
|
if err != nil {
|
2020-04-23 16:35:58 +00:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2020-01-16 17:52:29 +00:00
|
|
|
}
|
2020-01-21 17:41:37 +00:00
|
|
|
|
2020-01-16 17:52:29 +00:00
|
|
|
return bz, nil
|
|
|
|
}
|
2020-05-25 02:27:11 +00:00
|
|
|
|
|
|
|
// filterAuctions retrieves auctions filtered by a given set of params.
|
|
|
|
// If no filters are provided, all auctions will be returned in paginated form.
|
|
|
|
func filterAuctions(ctx sdk.Context, auctions types.Auctions, params types.QueryAllAuctionParams) types.Auctions {
|
|
|
|
filteredAuctions := make(types.Auctions, 0, len(auctions))
|
|
|
|
|
|
|
|
for _, auc := range auctions {
|
2020-09-17 00:45:10 +00:00
|
|
|
matchType, matchOwner, matchDenom, matchPhase := true, true, true, true
|
2020-05-25 02:27:11 +00:00
|
|
|
|
|
|
|
// match auction type (if supplied)
|
|
|
|
if len(params.Type) > 0 {
|
|
|
|
matchType = auc.GetType() == params.Type
|
|
|
|
}
|
|
|
|
|
2020-09-17 00:45:10 +00:00
|
|
|
// match auction owner (if supplied)
|
|
|
|
if len(params.Owner) > 0 {
|
|
|
|
if cAuc, ok := auc.(types.CollateralAuction); ok {
|
|
|
|
foundOwnerAddr := false
|
|
|
|
for _, addr := range cAuc.GetLotReturns().Addresses {
|
|
|
|
if addr.Equals(params.Owner) {
|
|
|
|
foundOwnerAddr = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !foundOwnerAddr {
|
|
|
|
matchOwner = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-25 02:27:11 +00:00
|
|
|
// match auction denom (if supplied)
|
|
|
|
if len(params.Denom) > 0 {
|
|
|
|
matchDenom = auc.GetBid().Denom == params.Denom || auc.GetLot().Denom == params.Denom
|
|
|
|
}
|
|
|
|
|
|
|
|
// match auction phase (if supplied)
|
|
|
|
if len(params.Phase) > 0 {
|
|
|
|
matchPhase = auc.GetPhase() == params.Phase
|
|
|
|
}
|
|
|
|
|
2020-09-17 00:45:10 +00:00
|
|
|
if matchType && matchOwner && matchDenom && matchPhase {
|
2020-05-25 02:27:11 +00:00
|
|
|
filteredAuctions = append(filteredAuctions, auc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
start, end := client.Paginate(len(filteredAuctions), params.Page, params.Limit, 100)
|
|
|
|
if start < 0 || end < 0 {
|
|
|
|
filteredAuctions = types.Auctions{}
|
|
|
|
} else {
|
|
|
|
filteredAuctions = filteredAuctions[start:end]
|
|
|
|
}
|
|
|
|
|
|
|
|
return filteredAuctions
|
|
|
|
}
|
2020-09-16 18:58:11 +00:00
|
|
|
|
|
|
|
func queryNextAuctionID(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte, error) {
|
|
|
|
nextAuctionID, _ := keeper.GetNextAuctionID(ctx)
|
|
|
|
|
|
|
|
bz, err := types.ModuleCdc.MarshalJSON(nextAuctionID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
|
|
|
}
|
|
|
|
return bz, nil
|
|
|
|
}
|