2019-11-25 19:46:02 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2020-04-23 16:35:58 +00:00
|
|
|
"bufio"
|
2020-01-16 15:20:12 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2019-11-25 19:46:02 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
"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"
|
2019-11-25 19:46:02 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/client/utils"
|
|
|
|
|
|
|
|
"github.com/kava-labs/kava/x/cdp/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetTxCmd returns the transaction commands for this module
|
|
|
|
func GetTxCmd(cdc *codec.Codec) *cobra.Command {
|
|
|
|
cdpTxCmd := &cobra.Command{
|
|
|
|
Use: "cdp",
|
|
|
|
Short: "cdp transactions subcommands",
|
|
|
|
}
|
|
|
|
|
2020-04-23 16:35:58 +00:00
|
|
|
cdpTxCmd.AddCommand(flags.PostCommands(
|
2020-01-12 15:35:34 +00:00
|
|
|
GetCmdCreateCdp(cdc),
|
|
|
|
GetCmdDeposit(cdc),
|
|
|
|
GetCmdWithdraw(cdc),
|
|
|
|
GetCmdDraw(cdc),
|
|
|
|
GetCmdRepay(cdc),
|
2019-11-25 19:46:02 +00:00
|
|
|
)...)
|
|
|
|
|
|
|
|
return cdpTxCmd
|
|
|
|
}
|
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
// GetCmdCreateCdp returns the command handler for creating a cdp
|
|
|
|
func GetCmdCreateCdp(cdc *codec.Codec) *cobra.Command {
|
|
|
|
return &cobra.Command{
|
2020-01-16 15:20:12 +00:00
|
|
|
Use: "create [collateral] [debt]",
|
2020-01-12 15:35:34 +00:00
|
|
|
Short: "create a new cdp",
|
2020-01-16 15:20:12 +00:00
|
|
|
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),
|
2020-01-12 15:35:34 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2020-04-23 16:35:58 +00:00
|
|
|
inBuf := bufio.NewReader(cmd.InOrStdin())
|
2020-01-12 15:35:34 +00:00
|
|
|
cliCtx := context.NewCLIContext().WithCodec(cdc)
|
2020-04-23 16:35:58 +00:00
|
|
|
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))
|
2020-01-16 15:20:12 +00:00
|
|
|
|
|
|
|
collateral, err := sdk.ParseCoins(args[0])
|
2020-01-12 15:35:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-16 15:20:12 +00:00
|
|
|
debt, err := sdk.ParseCoins(args[1])
|
2020-01-12 15:35:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
msg := types.NewMsgCreateCDP(cliCtx.GetFromAddress(), collateral, debt)
|
|
|
|
err = msg.ValidateBasic()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCmdDeposit cli command for depositing to a cdp.
|
|
|
|
func GetCmdDeposit(cdc *codec.Codec) *cobra.Command {
|
|
|
|
return &cobra.Command{
|
2020-01-16 15:20:12 +00:00
|
|
|
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),
|
2020-01-12 15:35:34 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2020-04-23 16:35:58 +00:00
|
|
|
inBuf := bufio.NewReader(cmd.InOrStdin())
|
2020-01-12 15:35:34 +00:00
|
|
|
cliCtx := context.NewCLIContext().WithCodec(cdc)
|
2020-04-23 16:35:58 +00:00
|
|
|
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))
|
2020-01-16 15:20:12 +00:00
|
|
|
|
|
|
|
collateral, err := sdk.ParseCoins(args[1])
|
2020-01-12 15:35:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
owner, err := sdk.AccAddressFromBech32(args[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-16 15:20:12 +00:00
|
|
|
msg := types.NewMsgDeposit(owner, cliCtx.GetFromAddress(), collateral)
|
2020-01-12 15:35:34 +00:00
|
|
|
err = msg.ValidateBasic()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCmdWithdraw cli command for withdrawing from a cdp.
|
|
|
|
func GetCmdWithdraw(cdc *codec.Codec) *cobra.Command {
|
|
|
|
return &cobra.Command{
|
2020-01-16 15:20:12 +00:00
|
|
|
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),
|
2020-01-12 15:35:34 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2020-04-23 16:35:58 +00:00
|
|
|
inBuf := bufio.NewReader(cmd.InOrStdin())
|
2020-01-12 15:35:34 +00:00
|
|
|
cliCtx := context.NewCLIContext().WithCodec(cdc)
|
2020-04-23 16:35:58 +00:00
|
|
|
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))
|
2020-01-16 15:20:12 +00:00
|
|
|
|
|
|
|
collateral, err := sdk.ParseCoins(args[1])
|
2020-01-12 15:35:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
owner, err := sdk.AccAddressFromBech32(args[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-16 15:20:12 +00:00
|
|
|
msg := types.NewMsgWithdraw(owner, cliCtx.GetFromAddress(), collateral)
|
2020-01-12 15:35:34 +00:00
|
|
|
err = msg.ValidateBasic()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCmdDraw cli command for depositing to a cdp.
|
|
|
|
func GetCmdDraw(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: "draw [collateral-name] [debt]",
|
2020-01-12 15:35:34 +00:00
|
|
|
Short: "draw debt off an existing cdp",
|
2020-01-16 15:20:12 +00:00
|
|
|
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),
|
2019-11-25 19:46:02 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2020-04-23 16:35:58 +00:00
|
|
|
inBuf := bufio.NewReader(cmd.InOrStdin())
|
2019-11-25 19:46:02 +00:00
|
|
|
cliCtx := context.NewCLIContext().WithCodec(cdc)
|
2020-04-23 16:35:58 +00:00
|
|
|
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))
|
2020-01-16 15:20:12 +00:00
|
|
|
|
|
|
|
debt, err := sdk.ParseCoins(args[1])
|
2020-01-12 15:35:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-16 15:20:12 +00:00
|
|
|
msg := types.NewMsgDrawDebt(cliCtx.GetFromAddress(), args[0], debt)
|
2020-01-12 15:35:34 +00:00
|
|
|
err = msg.ValidateBasic()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCmdRepay cli command for depositing to a cdp.
|
|
|
|
func GetCmdRepay(cdc *codec.Codec) *cobra.Command {
|
|
|
|
return &cobra.Command{
|
2020-01-16 15:20:12 +00:00
|
|
|
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),
|
2020-01-12 15:35:34 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2020-04-23 16:35:58 +00:00
|
|
|
inBuf := bufio.NewReader(cmd.InOrStdin())
|
2020-01-12 15:35:34 +00:00
|
|
|
cliCtx := context.NewCLIContext().WithCodec(cdc)
|
2020-04-23 16:35:58 +00:00
|
|
|
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))
|
2020-01-16 15:20:12 +00:00
|
|
|
|
|
|
|
payment, err := sdk.ParseCoins(args[1])
|
2020-01-12 15:35:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-16 15:20:12 +00:00
|
|
|
msg := types.NewMsgRepayDebt(cliCtx.GetFromAddress(), args[0], payment)
|
2020-01-12 15:35:34 +00:00
|
|
|
err = msg.ValidateBasic()
|
2019-11-25 19:46:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|