2020-09-21 21:08:43 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
2022-01-08 00:39:27 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/tx"
|
2020-09-21 21:08:43 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/version"
|
|
|
|
|
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
|
2022-01-08 00:39:27 +00:00
|
|
|
func GetTxCmd() *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,
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
cmds := []*cobra.Command{
|
|
|
|
getCmdDeposit(),
|
|
|
|
getCmdWithdraw(),
|
|
|
|
getCmdBorrow(),
|
|
|
|
getCmdRepay(),
|
|
|
|
getCmdLiquidate(),
|
|
|
|
}
|
2020-09-21 21:08:43 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
for _, cmd := range cmds {
|
|
|
|
flags.AddTxFlagsToCmd(cmd)
|
|
|
|
}
|
2020-09-21 21:08:43 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
hardTxCmd.AddCommand(cmds...)
|
|
|
|
|
|
|
|
return hardTxCmd
|
2021-02-08 12:51:42 +00:00
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func getCmdDeposit() *cobra.Command {
|
2020-09-21 21:08:43 +00:00
|
|
|
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(
|
2022-01-08 00:39:27 +00:00
|
|
|
`%s tx %s deposit 10000000bnb --from <key>`, version.AppName, types.ModuleName,
|
2020-09-21 21:08:43 +00:00
|
|
|
),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2022-01-08 00:39:27 +00:00
|
|
|
clientCtx, err := client.GetClientTxContext(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
amount, err := sdk.ParseCoinsNormalized(args[0])
|
2020-09-21 21:08:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
msg := types.NewMsgDeposit(clientCtx.GetFromAddress(), amount)
|
2020-09-21 21:08:43 +00:00
|
|
|
if err := msg.ValidateBasic(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg)
|
2020-09-21 21:08:43 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func getCmdWithdraw() *cobra.Command {
|
2020-09-21 21:08:43 +00:00
|
|
|
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(
|
2022-01-08 00:39:27 +00:00
|
|
|
`%s tx %s withdraw 10000000bnb --from <key>`, version.AppName, types.ModuleName,
|
2020-09-21 21:08:43 +00:00
|
|
|
),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2022-01-08 00:39:27 +00:00
|
|
|
clientCtx, err := client.GetClientTxContext(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
amount, err := sdk.ParseCoinsNormalized(args[0])
|
2020-09-21 21:08:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
msg := types.NewMsgWithdraw(clientCtx.GetFromAddress(), amount)
|
2020-09-21 21:08:43 +00:00
|
|
|
if err := msg.ValidateBasic(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg)
|
2020-09-21 21:08:43 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func getCmdBorrow() *cobra.Command {
|
2020-10-30 09:59:47 +00:00
|
|
|
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(
|
2022-01-08 00:39:27 +00:00
|
|
|
`%s tx %s borrow 1000000000ukava --from <key>`, version.AppName, types.ModuleName,
|
2020-10-30 09:59:47 +00:00
|
|
|
),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2022-01-08 00:39:27 +00:00
|
|
|
clientCtx, err := client.GetClientTxContext(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-30 09:59:47 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
coins, err := sdk.ParseCoinsNormalized(args[0])
|
2020-10-30 09:59:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
msg := types.NewMsgBorrow(clientCtx.GetFromAddress(), coins)
|
2020-10-30 09:59:47 +00:00
|
|
|
if err := msg.ValidateBasic(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg)
|
2020-10-30 09:59:47 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2020-12-07 21:51:35 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func getCmdRepay() *cobra.Command {
|
|
|
|
cmd := &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),
|
2022-01-08 00:39:27 +00:00
|
|
|
Example: fmt.Sprintf(`
|
|
|
|
%[1]s tx %[2]s repay 1000000000ukava --from <key>
|
|
|
|
%[1]s tx %[2]s repay 1000000000ukava,25000000000bnb --from <key>
|
|
|
|
%[1]s tx %[2]s repay 1000000000ukava,25000000000bnb --owner <owner-address> --from <key>`, version.AppName, types.ModuleName),
|
2020-12-07 21:51:35 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2022-01-08 00:39:27 +00:00
|
|
|
clientCtx, err := client.GetClientTxContext(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-07 21:51:35 +00:00
|
|
|
|
2021-02-08 12:51:42 +00:00
|
|
|
var owner sdk.AccAddress
|
2022-01-08 00:39:27 +00:00
|
|
|
ownerStr, err := cmd.Flags().GetString(flagOwner)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-08 12:51:42 +00:00
|
|
|
|
|
|
|
// 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 {
|
2022-01-08 00:39:27 +00:00
|
|
|
owner = clientCtx.GetFromAddress()
|
2021-02-08 12:51:42 +00:00
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
coins, err := sdk.ParseCoinsNormalized(args[0])
|
2020-12-07 21:51:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
msg := types.NewMsgRepay(clientCtx.GetFromAddress(), owner, coins)
|
2020-12-07 21:51:35 +00:00
|
|
|
if err := msg.ValidateBasic(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg)
|
2020-12-07 21:51:35 +00:00
|
|
|
},
|
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
|
|
|
|
cmd.Flags().String(flagOwner, "", "original borrower's address whose loan will be repaid")
|
|
|
|
|
|
|
|
return cmd
|
2020-12-07 21:51:35 +00:00
|
|
|
}
|
2020-12-15 17:38:14 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func getCmdLiquidate() *cobra.Command {
|
2020-12-15 17:38:14 +00:00
|
|
|
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(
|
2022-01-08 00:39:27 +00:00
|
|
|
`%s tx %s liquidate kava1hgcfsuwc889wtdmt8pjy7qffua9dd2tralu64j --from <key>`, version.AppName, types.ModuleName,
|
2020-12-15 17:38:14 +00:00
|
|
|
),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2022-01-08 00:39:27 +00:00
|
|
|
clientCtx, err := client.GetClientTxContext(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-15 17:38:14 +00:00
|
|
|
|
|
|
|
borrower, err := sdk.AccAddressFromBech32(args[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
msg := types.NewMsgLiquidate(clientCtx.GetFromAddress(), borrower)
|
2020-12-15 17:38:14 +00:00
|
|
|
if err := msg.ValidateBasic(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg)
|
2020-12-15 17:38:14 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|