2018-04-25 07:18:06 -07:00
|
|
|
package cli
|
2018-02-22 09:51:08 -08:00
|
|
|
|
|
|
|
import (
|
2019-11-14 06:17:21 -08:00
|
|
|
"bufio"
|
|
|
|
|
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"
|
2019-12-23 08:05:47 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
2020-03-25 07:55:28 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/tx"
|
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-15 05:34:11 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
2020-01-24 08:40:56 -08:00
|
|
|
authclient "github.com/cosmos/cosmos-sdk/x/auth/client"
|
2020-02-20 06:24:26 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/bank/types"
|
2018-02-22 09:51:08 -08:00
|
|
|
)
|
|
|
|
|
2020-03-25 07:55:28 -07:00
|
|
|
// NewTxCmd returns a root CLI command handler for all x/bank transaction commands.
|
|
|
|
func NewTxCmd(m codec.Marshaler, txg tx.Generator, ar tx.AccountRetriever) *cobra.Command {
|
|
|
|
txCmd := &cobra.Command{
|
|
|
|
Use: types.ModuleName,
|
|
|
|
Short: "Bank transaction subcommands",
|
|
|
|
DisableFlagParsing: true,
|
|
|
|
SuggestionsMinimumDistance: 2,
|
|
|
|
RunE: client.ValidateCmd,
|
|
|
|
}
|
|
|
|
|
2020-03-25 09:30:10 -07:00
|
|
|
txCmd.AddCommand(NewSendTxCmd(m, txg, ar))
|
2020-03-25 07:55:28 -07:00
|
|
|
|
|
|
|
return txCmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSendTxCmd returns a CLI command handler for creating a MsgSend transaction.
|
|
|
|
func NewSendTxCmd(m codec.Marshaler, txg tx.Generator, ar tx.AccountRetriever) *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "send [from_key_or_address] [to_address] [amount]",
|
|
|
|
Short: "Create and/or sign and broadcast a MsgSend transaction",
|
|
|
|
Args: cobra.ExactArgs(3),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
inBuf := bufio.NewReader(cmd.InOrStdin())
|
|
|
|
cliCtx := context.NewCLIContextWithInputAndFrom(inBuf, args[0]).WithMarshaler(m)
|
2020-04-02 12:07:47 -07:00
|
|
|
txf := tx.NewFactoryFromCLI(inBuf).WithTxGenerator(txg).WithAccountRetriever(ar)
|
2020-03-25 07:55:28 -07:00
|
|
|
|
|
|
|
toAddr, err := sdk.AccAddressFromBech32(args[1])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
coins, err := sdk.ParseCoins(args[2])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := types.NewMsgSend(cliCtx.GetFromAddress(), toAddr, coins)
|
2020-04-09 07:00:10 -07:00
|
|
|
if err := msg.ValidateBasic(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-27 10:25:56 -07:00
|
|
|
return tx.GenerateOrBroadcastTx(cliCtx, txf, msg)
|
2020-03-25 07:55:28 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return flags.PostCommands(cmd)[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// Deprecated
|
|
|
|
//
|
|
|
|
// TODO: Remove once client-side Protobuf migration has been completed.
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
// GetTxCmd returns the transaction commands for this module
|
2020-03-25 07:55:28 -07:00
|
|
|
//
|
|
|
|
// TODO: Remove once client-side Protobuf migration has been completed.
|
|
|
|
// ref: https://github.com/cosmos/cosmos-sdk/issues/5864
|
2019-06-05 16:26:17 -07:00
|
|
|
func GetTxCmd(cdc *codec.Codec) *cobra.Command {
|
|
|
|
txCmd := &cobra.Command{
|
|
|
|
Use: types.ModuleName,
|
|
|
|
Short: "Bank transaction subcommands",
|
|
|
|
DisableFlagParsing: true,
|
|
|
|
SuggestionsMinimumDistance: 2,
|
2019-06-15 05:34:11 -07:00
|
|
|
RunE: client.ValidateCmd,
|
2019-06-05 16:26:17 -07:00
|
|
|
}
|
|
|
|
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.
|
2020-03-25 07:55:28 -07:00
|
|
|
//
|
|
|
|
// TODO: Remove once client-side Protobuf migration has been completed.
|
|
|
|
// ref: https://github.com/cosmos/cosmos-sdk/issues/5864
|
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-11-14 06:17:21 -08:00
|
|
|
inBuf := bufio.NewReader(cmd.InOrStdin())
|
2020-01-24 08:40:56 -08:00
|
|
|
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(authclient.GetTxEncoder(cdc))
|
2019-11-14 06:17:21 -08:00
|
|
|
cliCtx := context.NewCLIContextWithInputAndFrom(inBuf, args[0]).WithCodec(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)
|
2020-01-24 08:40:56 -08:00
|
|
|
return authclient.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
|
|
|
|
2019-12-23 08:05:47 -08:00
|
|
|
cmd = flags.PostCommands(cmd)[0]
|
2019-03-26 07:36:10 -07:00
|
|
|
|
|
|
|
return cmd
|
2018-02-22 09:51:08 -08:00
|
|
|
}
|