2019-11-25 19:46:02 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-09-17 00:45:10 +00:00
|
|
|
"strconv"
|
2020-01-12 15:35:34 +00:00
|
|
|
"strings"
|
2020-10-03 23:14:42 +00:00
|
|
|
"time"
|
2019-11-25 19:46:02 +00:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2020-09-17 00:45:10 +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"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-01-16 15:20:12 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/version"
|
2020-06-19 19:30:10 +00:00
|
|
|
supply "github.com/cosmos/cosmos-sdk/x/supply"
|
2019-11-25 19:46:02 +00:00
|
|
|
|
|
|
|
"github.com/kava-labs/kava/x/cdp/types"
|
|
|
|
)
|
|
|
|
|
2020-09-17 00:45:10 +00:00
|
|
|
// 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",
|
|
|
|
}
|
|
|
|
|
2020-04-23 16:35:58 +00:00
|
|
|
cdpQueryCmd.AddCommand(flags.GetCommands(
|
2020-01-12 15:35:34 +00:00
|
|
|
QueryCdpCmd(queryRoute, cdc),
|
2020-09-17 00:45:10 +00:00
|
|
|
QueryGetCdpsCmd(queryRoute, cdc),
|
2020-01-28 20:08:17 +00:00
|
|
|
QueryCdpDepositsCmd(queryRoute, cdc),
|
2020-01-12 15:35:34 +00:00
|
|
|
QueryParamsCmd(queryRoute, cdc),
|
2020-06-19 19:30:10 +00:00
|
|
|
QueryGetAccounts(queryRoute, cdc),
|
2020-09-17 00:45:10 +00:00
|
|
|
QueryGetSavingsRateDistributed(queryRoute, cdc),
|
2020-10-03 23:14:42 +00:00
|
|
|
QueryGetSavingsRateDistTime(queryRoute, cdc),
|
2019-11-25 19:46:02 +00:00
|
|
|
)...)
|
|
|
|
|
|
|
|
return cdpQueryCmd
|
|
|
|
}
|
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
// 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{
|
2020-08-21 19:42:46 +00:00
|
|
|
Use: "cdp [owner-addr] [collateral-type]",
|
2019-11-25 19:46:02 +00:00
|
|
|
Short: "get info about a cdp",
|
2020-01-16 15:20:12 +00:00
|
|
|
Long: strings.TrimSpace(
|
|
|
|
fmt.Sprintf(`Get a CDP by the owner address and the collateral name.
|
|
|
|
|
|
|
|
Example:
|
2020-08-21 19:42:46 +00:00
|
|
|
$ %s query %s cdp kava15qdefkmwswysgg4qxgqpqr35k3m49pkx2jdfnw atom-a
|
2020-01-16 15:20:12 +00:00
|
|
|
`, 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
|
|
|
|
}
|
2020-01-15 14:19:33 +00:00
|
|
|
bz, err := cdc.MarshalJSON(types.QueryCdpParams{
|
2020-08-21 19:42:46 +00:00
|
|
|
CollateralType: args[1],
|
|
|
|
Owner: ownerAddress,
|
2019-11-25 19:46:02 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query
|
2020-01-12 15:35:34 +00:00
|
|
|
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
|
2020-01-28 17:47:08 +00:00
|
|
|
var cdp types.AugmentedCDP
|
2020-01-12 15:35:34 +00:00
|
|
|
cdc.MustUnmarshalJSON(res, &cdp)
|
|
|
|
return cliCtx.PrintOutput(cdp)
|
2019-11-25 19:46:02 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-17 00:45:10 +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:
|
2020-01-16 15:20:12 +00:00
|
|
|
Example:
|
2020-09-17 00:45:10 +00:00
|
|
|
$ 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 {
|
2020-09-17 00:45:10 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-09-17 00:45:10 +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
|
|
|
}
|
|
|
|
|
2020-09-17 00:45:10 +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
|
|
|
|
2020-09-17 00:45:10 +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
|
|
|
}
|
2020-09-17 00:45:10 +00:00
|
|
|
|
|
|
|
bz, err := cdc.MarshalJSON(params)
|
2019-11-25 19:46:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-17 00:45:10 +00:00
|
|
|
cliCtx := context.NewCLIContext().WithCodec(cdc)
|
|
|
|
|
2019-11-25 19:46:02 +00:00
|
|
|
// Query
|
2020-09-17 00:45:10 +00:00
|
|
|
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
|
2020-09-17 00:45:10 +00:00
|
|
|
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
|
|
|
},
|
|
|
|
}
|
2020-09-17 00:45:10 +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
|
|
|
}
|
|
|
|
|
2020-01-28 20:08:17 +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{
|
2020-08-21 19:42:46 +00:00
|
|
|
Use: "deposits [owner-addr] [collateral-type]",
|
2020-01-28 20:08:17 +00:00
|
|
|
Short: "get deposits for a cdp",
|
|
|
|
Long: strings.TrimSpace(
|
|
|
|
fmt.Sprintf(`Get the deposits of a CDP.
|
|
|
|
|
|
|
|
Example:
|
2020-08-21 19:42:46 +00:00
|
|
|
$ %s query %s deposits kava15qdefkmwswysgg4qxgqpqr35k3m49pkx2jdfnw atom-a
|
2020-01-28 20:08:17 +00:00
|
|
|
`, 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{
|
2020-08-21 19:42:46 +00:00
|
|
|
CollateralType: args[1],
|
|
|
|
Owner: ownerAddress,
|
2020-01-28 20:08:17 +00:00
|
|
|
})
|
|
|
|
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)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
// 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",
|
2020-09-17 00:45:10 +00:00
|
|
|
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)
|
2020-01-12 15:35:34 +00:00
|
|
|
res, _, err := cliCtx.QueryWithData(route, nil)
|
2019-11-25 19:46:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode and print results
|
2020-01-16 15:20:12 +00:00
|
|
|
var out types.Params
|
2019-11-25 19:46:02 +00:00
|
|
|
cdc.MustUnmarshalJSON(res, &out)
|
|
|
|
return cliCtx.PrintOutput(out)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2020-06-19 19:30:10 +00:00
|
|
|
|
2020-09-17 00:45:10 +00:00
|
|
|
// QueryGetAccounts queries CDP module accounts
|
2020-06-19 19:30:10 +00:00
|
|
|
func QueryGetAccounts(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
|
|
|
return &cobra.Command{
|
|
|
|
Use: "accounts",
|
2020-09-17 00:45:10 +00:00
|
|
|
Short: "get module accounts",
|
|
|
|
Long: "get cdp module account addresses",
|
2020-06-19 19:30:10 +00:00
|
|
|
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)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2020-09-17 00:45:10 +00:00
|
|
|
|
|
|
|
// QueryGetSavingsRateDistributed queries the total amount of savings rate distributed in USDX
|
|
|
|
func QueryGetSavingsRateDistributed(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
|
|
|
return &cobra.Command{
|
|
|
|
Use: "savings-rate-dist",
|
|
|
|
Short: "get total amount of savings rate distributed in USDX",
|
|
|
|
Long: "get total amount of savings rate distributed",
|
|
|
|
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.QueryGetSavingsRateDistributed), nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
|
|
|
|
|
|
|
// Decode and print results
|
|
|
|
var out sdk.Int
|
|
|
|
if err := cdc.UnmarshalJSON(res, &out); err != nil {
|
|
|
|
return fmt.Errorf("failed to unmarshal sdk.Int: %w", err)
|
|
|
|
}
|
|
|
|
return cliCtx.PrintOutput(out)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2020-10-03 23:14:42 +00:00
|
|
|
|
|
|
|
// QueryGetSavingsRateDistributed queries the total amount of savings rate distributed in USDX
|
|
|
|
func QueryGetSavingsRateDistTime(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
|
|
|
return &cobra.Command{
|
|
|
|
Use: "savings-rate-dist-time",
|
|
|
|
Short: "get the previous savings rate distribution time",
|
|
|
|
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.QueryGetPreviousSavingsDistributionTime), nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
|
|
|
|
|
|
|
// Decode and print results
|
|
|
|
var out time.Time
|
|
|
|
if err := cdc.UnmarshalJSON(res, &out); err != nil {
|
|
|
|
return fmt.Errorf("failed to unmarshal time.Time: %w", err)
|
|
|
|
}
|
|
|
|
return cliCtx.PrintOutput(out)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|