forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendtx.go
83 lines (68 loc) · 2.08 KB
/
sendtx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package cli
import (
"github.com/pkg/errors"
"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/x/auth"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
"github.com/cosmos/cosmos-sdk/x/bank/client"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
const (
flagTo = "to"
flagAmount = "amount"
)
// SendTxCmd will create a send tx and sign it with the given key
func SendTxCmd(cdc *wire.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "send",
Short: "Create and sign a send tx",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc))
// get the from/to address
from, err := ctx.GetFromAddress()
if err != nil {
return err
}
fromAcc, err := ctx.QueryStore(auth.AddressStoreKey(from), ctx.AccountStore)
if err != nil {
return err
}
// Check if account was found
if fromAcc == nil {
return errors.Errorf("No account with address %s was found in the state.\nAre you sure there has been a transaction involving it?", from)
}
toStr := viper.GetString(flagTo)
to, err := sdk.AccAddressFromBech32(toStr)
if err != nil {
return err
}
// parse coins trying to be sent
amount := viper.GetString(flagAmount)
coins, err := sdk.ParseCoins(amount)
if err != nil {
return err
}
// ensure account has enough coins
account, err := ctx.Decoder(fromAcc)
if err != nil {
return err
}
if !account.GetCoins().IsGTE(coins) {
return errors.Errorf("Address %s doesn't have enough coins to pay for this transaction.", from)
}
// build and sign the transaction, then broadcast to Tendermint
msg := client.BuildMsg(from, to, coins)
err = ctx.EnsureSignBuildBroadcast(ctx.FromAddressName, []sdk.Msg{msg}, cdc)
if err != nil {
return err
}
return nil
},
}
cmd.Flags().String(flagTo, "", "Address to send coins")
cmd.Flags().String(flagAmount, "", "Amount of coins to send")
return cmd
}