cosmos-sdk/x/bank/commands/sendtx.go

81 lines
1.8 KiB
Go
Raw Normal View History

package commands
import (
"encoding/hex"
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
2018-03-02 01:24:07 -08:00
"github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/x/bank"
)
const (
2018-03-03 11:41:43 -08:00
flagTo = "to"
flagAmount = "amount"
)
// SendTxCommand will create a send tx and sign it with the given key
2018-03-10 09:33:05 -08:00
func SendTxCmd(Cdc *wire.Codec) *cobra.Command {
cmdr := Commander{Cdc}
cmd := &cobra.Command{
Use: "send",
Short: "Create and sign a send tx",
2018-02-28 17:57:38 -08:00
RunE: cmdr.sendTxCmd,
}
cmd.Flags().String(flagTo, "", "Address to send coins")
cmd.Flags().String(flagAmount, "", "Amount of coins to send")
return cmd
}
2018-03-10 09:33:05 -08:00
type Commander struct {
Cdc *wire.Codec
2018-02-28 17:57:38 -08:00
}
2018-03-10 09:33:05 -08:00
func (c Commander) sendTxCmd(cmd *cobra.Command, args []string) error {
ctx := context.NewCoreContextFromViper()
2018-03-30 05:57:53 -07:00
2018-03-03 11:20:58 -08:00
// get the from address
2018-03-30 05:57:53 -07:00
from, err := ctx.GetFromAddress()
if err != nil {
return err
}
2018-03-10 09:33:05 -08:00
// parse coins
amount := viper.GetString(flagAmount)
coins, err := sdk.ParseCoins(amount)
if err != nil {
return err
}
// parse destination address
dest := viper.GetString(flagTo)
bz, err := hex.DecodeString(dest)
if err != nil {
return err
}
to := sdk.Address(bz)
2018-03-14 05:01:55 -07:00
// build message
msg := BuildMsg(from, to, coins)
2018-03-03 11:20:58 -08:00
// build and sign the transaction, then broadcast to Tendermint
2018-04-02 05:13:13 -07:00
res, err := ctx.SignBuildBroadcast(ctx.FromAddressName, msg, c.Cdc)
2018-02-23 01:40:10 -08:00
if err != nil {
2018-03-03 11:20:58 -08:00
return err
}
2018-03-03 11:20:58 -08:00
fmt.Printf("Committed at block %d. Hash: %s\n", res.Height, res.Hash.String())
return nil
}
2018-03-10 09:33:05 -08:00
func BuildMsg(from sdk.Address, to sdk.Address, coins sdk.Coins) sdk.Msg {
input := bank.NewInput(from, coins)
output := bank.NewOutput(to, coins)
msg := bank.NewSendMsg([]bank.Input{input}, []bank.Output{output})
return msg
}