cdp cli clean up (#305)

* clean up querier cmds #304 #298 #296

* #299 address cli ux issues

* fix typo

* edit help message
This commit is contained in:
Ruaridh 2020-01-16 16:20:12 +01:00 committed by Kevin Davis
parent a9c92439c6
commit 2d9820b3d1
5 changed files with 104 additions and 70 deletions

View File

@ -10,6 +10,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
"github.com/kava-labs/kava/x/cdp/types" "github.com/kava-labs/kava/x/cdp/types"
) )
@ -35,9 +36,15 @@ func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
// QueryCdpCmd returns the command handler for querying a particular cdp // QueryCdpCmd returns the command handler for querying a particular cdp
func QueryCdpCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { func QueryCdpCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{ return &cobra.Command{
Use: "cdp [ownerAddress] [collateralType]", Use: "cdp [owner-addr] [collateral-name]",
Short: "get info about a cdp", Short: "get info about a cdp",
Args: cobra.ExactArgs(2), 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),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc) cliCtx := context.NewCLIContext().WithCodec(cdc)
@ -46,9 +53,8 @@ func QueryCdpCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
if err != nil { if err != nil {
return err return err
} }
collateralType := args[1] // TODO validation?
bz, err := cdc.MarshalJSON(types.QueryCdpParams{ bz, err := cdc.MarshalJSON(types.QueryCdpParams{
CollateralDenom: collateralType, CollateralDenom: args[1],
Owner: ownerAddress, Owner: ownerAddress,
}) })
if err != nil { if err != nil {
@ -73,13 +79,15 @@ func QueryCdpCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
// QueryCdpsByDenomCmd returns the command handler for querying cdps for a collateral type // QueryCdpsByDenomCmd returns the command handler for querying cdps for a collateral type
func QueryCdpsByDenomCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { func QueryCdpsByDenomCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{ return &cobra.Command{
Use: "cdps [collateralType]", Use: "cdps [collateral-name]",
Short: "Query cdps by collateral type", Short: "query CDPs by collateral",
Long: strings.TrimSpace(`Query cdps by a specific collateral type, or query all cdps if none is specifed: Long: strings.TrimSpace(
fmt.Sprintf(`List all CDPs collateralized with the specified asset.
$ <appcli> query cdp cdps uatom Example:
`), $ %s query %s cdps uatom
Args: cobra.MaximumNArgs(1), `, version.ClientName, types.ModuleName)),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc) cliCtx := context.NewCLIContext().WithCodec(cdc)
@ -105,32 +113,37 @@ $ <appcli> query cdp cdps uatom
} }
// QueryCdpsByDenomAndRatioCmd returns the command handler for querying cdps // QueryCdpsByDenomAndRatioCmd returns the command handler for querying cdps
// by specified collateral type and collateralization ratio // that are under the specified collateral ratio
func QueryCdpsByDenomAndRatioCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { func QueryCdpsByDenomAndRatioCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{ return &cobra.Command{
Use: "cdps [collateralType] [ratio]", Use: "cdps-by-ratio [collateral-name] [collateralization-ratio]",
Short: "get cdps with matching collateral type and below the specified ratio", Short: "get cdps under a collateralization ratio",
Long: strings.TrimSpace(`Get all CDPS of a particular collateral type with collateralization Long: strings.TrimSpace(
ratio below the specified input.`), fmt.Sprintf(`List all CDPs under a collateralization ratios.
Collateralization ratio is: collateral * price / debt.
Example:
$ %s query %s cdps-by-ratio uatom 1.5
`, version.ClientName, types.ModuleName)),
Args: cobra.ExactArgs(2), Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc) cliCtx := context.NewCLIContext().WithCodec(cdc)
// Prepare params for querier // Prepare params for querier
price, errSdk := sdk.NewDecFromStr(args[1]) ratio, errSdk := sdk.NewDecFromStr(args[1])
if errSdk != nil { if errSdk != nil {
return fmt.Errorf(errSdk.Error()) return fmt.Errorf(errSdk.Error())
} }
bz, err := cdc.MarshalJSON(types.QueryCdpsByRatioParams{ bz, err := cdc.MarshalJSON(types.QueryCdpsByRatioParams{
CollateralDenom: args[0], CollateralDenom: args[0],
Ratio: price, Ratio: ratio,
}) })
if err != nil { if err != nil {
return err return err
} }
// Query // Query
route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryGetCdps) route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryGetCdpsByCollateralization)
res, _, err := cliCtx.QueryWithData(route, bz) res, _, err := cliCtx.QueryWithData(route, bz)
if err != nil { if err != nil {
return err return err
@ -162,7 +175,7 @@ func QueryParamsCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
} }
// Decode and print results // Decode and print results
var out types.QueryCdpParams var out types.Params
cdc.MustUnmarshalJSON(res, &out) cdc.MustUnmarshalJSON(res, &out)
return cliCtx.PrintOutput(out) return cliCtx.PrintOutput(out)
}, },

