2020-09-21 21:08:43 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2021-02-08 12:51:42 +00:00
|
|
|
"github.com/spf13/viper"
|
2020-09-21 21:08:43 +00:00
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
|
|
|
"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"
|
|
|
|
|
2020-12-21 17:18:55 +00:00
|
|
|
"github.com/kava-labs/kava/x/hard/types"
|
2020-09-21 21:08:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetTxCmd returns the transaction commands for this module
|
|
|
|
func GetTxCmd(cdc *codec.Codec) *cobra.Command {
|
2020-12-21 17:18:55 +00:00
|
|
|
hardTxCmd := &cobra.Command{
|
2020-09-21 21:08:43 +00:00
|
|
|
Use: types.ModuleName,
|
|
|
|
Short: fmt.Sprintf("%s transactions subcommands", types.ModuleName),
|
|
|
|
DisableFlagParsing: true,
|
|
|
|
SuggestionsMinimumDistance: 2,
|
|
|
|
RunE: client.ValidateCmd,
|
|
|
|
}
|
|
|
|
|
2020-12-21 17:18:55 +00:00
|
|
|
hardTxCmd.AddCommand(flags.PostCommands(
|
2020-09-21 21:08:43 +00:00
|
|
|
getCmdDeposit(cdc),
|
|
|
|
getCmdWithdraw(cdc),
|
2020-10-30 09:59:47 +00:00
|
|
|
getCmdBorrow(cdc),
|
2021-02-08 12:51:42 +00:00
|
|
|
addOptionalFlag(getCmdRepay(cdc), flagOwner, "", "original borrower's address whose loan will be repaid"),
|
2020-12-15 17:38:14 +00:00
|
|
|
getCmdLiquidate(cdc),
|
2020-09-21 21:08:43 +00:00
|
|
|
)...)
|
|
|
|
|
2020-12-21 17:18:55 +00:00
|
|
|
return hardTxCmd
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 12:51:42 +00:00
|
|
|
// addFlag adds a cobra flag and binds it using viper
|
|
|
|
func addOptionalFlag(cmd *cobra.Command, flagName, flagValue, flagUsage string) *cobra.Command {
|
|
|
|
cmd.Flags().String(flagName, flagValue, flagUsage)
|
|
|
|
viper.BindPFlag(flagName, cmd.Flags().Lookup(flagName))
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2020-09-21 21:08:43 +00:00
|
|
|
func getCmdDeposit(cdc *codec.Codec) *cobra.Command {
|
|
|
|
return &cobra.Command{
|
2020-11-12 16:43:28 +00:00
|
|
|
Use: "deposit [amount]",
|
2020-12-21 17:18:55 +00:00
|
|
|
Short: "deposit coins to hard",
|
2020-09-21 21:08:43 +00:00
|
|
|
Example: fmt.Sprintf(
|
2020-11-12 16:43:28 +00:00
|
|
|
`%s tx %s deposit 10000000bnb --from <key>`, version.ClientName, types.ModuleName,
|
2020-09-21 21:08:43 +00:00
|
|
|
),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
inBuf := bufio.NewReader(cmd.InOrStdin())
|
|
|
|
cliCtx := context.NewCLIContext().WithCodec(cdc)
|
|
|
|
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))
|
2020-12-18 16:05:21 +00:00
|
|
|
amount, err := sdk.ParseCoins(args[0])
|
2020-09-21 21:08:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-12 16:43:28 +00:00
|
|
|
msg := types.NewMsgDeposit(cliCtx.GetFromAddress(), amount)
|
2020-09-21 21:08:43 +00:00
|
|
|
if err := msg.ValidateBasic(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getCmdWithdraw(cdc *codec.Codec) *cobra.Command {
|
|
|
|
return &cobra.Command{
|
2020-11-12 16:43:28 +00:00
|
|
|
Use: "withdraw [amount]",
|
2020-12-21 17:18:55 +00:00
|
|
|
Short: "withdraw coins from hard",
|
2020-11-12 16:43:28 +00:00
|
|
|
Args: cobra.ExactArgs(1),
|
2020-09-21 21:08:43 +00:00
|
|
|
Example: fmt.Sprintf(
|
2020-11-12 16:43:28 +00:00
|
|
|
`%s tx %s withdraw 10000000bnb --from <key>`, version.ClientName, types.ModuleName,
|
2020-09-21 21:08:43 +00:00
|
|
|
),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
inBuf := bufio.NewReader(cmd.InOrStdin())
|
|
|
|
cliCtx := context.NewCLIContext().WithCodec(cdc)
|
|
|
|
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))
|
2020-12-18 16:05:21 +00:00
|
|
|
amount, err := sdk.ParseCoins(args[0])
|
2020-09-21 21:08:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-12 16:43:28 +00:00
|
|
|
msg := types.NewMsgWithdraw(cliCtx.GetFromAddress(), amount)
|
2020-09-21 21:08:43 +00:00
|
|
|
if err := msg.ValidateBasic(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-30 09:59:47 +00:00
|
|
|
func getCmdBorrow(cdc *codec.Codec) *cobra.Command {
|
|
|
|
return &cobra.Command{
|
2021-02-08 12:51:42 +00:00
|
|
|
Use: "borrow [amount]",
|
2020-12-21 17:18:55 +00:00
|
|
|
Short: "borrow tokens from the hard protocol",
|
|
|
|
Long: strings.TrimSpace(`borrows tokens from the hard protocol`),
|
2020-10-30 09:59:47 +00:00
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
Example: fmt.Sprintf(
|
|
|
|
`%s tx %s borrow 1000000000ukava --from <key>`, version.ClientName, types.ModuleName,
|
|
|
|
),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
inBuf := bufio.NewReader(cmd.InOrStdin())
|
|
|
|
cliCtx := context.NewCLIContext().WithCodec(cdc)
|
|
|
|
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))
|
|
|
|
|
|
|
|
coins, err := sdk.ParseCoins(args[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := types.NewMsgBorrow(cliCtx.GetFromAddress(), coins)
|
|
|
|
if err := msg.ValidateBasic(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2020-12-07 21:51:35 +00:00
|
|
|
|
|
|
|
func getCmdRepay(cdc *codec.Codec) *cobra.Command {
|
|
|
|
return &cobra.Command{
|
2021-02-08 12:51:42 +00:00
|
|
|
Use: "repay [amount]",
|
2020-12-21 17:18:55 +00:00
|
|
|
Short: "repay tokens to the hard protocol",
|
2021-02-08 12:51:42 +00:00
|
|
|
Long: strings.TrimSpace(`repay tokens to the hard protocol with optional --owner param to repay another account's loan`),
|
2020-12-07 21:51:35 +00:00
|
|
|
Args: cobra.ExactArgs(1),
|
2021-02-08 12:51:42 +00:00
|
|
|
Example: strings.TrimSpace(`
|
|
|
|
kvcli tx hard repay 1000000000ukava --from <key>
|
|
|
|
kvcli tx hard repay 1000000000ukava,25000000000bnb --from <key>
|
|
|
|
kvcli tx hard repay 1000000000ukava,25000000000bnb --owner <owner-address> --from <key>
|
|
|
|
`),
|
2020-12-07 21:51:35 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
inBuf := bufio.NewReader(cmd.InOrStdin())
|
|
|
|
cliCtx := context.NewCLIContext().WithCodec(cdc)
|
|
|
|
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))
|
|
|
|
|
2021-02-08 12:51:42 +00:00
|
|
|
var owner sdk.AccAddress
|
|
|
|
ownerStr := viper.GetString(flagOwner)
|
|
|
|
|
|
|
|
// Parse optional owner argument or default to sender
|
|
|
|
if len(ownerStr) > 0 {
|
|
|
|
ownerAddr, err := sdk.AccAddressFromBech32(ownerStr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
owner = ownerAddr
|
|
|
|
} else {
|
|
|
|
owner = cliCtx.GetFromAddress()
|
|
|
|
}
|
|
|
|
|
2020-12-07 21:51:35 +00:00
|
|
|
coins, err := sdk.ParseCoins(args[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-08 12:51:42 +00:00
|
|
|
msg := types.NewMsgRepay(cliCtx.GetFromAddress(), owner, coins)
|
2020-12-07 21:51:35 +00:00
|
|
|
if err := msg.ValidateBasic(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2020-12-15 17:38:14 +00:00
|
|
|
|
|
|
|
func getCmdLiquidate(cdc *codec.Codec) *cobra.Command {
|
|
|
|
return &cobra.Command{
|
|
|
|
Use: "liquidate [borrower-addr]",
|
|
|
|
Short: "liquidate a borrower that's over their loan-to-value ratio",
|
|
|
|
Long: strings.TrimSpace(`liquidate a borrower that's over their loan-to-value ratio`),
|
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
Example: fmt.Sprintf(
|
2021-01-11 20:50:00 +00:00
|
|
|
`%s tx %s liquidate kava1hgcfsuwc889wtdmt8pjy7qffua9dd2tralu64j --from <key>`, version.ClientName, types.ModuleName,
|
2020-12-15 17:38:14 +00:00
|
|
|
),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
inBuf := bufio.NewReader(cmd.InOrStdin())
|
|
|
|
cliCtx := context.NewCLIContext().WithCodec(cdc)
|
|
|
|
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))
|
|
|
|
|
|
|
|
borrower, err := sdk.AccAddressFromBech32(args[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := types.NewMsgLiquidate(cliCtx.GetFromAddress(), borrower)
|
|
|
|
if err := msg.ValidateBasic(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|