2018-04-25 07:18:06 -07:00
|
|
|
package cli
|
2018-02-22 09:51:08 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
2018-04-02 05:05:23 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
2018-02-22 09:51:08 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-03-02 01:24:07 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/wire"
|
2018-04-25 07:18:06 -07:00
|
|
|
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/bank/client"
|
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-02-23 01:49:30 -08:00
|
|
|
// SendTxCommand will create a send tx and sign it with the given key
|
2018-04-18 21:49:24 -07:00
|
|
|
func SendTxCmd(cdc *wire.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 {
|
|
|
|
ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc))
|
2018-02-22 09:51:08 -08:00
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
// get the from/to address
|
|
|
|
from, err := ctx.GetFromAddress()
|
|
|
|
if err != nil {
|
|
|
|
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-06-01 07:23:58 -07:00
|
|
|
to, err := sdk.GetAccAddressBech32(toStr)
|
2018-04-18 21:49:24 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// parse coins
|
|
|
|
amount := viper.GetString(flagAmount)
|
|
|
|
coins, err := sdk.ParseCoins(amount)
|
|
|
|
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
|
2018-04-25 07:18:06 -07:00
|
|
|
msg := client.BuildMsg(from, to, coins)
|
2018-06-21 15:05:25 -07:00
|
|
|
res, err := ctx.EnsureSignBuildBroadcast(ctx.FromAddressName, []sdk.Msg{msg}, cdc)
|
2018-04-18 21:49:24 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Printf("Committed at block %d. Hash: %s\n", res.Height, res.Hash.String())
|
|
|
|
return nil
|
|
|
|
},
|
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")
|
|
|
|
return cmd
|
2018-02-22 09:51:08 -08:00
|
|
|
}
|