2019-11-25 19:46:02 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-01-21 17:41:37 +00:00
|
|
|
"strconv"
|
2020-05-25 02:27:11 +00:00
|
|
|
"strings"
|
2019-11-25 19:46:02 +00:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2020-05-25 02:27:11 +00:00
|
|
|
"github.com/spf13/viper"
|
2019-11-25 19:46:02 +00:00
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
2020-04-23 16:35:58 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
2019-11-25 19:46:02 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2020-05-25 02:27:11 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-01-21 17:41:37 +00:00
|
|
|
|
2019-11-25 19:46:02 +00:00
|
|
|
"github.com/kava-labs/kava/x/auction/types"
|
|
|
|
)
|
|
|
|
|
2020-05-25 02:27:11 +00:00
|
|
|
// Query auction flags
|
|
|
|
const (
|
|
|
|
flagType = "type"
|
|
|
|
flagDenom = "denom"
|
|
|
|
flagPhase = "phase"
|
|
|
|
)
|
|
|
|
|
2019-11-25 19:46:02 +00:00
|
|
|
// GetQueryCmd returns the cli query commands for this module
|
|
|
|
func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
|
|
|
// Group nameservice queries under a subcommand
|
|
|
|
auctionQueryCmd := &cobra.Command{
|
|
|
|
Use: "auction",
|
|
|
|
Short: "Querying commands for the auction module",
|
|
|
|
}
|
|
|
|
|
2020-04-23 16:35:58 +00:00
|
|
|
auctionQueryCmd.AddCommand(flags.GetCommands(
|
2020-01-21 17:41:37 +00:00
|
|
|
QueryGetAuctionCmd(queryRoute, cdc),
|
2020-01-16 17:52:29 +00:00
|
|
|
QueryGetAuctionsCmd(queryRoute, cdc),
|
|
|
|
QueryParamsCmd(queryRoute, cdc),
|
2019-11-25 19:46:02 +00:00
|
|
|
)...)
|
|
|
|
|
|
|
|
return auctionQueryCmd
|
|
|
|
}
|
|
|
|
|
2020-01-21 17:41:37 +00:00
|
|
|
// QueryGetAuctionCmd queries one auction in the store
|
|
|
|
func QueryGetAuctionCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
|
|
|
return &cobra.Command{
|
|
|
|
Use: "auction [auction-id]",
|
|
|
|
Short: "get a info about an auction",
|
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
cliCtx := context.NewCLIContext().WithCodec(cdc)
|
|
|
|
|
|
|
|
// Prepare params for querier
|
|
|
|
id, err := strconv.ParseUint(args[0], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("auction-id '%s' not a valid uint", args[0])
|
|
|
|
}
|
|
|
|
bz, err := cdc.MarshalJSON(types.QueryAuctionParams{
|
|
|
|
AuctionID: id,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query
|
2020-05-25 02:27:11 +00:00
|
|
|
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryGetAuction), bz)
|
2020-01-21 17:41:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode and print results
|
|
|
|
var auction types.Auction
|
|
|
|
cdc.MustUnmarshalJSON(res, &auction)
|
2020-01-29 14:42:03 +00:00
|
|
|
auctionWithPhase := types.NewAuctionWithPhase(auction)
|
2020-05-25 02:27:11 +00:00
|
|
|
|
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
2020-01-29 14:42:03 +00:00
|
|
|
return cliCtx.PrintOutput(auctionWithPhase)
|
2020-01-21 17:41:37 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-16 17:52:29 +00:00
|
|
|
// QueryGetAuctionsCmd queries the auctions in the store
|
|
|
|
func QueryGetAuctionsCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
2020-05-25 02:27:11 +00:00
|
|
|
cmd := &cobra.Command{
|
2020-01-16 17:52:29 +00:00
|
|
|
Use: "auctions",
|
2020-05-25 02:27:11 +00:00
|
|
|
Short: "query auctions with optional filters",
|
|
|
|
Long: strings.TrimSpace(`Query for all paginated auctions that match optional filters:
|
|
|
|
Example:
|
|
|
|
$ kvcli q auction auctions --type=(collateral|surplus|debt)
|
|
|
|
$ kvcli q auction auctions --denom=bnb
|
|
|
|
$ kvcli q auction auctions --phase=(forward|reverse)
|
|
|
|
$ kvcli q auction auctions --page=2 --limit=100
|
|
|
|
`,
|
|
|
|
),
|
2019-11-25 19:46:02 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2020-05-25 02:27:11 +00:00
|
|
|
strType := viper.GetString(flagType)
|
|
|
|
strDenom := viper.GetString(flagDenom)
|
|
|
|
strPhase := viper.GetString(flagPhase)
|
|
|
|
page := viper.GetInt(flags.FlagPage)
|
|
|
|
limit := viper.GetInt(flags.FlagLimit)
|
|
|
|
|
|
|
|
var (
|
|
|
|
auctionType string
|
|
|
|
auctionDenom string
|
|
|
|
auctionPhase string
|
|
|
|
)
|
|
|
|
|
|
|
|
params := types.NewQueryAllAuctionParams(page, limit, auctionType, auctionDenom, auctionPhase)
|
|
|
|
|
|
|
|
if len(strType) != 0 {
|
|
|
|
auctionType = strings.ToLower(strings.TrimSpace(strType))
|
|
|
|
if auctionType != types.CollateralAuctionType &&
|
|
|
|
auctionType != types.SurplusAuctionType &&
|
|
|
|
auctionType != types.DebtAuctionType {
|
|
|
|
return fmt.Errorf("invalid auction type %s", strType)
|
|
|
|
}
|
|
|
|
params.Type = auctionType
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(strDenom) != 0 {
|
|
|
|
auctionDenom := strings.TrimSpace(strDenom)
|
|
|
|
err := sdk.ValidateDenom(auctionDenom)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
params.Denom = auctionDenom
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(strPhase) != 0 {
|
|
|
|
auctionPhase := strings.ToLower(strings.TrimSpace(strPhase))
|
|
|
|
if auctionType != types.CollateralAuctionType && len(auctionType) > 0 {
|
|
|
|
return fmt.Errorf("cannot apply phase flag to non-collateral auction type")
|
|
|
|
}
|
|
|
|
if auctionPhase != types.ForwardAuctionPhase && auctionPhase != types.ReverseAuctionPhase {
|
|
|
|
return fmt.Errorf("invalid auction phase %s", strPhase)
|
|
|
|
}
|
|
|
|
params.Phase = auctionPhase
|
|
|
|
}
|
|
|
|
|
|
|
|
bz, err := cdc.MarshalJSON(params)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-11-25 19:46:02 +00:00
|
|
|
cliCtx := context.NewCLIContext().WithCodec(cdc)
|
2020-01-21 17:41:37 +00:00
|
|
|
|
|
|
|
// Query
|
2020-05-25 02:27:11 +00:00
|
|
|
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryGetAuctions), bz)
|
2019-11-25 19:46:02 +00:00
|
|
|
if err != nil {
|
2020-01-21 17:41:37 +00:00
|
|
|
return err
|
2019-11-25 19:46:02 +00:00
|
|
|
}
|
2020-01-21 17:41:37 +00:00
|
|
|
|
|
|
|
// Decode and print results
|
2020-05-25 02:27:11 +00:00
|
|
|
var matchingAuctions types.Auctions
|
|
|
|
cdc.MustUnmarshalJSON(res, &matchingAuctions)
|
|
|
|
|
|
|
|
if len(matchingAuctions) == 0 {
|
|
|
|
return fmt.Errorf("No matching auctions found")
|
|
|
|
}
|
2020-01-29 14:42:03 +00:00
|
|
|
|
2020-02-03 15:54:00 +00:00
|
|
|
auctionsWithPhase := []types.AuctionWithPhase{} // using empty slice so json returns [] instead of null when there's no auctions
|
2020-05-25 02:27:11 +00:00
|
|
|
for _, a := range matchingAuctions {
|
2020-01-29 14:42:03 +00:00
|
|
|
auctionsWithPhase = append(auctionsWithPhase, types.NewAuctionWithPhase(a))
|
|
|
|
}
|
2020-05-25 02:27:11 +00:00
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
|
|
|
return cliCtx.PrintOutput(auctionsWithPhase) // nolint:errcheck
|
2019-11-25 19:46:02 +00:00
|
|
|
},
|
|
|
|
}
|
2020-05-25 02:27:11 +00:00
|
|
|
|
|
|
|
cmd.Flags().Int(flags.FlagPage, 1, "pagination page of auctions to to query for")
|
|
|
|
cmd.Flags().Int(flags.FlagLimit, 100, "pagination limit of auctions to query for")
|
|
|
|
cmd.Flags().String(flagType, "", "(optional) filter by auction type, type: collateral, debt, surplus")
|
|
|
|
cmd.Flags().String(flagDenom, "", "(optional) filter by auction denom")
|
|
|
|
cmd.Flags().String(flagPhase, "", "(optional) filter by collateral auction phase, phase: forward/reverse")
|
|
|
|
|
|
|
|
return cmd
|
2019-11-25 19:46:02 +00:00
|
|
|
}
|
2020-01-16 17:52:29 +00:00
|
|
|
|
|
|
|
// QueryParamsCmd queries the auction module parameters
|
|
|
|
func QueryParamsCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
|
|
|
return &cobra.Command{
|
|
|
|
Use: "params",
|
|
|
|
Short: "get the auction module parameters",
|
|
|
|
Long: "Get the current global auction module parameters.",
|
|
|
|
Args: cobra.NoArgs,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
cliCtx := context.NewCLIContext().WithCodec(cdc)
|
|
|
|
|
|
|
|
// Query
|
|
|
|
route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryGetParams)
|
2020-05-25 02:27:11 +00:00
|
|
|
res, height, err := cliCtx.QueryWithData(route, nil)
|
2020-01-16 17:52:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode and print results
|
|
|
|
var out types.Params
|
|
|
|
cdc.MustUnmarshalJSON(res, &out)
|
2020-05-25 02:27:11 +00:00
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
2020-01-16 17:52:29 +00:00
|
|
|
return cliCtx.PrintOutput(out)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|