2019-11-25 19:46:02 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-01-12 15:35:34 +00:00
|
|
|
"strings"
|
2019-11-25 19:46:02 +00:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
|
|
|
"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"
|
2019-11-25 19:46:02 +00:00
|
|
|
|
|
|
|
"github.com/kava-labs/kava/x/cdp/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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(client.GetCommands(
|
2020-01-12 15:35:34 +00:00
|
|
|
QueryCdpCmd(queryRoute, cdc),
|
|
|
|
QueryCdpsByDenomCmd(queryRoute, cdc),
|
|
|
|
QueryCdpsByDenomAndRatioCmd(queryRoute, cdc),
|
2020-01-28 20:08:17 +00:00
|
|
|
QueryCdpDepositsCmd(queryRoute, cdc),
|
2020-01-12 15:35:34 +00:00
|
|
|
QueryParamsCmd(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-01-16 15:20:12 +00:00
|
|
|
Use: "cdp [owner-addr] [collateral-name]",
|
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:
|
|
|
|
$ %s query %s cdp kava15qdefkmwswysgg4qxgqpqr35k3m49pkx2jdfnw uatom
|
|
|
|
`, 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-01-16 15:20:12 +00:00
|
|
|
CollateralDenom: args[1],
|
2020-01-15 14:19:33 +00:00
|
|
|
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-01-12 15:35:34 +00:00
|
|
|
// QueryCdpsByDenomCmd returns the command handler for querying cdps for a collateral type
|
|
|
|
func QueryCdpsByDenomCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
2019-11-25 19:46:02 +00:00
|
|
|
return &cobra.Command{
|
2020-01-16 15:20:12 +00:00
|
|
|
Use: "cdps [collateral-name]",
|
|
|
|
Short: "query CDPs by collateral",
|
|
|
|
Long: strings.TrimSpace(
|
|
|
|
fmt.Sprintf(`List all CDPs collateralized with the specified asset.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
$ %s query %s cdps uatom
|
|
|
|
`, version.ClientName, types.ModuleName)),
|
|
|
|
Args: cobra.ExactArgs(1),
|
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
|
2020-01-12 15:35:34 +00:00
|
|
|
bz, err := cdc.MarshalJSON(types.QueryCdpsParams{CollateralDenom: args[0]})
|
2019-11-25 19:46:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query
|
|
|
|
route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryGetCdps)
|
|
|
|
res, _, err := cliCtx.QueryWithData(route, bz)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode and print results
|
2020-01-28 17:47:08 +00:00
|
|
|
var cdps types.AugmentedCDPs
|
|
|
|
cdc.MustUnmarshalJSON(res, &cdps)
|
|
|
|
return cliCtx.PrintOutput(cdps)
|
2019-11-25 19:46:02 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
// QueryCdpsByDenomAndRatioCmd returns the command handler for querying cdps
|
2020-01-16 15:20:12 +00:00
|
|
|
// that are under the specified collateral ratio
|
2020-01-12 15:35:34 +00:00
|
|
|
func QueryCdpsByDenomAndRatioCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
2019-11-25 19:46:02 +00:00
|
|
|
return &cobra.Command{
|
2020-01-16 15:20:12 +00:00
|
|
|
Use: "cdps-by-ratio [collateral-name] [collateralization-ratio]",
|
|
|
|
Short: "get cdps under a collateralization ratio",
|
|
|
|
Long: strings.TrimSpace(
|
2020-01-28 17:47:08 +00:00
|
|
|
fmt.Sprintf(`List all CDPs under a specified collateralization ratio.
|
2020-01-16 15:20:12 +00:00
|
|
|
Collateralization ratio is: collateral * price / debt.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
$ %s query %s cdps-by-ratio uatom 1.5
|
|
|
|
`, version.ClientName, types.ModuleName)),
|
2020-01-12 15:35:34 +00:00
|
|
|
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
|
2020-01-16 15:20:12 +00:00
|
|
|
ratio, errSdk := sdk.NewDecFromStr(args[1])
|
2019-11-25 19:46:02 +00:00
|
|
|
if errSdk != nil {
|
2020-01-12 15:35:34 +00:00
|
|
|
return fmt.Errorf(errSdk.Error())
|
2019-11-25 19:46:02 +00:00
|
|
|
}
|
2020-01-12 15:35:34 +00:00
|
|
|
bz, err := cdc.MarshalJSON(types.QueryCdpsByRatioParams{
|
|
|
|
CollateralDenom: args[0],
|
2020-01-16 15:20:12 +00:00
|
|
|
Ratio: ratio,
|
2019-11-25 19:46:02 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query
|
2020-01-16 15:20:12 +00:00
|
|
|
route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryGetCdpsByCollateralization)
|
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 cdps types.AugmentedCDPs
|
|
|
|
cdc.MustUnmarshalJSON(res, &cdps)
|
|
|
|
return cliCtx.PrintOutput(cdps)
|
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{
|
|
|
|
Use: "deposits [owner-addr] [collateral-name]",
|
|
|
|
Short: "get deposits for a cdp",
|
|
|
|
Long: strings.TrimSpace(
|
|
|
|
fmt.Sprintf(`Get the deposits of a CDP.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
$ %s query %s deposits kava15qdefkmwswysgg4qxgqpqr35k3m49pkx2jdfnw uatom
|
|
|
|
`, 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{
|
|
|
|
CollateralDenom: 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)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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",
|
|
|
|
Long: "Get the current global cdp 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-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)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|