2018-04-25 07:18:06 -07:00
|
|
|
package cli
|
2018-02-22 09:51:08 -08:00
|
|
|
|
|
|
|
import (
|
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"
|
2018-04-25 07:18:06 -07:00
|
|
|
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
|
2018-09-07 10:04:58 -07:00
|
|
|
authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder"
|
2018-04-25 07:18:06 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/bank/client"
|
2018-08-06 11:11:30 -07:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2018-07-05 19:15:35 -07:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
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
|
|
|
)
|
|
|
|
|
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{
|
|
|
|
Use: "send",
|
|
|
|
Short: "Create and sign a send tx",
|
2018-04-18 21:49:24 -07:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2018-09-07 10:15:49 -07:00
|
|
|
txBldr := authtxb.NewTxBuilderFromCLI().WithCodec(cdc)
|
2018-08-06 11:11:30 -07:00
|
|
|
cliCtx := context.NewCLIContext().
|
|
|
|
WithCodec(cdc).
|
|
|
|
WithAccountDecoder(authcmd.GetAccountDecoder(cdc))
|
2018-02-22 09:51:08 -08:00
|
|
|
|
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
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
toStr := viper.GetString(flagTo)
|
2018-05-26 14:21:29 -07:00
|
|
|
|
2018-07-09 16:06:05 -07:00
|
|
|
to, err := sdk.AccAddressFromBech32(toStr)
|
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
|
2018-04-18 21:49:24 -07:00
|
|
|
amount := viper.GetString(flagAmount)
|
|
|
|
coins, err := sdk.ParseCoins(amount)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-03-10 09:33:05 -08:00
|
|
|
|
2018-08-06 11:11:30 -07:00
|
|
|
from, err := cliCtx.GetFromAddress()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
account, err := cliCtx.GetAccount(from)
|
2018-06-30 01:27:58 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-08-06 11:11:30 -07:00
|
|
|
|
|
|
|
// ensure account has enough coins
|
2018-06-30 01:27:58 -07:00
|
|
|
if !account.GetCoins().IsGTE(coins) {
|
2018-07-09 01:47:38 -07:00
|
|
|
return errors.Errorf("Address %s doesn't have enough coins to pay for this transaction.", from)
|
2018-06-30 01:27:58 -07:00
|
|
|
}
|
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
// build and sign the transaction, then broadcast to Tendermint
|
2018-09-26 06:29:39 -07:00
|
|
|
msg := client.CreateMsg(from, to, coins)
|
2018-09-02 11:20:14 -07:00
|
|
|
if cliCtx.GenerateOnly {
|
2018-10-10 15:45:41 -07:00
|
|
|
return utils.PrintUnsignedStdTx(txBldr, cliCtx, []sdk.Msg{msg}, false)
|
2018-09-02 11:20:14 -07:00
|
|
|
}
|
2018-06-28 12:23:47 -07:00
|
|
|
|
2018-09-26 06:29:39 -07:00
|
|
|
return utils.CompleteAndBroadcastTxCli(txBldr, cliCtx, []sdk.Msg{msg})
|
2018-04-18 21:49:24 -07:00
|
|
|
},
|
2018-03-10 09:33:05 -08:00
|
|
|
}
|
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
cmd.Flags().String(flagTo, "", "Address to send coins")
|
|
|
|
cmd.Flags().String(flagAmount, "", "Amount of coins to send")
|
2018-10-20 23:52:45 -07:00
|
|
|
cmd.MarkFlagRequired(flagTo)
|
|
|
|
cmd.MarkFlagRequired(flagAmount)
|
2018-06-28 12:23:47 -07:00
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
return cmd
|
2018-02-22 09:51:08 -08:00
|
|
|
}
|