View File

@ -1,12 +1,16 @@
package cli package cli
import ( import (
"fmt"
"strings"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/cosmos-sdk/x/auth" "github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/auth/client/utils" "github.com/cosmos/cosmos-sdk/x/auth/client/utils"
@ -34,17 +38,24 @@ func GetTxCmd(cdc *codec.Codec) *cobra.Command {
// GetCmdCreateCdp returns the command handler for creating a cdp // GetCmdCreateCdp returns the command handler for creating a cdp
func GetCmdCreateCdp(cdc *codec.Codec) *cobra.Command { func GetCmdCreateCdp(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{ return &cobra.Command{
Use: "create [ownerAddress] [collateralChange] [debtChange]", Use: "create [collateral] [debt]",
Short: "create a new cdp", Short: "create a new cdp",
Args: cobra.ExactArgs(3), Long: strings.TrimSpace(
fmt.Sprintf(`Create a new cdp, depositing some collateral and drawing some debt.
Example:
$ %s tx %s create 10000000uatom 1000usdx --from myKeyName
`, version.ClientName, types.ModuleName)),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc) cliCtx := context.NewCLIContext().WithCodec(cdc)
txBldr := auth.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc)) txBldr := auth.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc))
collateral, err := sdk.ParseCoins(args[1])
collateral, err := sdk.ParseCoins(args[0])
if err != nil { if err != nil {
return err return err
} }
debt, err := sdk.ParseCoins(args[2]) debt, err := sdk.ParseCoins(args[1])
if err != nil { if err != nil {
return err return err
} }
@ -61,13 +72,20 @@ func GetCmdCreateCdp(cdc *codec.Codec) *cobra.Command {
// GetCmdDeposit cli command for depositing to a cdp. // GetCmdDeposit cli command for depositing to a cdp.
func GetCmdDeposit(cdc *codec.Codec) *cobra.Command { func GetCmdDeposit(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{ return &cobra.Command{
Use: "deposit [ownerAddress] [depositorAddress] [collateralChange]", Use: "deposit [owner-addr] [collateral]",
Short: "deposit to an existing cdp", Short: "deposit collateral to an existing cdp",
Args: cobra.ExactArgs(3), Long: strings.TrimSpace(
fmt.Sprintf(`Add collateral to an existing cdp.
Example:
$ %s tx %s deposit kava15qdefkmwswysgg4qxgqpqr35k3m49pkx2jdfnw 10000000uatom --from myKeyName
`, version.ClientName, types.ModuleName)),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc) cliCtx := context.NewCLIContext().WithCodec(cdc)
txBldr := auth.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc)) txBldr := auth.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc))
collateral, err := sdk.ParseCoins(args[2])
collateral, err := sdk.ParseCoins(args[1])
if err != nil { if err != nil {
return err return err
} }
@ -75,11 +93,7 @@ func GetCmdDeposit(cdc *codec.Codec) *cobra.Command {
if err != nil { if err != nil {
return err return err
} }
depositor, err := sdk.AccAddressFromBech32(args[1]) msg := types.NewMsgDeposit(owner, cliCtx.GetFromAddress(), collateral)
if err != nil {
return err
}
msg := types.NewMsgDeposit(owner, depositor, collateral)
err = msg.ValidateBasic() err = msg.ValidateBasic()
if err != nil { if err != nil {
return err return err
@ -92,13 +106,20 @@ func GetCmdDeposit(cdc *codec.Codec) *cobra.Command {
// GetCmdWithdraw cli command for withdrawing from a cdp. // GetCmdWithdraw cli command for withdrawing from a cdp.
func GetCmdWithdraw(cdc *codec.Codec) *cobra.Command { func GetCmdWithdraw(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{ return &cobra.Command{
Use: "withdraw [ownerAddress] [depositorAddress] [collateralChange]", Use: "withdraw [owner-addr] [collateral]",
Short: "withdraw from an existing cdp", Short: "withdraw collateral from an existing cdp",
Args: cobra.ExactArgs(3), Long: strings.TrimSpace(
fmt.Sprintf(`Remove collateral from an existing cdp.
Example:
$ %s tx %s withdraw kava15qdefkmwswysgg4qxgqpqr35k3m49pkx2jdfnw 10000000uatom --from myKeyName
`, version.ClientName, types.ModuleName)),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc) cliCtx := context.NewCLIContext().WithCodec(cdc)
txBldr := auth.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc)) txBldr := auth.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc))
collateral, err := sdk.ParseCoins(args[2])
collateral, err := sdk.ParseCoins(args[1])
if err != nil { if err != nil {
return err return err
} }
@ -106,11 +127,7 @@ func GetCmdWithdraw(cdc *codec.Codec) *cobra.Command {
if err != nil { if err != nil {
return err return err
} }
depositor, err := sdk.AccAddressFromBech32(args[1]) msg := types.NewMsgWithdraw(owner, cliCtx.GetFromAddress(), collateral)
if err != nil {
return err
}
msg := types.NewMsgWithdraw(owner, depositor, collateral)
err = msg.ValidateBasic() err = msg.ValidateBasic()
if err != nil { if err != nil {
return err return err
@ -123,21 +140,24 @@ func GetCmdWithdraw(cdc *codec.Codec) *cobra.Command {
// GetCmdDraw cli command for depositing to a cdp. // GetCmdDraw cli command for depositing to a cdp.
func GetCmdDraw(cdc *codec.Codec) *cobra.Command { func GetCmdDraw(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{ return &cobra.Command{
Use: "draw [ownerAddress] [collateralDenom] [debtChange]", Use: "draw [collateral-name] [debt]",
Short: "draw debt off an existing cdp", Short: "draw debt off an existing cdp",
Args: cobra.ExactArgs(3), Long: strings.TrimSpace(
fmt.Sprintf(`Create debt in an existing cdp and send the newly minted asset to your account.
Example:
$ %s tx %s draw uatom 1000usdx --from myKeyName
`, version.ClientName, types.ModuleName)),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc) cliCtx := context.NewCLIContext().WithCodec(cdc)
txBldr := auth.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc)) txBldr := auth.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc))
debt, err := sdk.ParseCoins(args[2])
debt, err := sdk.ParseCoins(args[1])
if err != nil { if err != nil {
return err return err
} }
owner, err := sdk.AccAddressFromBech32(args[0]) msg := types.NewMsgDrawDebt(cliCtx.GetFromAddress(), args[0], debt)
if err != nil {
return err
}
msg := types.NewMsgDrawDebt(owner, args[1], debt)
err = msg.ValidateBasic() err = msg.ValidateBasic()
if err != nil { if err != nil {
return err return err
@ -150,21 +170,24 @@ func GetCmdDraw(cdc *codec.Codec) *cobra.Command {
// GetCmdRepay cli command for depositing to a cdp. // GetCmdRepay cli command for depositing to a cdp.
func GetCmdRepay(cdc *codec.Codec) *cobra.Command { func GetCmdRepay(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{ return &cobra.Command{
Use: "repay [ownerAddress] [collateralDenom] [payment]", Use: "repay [collateral-name] [debt]",
Short: "repay debt from an existing cdp", Short: "repay debt to an existing cdp",
Args: cobra.ExactArgs(3), Long: strings.TrimSpace(
fmt.Sprintf(`Cancel out debt in an existing cdp.
Example:
$ %s tx %s repay uatom 1000usdx --from myKeyName
`, version.ClientName, types.ModuleName)),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc) cliCtx := context.NewCLIContext().WithCodec(cdc)
txBldr := auth.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc)) txBldr := auth.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc))
payment, err := sdk.ParseCoins(args[2])
payment, err := sdk.ParseCoins(args[1])
if err != nil { if err != nil {
return err return err
} }
owner, err := sdk.AccAddressFromBech32(args[0]) msg := types.NewMsgRepayDebt(cliCtx.GetFromAddress(), args[0], payment)
if err != nil {
return err
}
msg := types.NewMsgRepayDebt(owner, args[1], payment)
err = msg.ValidateBasic() err = msg.ValidateBasic()
if err != nil { if err != nil {
return err return err

View File

@ -1,8 +1,6 @@
package keeper package keeper
import ( import (
"fmt"
abci "github.com/tendermint/tendermint/abci/types" abci "github.com/tendermint/tendermint/abci/types"
"github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/codec"
@ -33,7 +31,7 @@ func queryGetCdp(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte,
var requestParams types.QueryCdpParams var requestParams types.QueryCdpParams
err := keeper.cdc.UnmarshalJSON(req.Data, &requestParams) err := keeper.cdc.UnmarshalJSON(req.Data, &requestParams)
if err != nil { if err != nil {
return nil, sdk.ErrInternal(fmt.Sprintf("failed to parse params: %s", err)) return nil, sdk.ErrUnknownRequest(sdk.AppendMsgToErr("incorrectly formatted request data", err.Error()))
} }
_, valid := keeper.GetDenomPrefix(ctx, requestParams.CollateralDenom) _, valid := keeper.GetDenomPrefix(ctx, requestParams.CollateralDenom)
@ -59,7 +57,7 @@ func queryGetCdpsByRatio(ctx sdk.Context, req abci.RequestQuery, keeper Keeper)
var requestParams types.QueryCdpsByRatioParams var requestParams types.QueryCdpsByRatioParams
err := keeper.cdc.UnmarshalJSON(req.Data, &requestParams) err := keeper.cdc.UnmarshalJSON(req.Data, &requestParams)
if err != nil { if err != nil {
return nil, sdk.ErrInternal(fmt.Sprintf("failed to parse params: %s", err)) return nil, sdk.ErrUnknownRequest(sdk.AppendMsgToErr("incorrectly formatted request data", err.Error()))
} }
_, valid := keeper.GetDenomPrefix(ctx, requestParams.CollateralDenom) _, valid := keeper.GetDenomPrefix(ctx, requestParams.CollateralDenom)
if !valid { if !valid {
@ -79,7 +77,7 @@ func queryGetCdpsByDenom(ctx sdk.Context, req abci.RequestQuery, keeper Keeper)
var requestParams types.QueryCdpsParams var requestParams types.QueryCdpsParams
err := keeper.cdc.UnmarshalJSON(req.Data, &requestParams) err := keeper.cdc.UnmarshalJSON(req.Data, &requestParams)
if err != nil { if err != nil {
return nil, sdk.ErrInternal(fmt.Sprintf("failed to parse params: %s", err)) return nil, sdk.ErrUnknownRequest(sdk.AppendMsgToErr("incorrectly formatted request data", err.Error()))
} }
_, valid := keeper.GetDenomPrefix(ctx, requestParams.CollateralDenom) _, valid := keeper.GetDenomPrefix(ctx, requestParams.CollateralDenom)
if !valid { if !valid {

View File

@ -126,7 +126,7 @@ func (dp DebtParam) String() string {
Reference Asset: %s Reference Asset: %s
Debt Limit: %s Debt Limit: %s
Conversion Factor: %s Conversion Factor: %s
Debt Floot %s`, dp.Denom, dp.ReferenceAsset, dp.DebtLimit, dp.ConversionFactor, dp.DebtFloor) Debt Floor %s`, dp.Denom, dp.ReferenceAsset, dp.DebtLimit, dp.ConversionFactor, dp.DebtFloor)
} }
// DebtParams array of DebtParam // DebtParams array of DebtParam

View File

@ -27,6 +27,12 @@ func NewQueryCdpsParams(denom string) QueryCdpsParams {
} }
} }
// QueryCdpParams params for query /cdp/cdp
type QueryCdpParams struct {
CollateralDenom string // get CDPs with this collateral denom
Owner sdk.AccAddress // get CDPs belonging to this owner
}
// NewQueryCdpParams returns QueryCdpParams // NewQueryCdpParams returns QueryCdpParams
func NewQueryCdpParams(owner sdk.AccAddress, denom string) QueryCdpParams { func NewQueryCdpParams(owner sdk.AccAddress, denom string) QueryCdpParams {
return QueryCdpParams{ return QueryCdpParams{
@ -35,12 +41,6 @@ func NewQueryCdpParams(owner sdk.AccAddress, denom string) QueryCdpParams {
} }
} }
// QueryCdpParams params for query /cdp/cdp
type QueryCdpParams struct {
CollateralDenom string // get CDPs with this collateral denom
Owner sdk.AccAddress // get CDPs belonging to this owner
}
// QueryCdpsByRatioParams params for query /cdp/cdps/ratio // QueryCdpsByRatioParams params for query /cdp/cdps/ratio
type QueryCdpsByRatioParams struct { type QueryCdpsByRatioParams struct {
CollateralDenom string // get CDPs with this collateral denom CollateralDenom string // get CDPs with this collateral denom