cosmos-sdk/x/bank/client/cli/tx.go

126 lines
3.8 KiB
Go
Raw Normal View History

2018-04-25 07:18:06 -07:00
package cli
import (
"bufio"
"github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/flags"
2020-03-25 07:55:28 -07:00
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
authclient "github.com/cosmos/cosmos-sdk/x/auth/client"
"github.com/cosmos/cosmos-sdk/x/bank/types"
)
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)
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.
// ---------------------------------------------------------------------------
// 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
func GetTxCmd(cdc *codec.Codec) *cobra.Command {
txCmd := &cobra.Command{
Use: types.ModuleName,
Short: "Bank transaction subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.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.
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
func SendTxCmd(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "send [from_key_or_address] [to_address] [amount]",
Short: "Create and sign a send tx",
Args: cobra.ExactArgs(3),
2018-04-18 21:49:24 -07:00
RunE: func(cmd *cobra.Command, args []string) error {
inBuf := bufio.NewReader(cmd.InOrStdin())
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(authclient.GetTxEncoder(cdc))
cliCtx := context.NewCLIContextWithInputAndFrom(inBuf, args[0]).WithCodec(cdc)
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
// parse coins trying to be sent
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
msg := types.NewMsgSend(cliCtx.GetFromAddress(), to, coins)
return authclient.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
2018-04-18 21:49:24 -07:00
},
2018-03-10 09:33:05 -08:00
}
cmd = flags.PostCommands(cmd)[0]
return cmd
}