2018-04-25 07:18:06 -07:00
|
|
|
package cli
|
2018-02-22 09:51:08 -08:00
|
|
|
|
|
|
|
import (
|
2019-06-05 16:26:17 -07:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2018-11-19 09:02:34 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2018-04-02 05:05:23 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
2018-08-06 11:11:30 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/utils"
|
2018-09-13 11:17:32 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2018-02-22 09:51:08 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-06-05 16:26:17 -07:00
|
|
|
auth "github.com/cosmos/cosmos-sdk/x/auth"
|
2019-06-14 07:10:37 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/bank/internal/types"
|
2018-02-22 09:51:08 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-03-03 11:41:43 -08:00
|
|
|
flagTo = "to"
|
|
|
|
flagAmount = "amount"
|
2018-02-22 09:51:08 -08:00
|
|
|
)
|
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
// GetTxCmd returns the transaction commands for this module
|
|
|
|
func GetTxCmd(cdc *codec.Codec) *cobra.Command {
|
|
|
|
txCmd := &cobra.Command{
|
|
|
|
Use: types.ModuleName,
|
|
|
|
Short: "Bank transaction subcommands",
|
|
|
|
DisableFlagParsing: true,
|
|
|
|
SuggestionsMinimumDistance: 2,
|
|
|
|
RunE: utils.ValidateCmd,
|
|
|
|
}
|
|
|
|
txCmd.AddCommand(
|
|
|
|
SendTxCmd(cdc),
|
|
|
|
)
|
|
|
|
return txCmd
|
|
|
|
}
|
|
|
|
|
2018-08-06 11:11:30 -07:00
|
|
|
// SendTxCmd will create a send tx and sign it with the given key.
|
2018-09-13 11:17:32 -07:00
|
|
|
func SendTxCmd(cdc *codec.Codec) *cobra.Command {
|
2018-02-22 09:51:08 -08:00
|
|
|
cmd := &cobra.Command{
|
2019-04-18 11:48:52 -07:00
|
|
|
Use: "send [from_key_or_address] [to_address] [amount]",
|
2018-02-22 09:51:08 -08:00
|
|
|
Short: "Create and sign a send tx",
|
2019-04-18 11:48:52 -07:00
|
|
|
Args: cobra.ExactArgs(3),
|
2018-04-18 21:49:24 -07:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2019-06-05 16:26:17 -07:00
|
|
|
txBldr := auth.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc))
|
2019-04-18 11:48:52 -07:00
|
|
|
cliCtx := context.NewCLIContextWithFrom(args[0]).
|
2018-08-06 11:11:30 -07:00
|
|
|
WithCodec(cdc).
|
2018-11-19 09:02:34 -08:00
|
|
|
WithAccountDecoder(cdc)
|
2018-02-22 09:51:08 -08:00
|
|
|
|
2019-04-18 11:48:52 -07:00
|
|
|
to, err := sdk.AccAddressFromBech32(args[1])
|
2018-04-18 21:49:24 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-08-06 11:11:30 -07:00
|
|
|
|
2018-06-30 01:27:58 -07:00
|
|
|
// parse coins trying to be sent
|
2019-04-18 11:48:52 -07:00
|
|
|
coins, err := sdk.ParseCoins(args[2])
|
2018-04-18 21:49:24 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-03-10 09:33:05 -08:00
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
// build and sign the transaction, then broadcast to Tendermint
|
2019-06-05 16:26:17 -07:00
|
|
|
msg := types.NewMsgSend(cliCtx.GetFromAddress(), to, coins)
|
2019-05-08 13:06:05 -07:00
|
|
|
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
|
2018-04-18 21:49:24 -07:00
|
|
|
},
|
2018-03-10 09:33:05 -08:00
|
|
|
}
|
2019-03-26 07:36:10 -07:00
|
|
|
|
|
|
|
cmd = client.PostCommands(cmd)[0]
|
|
|
|
|
|
|
|
return cmd
|
2018-02-22 09:51:08 -08:00
|
|
|
}
|