From 2d9820b3d1073a91d696eede0c7f8d17eaebd2a8 Mon Sep 17 00:00:00 2001 From: Ruaridh Date: Thu, 16 Jan 2020 16:20:12 +0100 Subject: [PATCH] cdp cli clean up (#305) * clean up querier cmds #304 #298 #296 * #299 address cli ux issues * fix typo * edit help message --- x/cdp/client/cli/query.go | 51 ++++++++++++------- x/cdp/client/cli/tx.go | 101 +++++++++++++++++++++++--------------- x/cdp/keeper/querier.go | 8 ++- x/cdp/types/params.go | 2 +- x/cdp/types/querier.go | 12 ++--- 5 files changed, 104 insertions(+), 70 deletions(-) diff --git a/x/cdp/client/cli/query.go b/x/cdp/client/cli/query.go index 45b768ec..01247bd7 100644 --- a/x/cdp/client/cli/query.go +++ b/x/cdp/client/cli/query.go @@ -10,6 +10,7 @@ import ( "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/version" "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 func QueryCdpCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { return &cobra.Command{ - Use: "cdp [ownerAddress] [collateralType]", + Use: "cdp [owner-addr] [collateral-name]", 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 { cliCtx := context.NewCLIContext().WithCodec(cdc) @@ -46,9 +53,8 @@ func QueryCdpCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { if err != nil { return err } - collateralType := args[1] // TODO validation? bz, err := cdc.MarshalJSON(types.QueryCdpParams{ - CollateralDenom: collateralType, + CollateralDenom: args[1], Owner: ownerAddress, }) 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 func QueryCdpsByDenomCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { return &cobra.Command{ - Use: "cdps [collateralType]", - Short: "Query cdps by collateral type", - Long: strings.TrimSpace(`Query cdps by a specific collateral type, or query all cdps if none is specifed: + Use: "cdps [collateral-name]", + Short: "query CDPs by collateral", + Long: strings.TrimSpace( + fmt.Sprintf(`List all CDPs collateralized with the specified asset. -$ query cdp cdps uatom - `), - Args: cobra.MaximumNArgs(1), +Example: +$ %s query %s cdps uatom +`, version.ClientName, types.ModuleName)), + Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { cliCtx := context.NewCLIContext().WithCodec(cdc) @@ -105,32 +113,37 @@ $ query cdp cdps uatom } // 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 { return &cobra.Command{ - Use: "cdps [collateralType] [ratio]", - Short: "get cdps with matching collateral type and below the specified ratio", - Long: strings.TrimSpace(`Get all CDPS of a particular collateral type with collateralization - ratio below the specified input.`), + Use: "cdps-by-ratio [collateral-name] [collateralization-ratio]", + Short: "get cdps under a collateralization ratio", + Long: strings.TrimSpace( + 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), RunE: func(cmd *cobra.Command, args []string) error { cliCtx := context.NewCLIContext().WithCodec(cdc) // Prepare params for querier - price, errSdk := sdk.NewDecFromStr(args[1]) + ratio, errSdk := sdk.NewDecFromStr(args[1]) if errSdk != nil { return fmt.Errorf(errSdk.Error()) } bz, err := cdc.MarshalJSON(types.QueryCdpsByRatioParams{ CollateralDenom: args[0], - Ratio: price, + Ratio: ratio, }) if err != nil { return err } // 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) if err != nil { return err @@ -162,7 +175,7 @@ func QueryParamsCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { } // Decode and print results - var out types.QueryCdpParams + var out types.Params cdc.MustUnmarshalJSON(res, &out) return cliCtx.PrintOutput(out) }, diff --git a/x/cdp/client/cli/tx.go b/x/cdp/client/cli/tx.go index 83d9ca94..9214aa1b 100644 --- a/x/cdp/client/cli/tx.go +++ b/x/cdp/client/cli/tx.go @@ -1,12 +1,16 @@ package cli import ( + "fmt" + "strings" + "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" + "github.com/cosmos/cosmos-sdk/version" "github.com/cosmos/cosmos-sdk/x/auth" "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 func GetCmdCreateCdp(cdc *codec.Codec) *cobra.Command { return &cobra.Command{ - Use: "create [ownerAddress] [collateralChange] [debtChange]", + Use: "create [collateral] [debt]", 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 { cliCtx := context.NewCLIContext().WithCodec(cdc) txBldr := auth.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc)) - collateral, err := sdk.ParseCoins(args[1]) + + collateral, err := sdk.ParseCoins(args[0]) if err != nil { return err } - debt, err := sdk.ParseCoins(args[2]) + debt, err := sdk.ParseCoins(args[1]) if err != nil { return err } @@ -61,13 +72,20 @@ func GetCmdCreateCdp(cdc *codec.Codec) *cobra.Command { // GetCmdDeposit cli command for depositing to a cdp. func GetCmdDeposit(cdc *codec.Codec) *cobra.Command { return &cobra.Command{ - Use: "deposit [ownerAddress] [depositorAddress] [collateralChange]", - Short: "deposit to an existing cdp", - Args: cobra.ExactArgs(3), + Use: "deposit [owner-addr] [collateral]", + Short: "deposit collateral to an existing cdp", + 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 { cliCtx := context.NewCLIContext().WithCodec(cdc) txBldr := auth.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc)) - collateral, err := sdk.ParseCoins(args[2]) + + collateral, err := sdk.ParseCoins(args[1]) if err != nil { return err } @@ -75,11 +93,7 @@ func GetCmdDeposit(cdc *codec.Codec) *cobra.Command { if err != nil { return err } - depositor, err := sdk.AccAddressFromBech32(args[1]) - if err != nil { - return err - } - msg := types.NewMsgDeposit(owner, depositor, collateral) + msg := types.NewMsgDeposit(owner, cliCtx.GetFromAddress(), collateral) err = msg.ValidateBasic() if err != nil { return err @@ -92,13 +106,20 @@ func GetCmdDeposit(cdc *codec.Codec) *cobra.Command { // GetCmdWithdraw cli command for withdrawing from a cdp. func GetCmdWithdraw(cdc *codec.Codec) *cobra.Command { return &cobra.Command{ - Use: "withdraw [ownerAddress] [depositorAddress] [collateralChange]", - Short: "withdraw from an existing cdp", - Args: cobra.ExactArgs(3), + Use: "withdraw [owner-addr] [collateral]", + Short: "withdraw collateral from an existing cdp", + 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 { cliCtx := context.NewCLIContext().WithCodec(cdc) txBldr := auth.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc)) - collateral, err := sdk.ParseCoins(args[2]) + + collateral, err := sdk.ParseCoins(args[1]) if err != nil { return err } @@ -106,11 +127,7 @@ func GetCmdWithdraw(cdc *codec.Codec) *cobra.Command { if err != nil { return err } - depositor, err := sdk.AccAddressFromBech32(args[1]) - if err != nil { - return err - } - msg := types.NewMsgWithdraw(owner, depositor, collateral) + msg := types.NewMsgWithdraw(owner, cliCtx.GetFromAddress(), collateral) err = msg.ValidateBasic() if err != nil { return err @@ -123,21 +140,24 @@ func GetCmdWithdraw(cdc *codec.Codec) *cobra.Command { // GetCmdDraw cli command for depositing to a cdp. func GetCmdDraw(cdc *codec.Codec) *cobra.Command { return &cobra.Command{ - Use: "draw [ownerAddress] [collateralDenom] [debtChange]", + Use: "draw [collateral-name] [debt]", 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 { cliCtx := context.NewCLIContext().WithCodec(cdc) txBldr := auth.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc)) - debt, err := sdk.ParseCoins(args[2]) + + debt, err := sdk.ParseCoins(args[1]) if err != nil { return err } - owner, err := sdk.AccAddressFromBech32(args[0]) - if err != nil { - return err - } - msg := types.NewMsgDrawDebt(owner, args[1], debt) + msg := types.NewMsgDrawDebt(cliCtx.GetFromAddress(), args[0], debt) err = msg.ValidateBasic() if err != nil { return err @@ -150,21 +170,24 @@ func GetCmdDraw(cdc *codec.Codec) *cobra.Command { // GetCmdRepay cli command for depositing to a cdp. func GetCmdRepay(cdc *codec.Codec) *cobra.Command { return &cobra.Command{ - Use: "repay [ownerAddress] [collateralDenom] [payment]", - Short: "repay debt from an existing cdp", - Args: cobra.ExactArgs(3), + Use: "repay [collateral-name] [debt]", + Short: "repay debt to an existing cdp", + 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 { cliCtx := context.NewCLIContext().WithCodec(cdc) txBldr := auth.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc)) - payment, err := sdk.ParseCoins(args[2]) + + payment, err := sdk.ParseCoins(args[1]) if err != nil { return err } - owner, err := sdk.AccAddressFromBech32(args[0]) - if err != nil { - return err - } - msg := types.NewMsgRepayDebt(owner, args[1], payment) + msg := types.NewMsgRepayDebt(cliCtx.GetFromAddress(), args[0], payment) err = msg.ValidateBasic() if err != nil { return err diff --git a/x/cdp/keeper/querier.go b/x/cdp/keeper/querier.go index ec83986a..dada19ca 100644 --- a/x/cdp/keeper/querier.go +++ b/x/cdp/keeper/querier.go @@ -1,8 +1,6 @@ package keeper import ( - "fmt" - abci "github.com/tendermint/tendermint/abci/types" "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 err := keeper.cdc.UnmarshalJSON(req.Data, &requestParams) 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) @@ -59,7 +57,7 @@ func queryGetCdpsByRatio(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) var requestParams types.QueryCdpsByRatioParams err := keeper.cdc.UnmarshalJSON(req.Data, &requestParams) 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) if !valid { @@ -79,7 +77,7 @@ func queryGetCdpsByDenom(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) var requestParams types.QueryCdpsParams err := keeper.cdc.UnmarshalJSON(req.Data, &requestParams) 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) if !valid { diff --git a/x/cdp/types/params.go b/x/cdp/types/params.go index 58b9016a..ec0f94f9 100644 --- a/x/cdp/types/params.go +++ b/x/cdp/types/params.go @@ -126,7 +126,7 @@ func (dp DebtParam) String() string { Reference Asset: %s Debt Limit: %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 diff --git a/x/cdp/types/querier.go b/x/cdp/types/querier.go index 368621e2..fa3c8ed9 100644 --- a/x/cdp/types/querier.go +++ b/x/cdp/types/querier.go @@ -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 func NewQueryCdpParams(owner sdk.AccAddress, denom string) 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 type QueryCdpsByRatioParams struct { CollateralDenom string // get CDPs with this collateral denom