53 lines
1.4 KiB
Go
53 lines
1.4 KiB
Go
package cli
|
|
|
|
import (
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
|
"github.com/cosmos/cosmos-sdk/client/utils"
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder"
|
|
"github.com/cosmos/cosmos-sdk/x/bank"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
const (
|
|
flagTo = "to"
|
|
flagAmount = "amount"
|
|
)
|
|
|
|
// SendTxCmd will create a send tx and sign it with the given key.
|
|
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),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
txBldr := authtxb.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc))
|
|
cliCtx := context.NewCLIContextWithFrom(args[0]).
|
|
WithCodec(cdc).
|
|
WithAccountDecoder(cdc)
|
|
|
|
to, err := sdk.AccAddressFromBech32(args[1])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// parse coins trying to be sent
|
|
coins, err := sdk.ParseCoins(args[2])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// build and sign the transaction, then broadcast to Tendermint
|
|
msg := bank.NewMsgSend(cliCtx.GetFromAddress(), to, coins)
|
|
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg}, false)
|
|
},
|
|
}
|
|
|
|
cmd = client.PostCommands(cmd)[0]
|
|
|
|
return cmd
|
|
}
|