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

60 lines
1.5 KiB
Go
Raw Normal View History

2018-04-25 07:18:06 -07:00
package cli
import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
2018-08-06 11:11:30 -07:00
"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"
2018-08-06 11:11:30 -07:00
"github.com/spf13/cobra"
)
const (
2018-03-03 11:41:43 -08:00
flagTo = "to"
flagAmount = "amount"
)
2018-08-06 11:11:30 -07:00
// SendTxCmd will create a send tx and sign it with the given key.
func SendTxCmd(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
2019-02-06 16:15:37 -08:00
Use: "send [to_address] [amount]",
Short: "Create and sign a send tx",
2019-02-06 16:15:37 -08:00
Args: cobra.ExactArgs(2),
2018-04-18 21:49:24 -07:00
RunE: func(cmd *cobra.Command, args []string) error {
txBldr := authtxb.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc))
2018-08-06 11:11:30 -07:00
cliCtx := context.NewCLIContext().
WithCodec(cdc).
WithAccountDecoder(cdc)
2018-08-06 11:11:30 -07:00
if err := cliCtx.EnsureAccountExists(); err != nil {
2018-04-18 21:49:24 -07:00
return err
}
2018-03-30 05:57:53 -07:00
2019-02-06 16:15:37 -08:00
to, err := sdk.AccAddressFromBech32(args[0])
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
2019-02-06 16:15:37 -08:00
coins, err := sdk.ParseCoins(args[1])
2018-04-18 21:49:24 -07:00
if err != nil {
return err
}
2018-03-10 09:33:05 -08:00
from := cliCtx.GetFromAddress()
2018-04-18 21:49:24 -07:00
// build and sign the transaction, then broadcast to Tendermint
msg := bank.NewMsgSend(from, to, coins)
2019-02-06 16:15:37 -08:00
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg}, false)
2018-04-18 21:49:24 -07:00
},
2018-03-10 09:33:05 -08:00
}
cmd = client.PostCommands(cmd)[0]
cmd.MarkFlagRequired(client.FlagFrom)
return cmd
}