0g-chain/x/cdp/client/cli/query.go

284 lines
8.1 KiB
Go
Raw Normal View History

2019-11-25 19:46:02 +00:00
package cli
import (
"fmt"
"strconv"
"strings"
2019-11-25 19:46:02 +00:00
"github.com/spf13/cobra"
"github.com/spf13/viper"
2019-11-25 19:46:02 +00:00
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/flags"
2019-11-25 19:46:02 +00:00
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
supply "github.com/cosmos/cosmos-sdk/x/supply"
2019-11-25 19:46:02 +00:00
"github.com/kava-labs/kava/x/cdp/types"
)
// Query CDP flags
const (
flagCollateralType = "collateral-type"
flagOwner = "owner"
flagID = "id"
flagRatio = "ratio" // returns CDPs under the given collateralization ratio threshold
)
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
cdpQueryCmd := &cobra.Command{
Use: "cdp",
Short: "Querying commands for the cdp module",
}
cdpQueryCmd.AddCommand(flags.GetCommands(
QueryCdpCmd(queryRoute, cdc),
QueryGetCdpsCmd(queryRoute, cdc),
QueryCdpDepositsCmd(queryRoute, cdc),
QueryParamsCmd(queryRoute, cdc),
QueryGetAccounts(queryRoute, cdc),
2019-11-25 19:46:02 +00:00
)...)
return cdpQueryCmd
}
// QueryCdpCmd returns the command handler for querying a particular cdp
func QueryCdpCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
2019-11-25 19:46:02 +00:00
return &cobra.Command{
Use: "cdp [owner-addr] [collateral-type]",
2019-11-25 19:46:02 +00:00
Short: "get info about a cdp",
Long: strings.TrimSpace(
fmt.Sprintf(`Get a CDP by the owner address and the collateral name.
Example:
$ %s query %s cdp kava15qdefkmwswysgg4qxgqpqr35k3m49pkx2jdfnw atom-a
`, version.ClientName, types.ModuleName)),
Args: cobra.ExactArgs(2),
2019-11-25 19:46:02 +00:00
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
// Prepare params for querier
ownerAddress, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return err
}
bz, err := cdc.MarshalJSON(types.QueryCdpParams{
CollateralType: args[1],
Owner: ownerAddress,
2019-11-25 19:46:02 +00:00
})
if err != nil {
return err
}
// Query
route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryGetCdp)
2019-11-25 19:46:02 +00:00
res, _, err := cliCtx.QueryWithData(route, bz)
if err != nil {
return err
}
// Decode and print results
var cdp types.AugmentedCDP
cdc.MustUnmarshalJSON(res, &cdp)
return cliCtx.PrintOutput(cdp)
2019-11-25 19:46:02 +00:00
},
}
}
// QueryGetCdpsCmd queries the cdps in the store
func QueryGetCdpsCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "cdps",
Short: "query cdps with optional filters",
Long: strings.TrimSpace(`Query for all paginated cdps that match optional filters:
Example:
$ kvcli q cdp cdps --collateral-type=bnb
$ kvcli q cdp cdps --owner=kava1hatdq32u5x4wnxrtv5wzjzmq49sxgjgsj0mffm
$ kvcli q cdp cdps --id=21
$ kvcli q cdp cdps --ratio=2.75
$ kvcli q cdp cdps --page=2 --limit=100
`,
),
2019-11-25 19:46:02 +00:00
RunE: func(cmd *cobra.Command, args []string) error {
strCollateralType := viper.GetString(flagCollateralType)
strOwner := viper.GetString(flagOwner)
strID := viper.GetString(flagID)
strRatio := viper.GetString(flagRatio)
page := viper.GetInt(flags.FlagPage)
limit := viper.GetInt(flags.FlagLimit)
var (
cdpCollateralType string
cdpOwner sdk.AccAddress
cdpID uint64
cdpRatio sdk.Dec
)
params := types.NewQueryCdpsParams(page, limit, cdpCollateralType, cdpOwner, cdpID, cdpRatio)
if len(strCollateralType) != 0 {
cdpCollateralType = strings.ToLower(strings.TrimSpace(strCollateralType))
params.CollateralType = cdpCollateralType
2019-11-25 19:46:02 +00:00
}
if len(strOwner) != 0 {
cdpOwner, err := sdk.AccAddressFromBech32(strings.ToLower(strings.TrimSpace(strOwner)))
if err != nil {
return fmt.Errorf("cannot parse address from cdp owner %s", strOwner)
}
params.Owner = cdpOwner
2019-11-25 19:46:02 +00:00
}
if len(strID) != 0 {
cdpID, err := strconv.ParseUint(strID, 10, 64)
if err != nil {
return fmt.Errorf("cannot parse cdp ID %s", strID)
}
params.ID = cdpID
}
2019-11-25 19:46:02 +00:00
params.Ratio = sdk.ZeroDec()
if len(strRatio) != 0 {
cdpRatio, err := sdk.NewDecFromStr(strRatio)
if err != nil {
return fmt.Errorf("cannot parse cdp ratio %s", strRatio)
}
params.Ratio = cdpRatio
} else {
// Set to sdk.Dec(0) so that if not specified in params it doesn't panic when unmarshaled
params.Ratio = sdk.ZeroDec()
2019-11-25 19:46:02 +00:00
}
bz, err := cdc.MarshalJSON(params)
2019-11-25 19:46:02 +00:00
if err != nil {
return err
}
cliCtx := context.NewCLIContext().WithCodec(cdc)
2019-11-25 19:46:02 +00:00
// Query
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryGetCdps), bz)
2019-11-25 19:46:02 +00:00
if err != nil {
return err
}
// Decode and print results
var matchingCDPs types.AugmentedCDPs
cdc.MustUnmarshalJSON(res, &matchingCDPs)
if len(matchingCDPs) == 0 {
return fmt.Errorf("No matching CDPs found")
}
cliCtx = cliCtx.WithHeight(height)
return cliCtx.PrintOutput(matchingCDPs) // nolint:errcheck
2019-11-25 19:46:02 +00:00
},
}
cmd.Flags().Int(flags.FlagPage, 1, "pagination page of CDPs to to query for")
cmd.Flags().Int(flags.FlagLimit, 100, "pagination limit of CDPs to query for")
cmd.Flags().String(flagCollateralType, "", "(optional) filter by CDP collateral type")
cmd.Flags().String(flagOwner, "", "(optional) filter by CDP owner")
cmd.Flags().String(flagID, "", "(optional) filter by CDP ID")
cmd.Flags().String(flagRatio, "", "(optional) filter by CDP collateralization ratio threshold")
return cmd
2019-11-25 19:46:02 +00:00
}
// QueryCdpDepositsCmd returns the command handler for querying the deposits of a particular cdp
func QueryCdpDepositsCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "deposits [owner-addr] [collateral-type]",
Short: "get deposits for a cdp",
Long: strings.TrimSpace(
fmt.Sprintf(`Get the deposits of a CDP.
Example:
$ %s query %s deposits kava15qdefkmwswysgg4qxgqpqr35k3m49pkx2jdfnw atom-a
`, version.ClientName, types.ModuleName)),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
// Prepare params for querier
ownerAddress, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return err
}
bz, err := cdc.MarshalJSON(types.QueryCdpParams{
CollateralType: args[1],
Owner: ownerAddress,
})
if err != nil {
return err
}
// Query
route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryGetCdpDeposits)
res, _, err := cliCtx.QueryWithData(route, bz)
if err != nil {
return err
}
// Decode and print results
var deposits types.Deposits
cdc.MustUnmarshalJSON(res, &deposits)
return cliCtx.PrintOutput(deposits)
},
}
}
// QueryParamsCmd returns the command handler for cdp parameter querying
func QueryParamsCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
2019-11-25 19:46:02 +00:00
return &cobra.Command{
Use: "params",
Short: "get the cdp module parameters",
Long: "get the current global cdp module parameters.",
2019-11-25 19:46:02 +00:00
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)
res, _, err := cliCtx.QueryWithData(route, nil)
2019-11-25 19:46:02 +00:00
if err != nil {
return err
}
// Decode and print results
var out types.Params
2019-11-25 19:46:02 +00:00
cdc.MustUnmarshalJSON(res, &out)
return cliCtx.PrintOutput(out)
},
}
}
// QueryGetAccounts queries CDP module accounts
func QueryGetAccounts(queryRoute string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "accounts",
Short: "get module accounts",
Long: "get cdp module account addresses",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
// Query
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryGetAccounts), nil)
if err != nil {
return err
}
cliCtx = cliCtx.WithHeight(height)
// Decode and print results
var out []supply.ModuleAccount
if err := cdc.UnmarshalJSON(res, &out); err != nil {
return fmt.Errorf("failed to unmarshal accounts: %w", err)
}
return cliCtx.PrintOutput(out)
},
}
}