-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx.go
64 lines (54 loc) · 1.93 KB
/
tx.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
package cli
import (
"bufio"
"github.com/spf13/cobra"
"github.com/furyaxyz/fuxchain/libs/cosmos-sdk/client"
"github.com/furyaxyz/fuxchain/libs/cosmos-sdk/client/context"
"github.com/furyaxyz/fuxchain/libs/cosmos-sdk/client/flags"
"github.com/furyaxyz/fuxchain/libs/cosmos-sdk/codec"
sdk "github.com/furyaxyz/fuxchain/libs/cosmos-sdk/types"
"github.com/furyaxyz/fuxchain/libs/cosmos-sdk/x/auth"
"github.com/furyaxyz/fuxchain/libs/cosmos-sdk/x/auth/client/utils"
"github.com/furyaxyz/fuxchain/libs/cosmos-sdk/x/bank/internal/types"
)
// GetTxCmd returns the transaction commands for this module
func GetTxCmd(cdc *codec.Codec) *cobra.Command {
txCmd := &cobra.Command{
Use: types.ModuleName,
Short: "Bank transaction subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
txCmd.AddCommand(
SendTxCmd(cdc),
)
return txCmd
}
// SendTxCmd will create a send tx and sign it with the given key.
func SendTxCmd(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "send [from_key_or_address] [to_address] [amount]",
Short: "Create and sign a send tx",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
inBuf := bufio.NewReader(cmd.InOrStdin())
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))
cliCtx := context.NewCLIContextWithInputAndFrom(inBuf, args[0]).WithCodec(cdc)
to, err := sdk.AccAddressFromBech32(args[1])
if err != nil {
return err
}
// parse coins trying to be sent
coins, err := sdk.ParseCoins(args[2])
if err != nil {
return err
}
// build and sign the transaction, then broadcast to Tendermint
msg := types.NewMsgSend(cliCtx.GetFromAddress(), to, coins)
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
cmd = flags.PostCommands(cmd)[0]
return cmd
}