-
Notifications
You must be signed in to change notification settings - Fork 39
/
tx.go
190 lines (156 loc) · 4.92 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package cli
import (
"fmt"
"github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/bank/types"
feeutils "github.com/classic-terra/core/v3/custom/auth/client/utils"
)
var FlagSplit = "split"
// GetTxCmd returns the transaction commands for this module
func GetTxCmd() *cobra.Command {
txCmd := &cobra.Command{
Use: types.ModuleName,
Short: "Bank transaction subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
txCmd.AddCommand(sendTxCmd())
txCmd.AddCommand(multiSendTxCmd())
return txCmd
}
// sendTxCmd will create a send tx and sign it with the given key.
func sendTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "send [from_key_or_address] [to_address] [amount]",
Short: `Send funds from one account to another. Note, the'--from' flag is
ignored as it is implied from [from_key_or_address].`,
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
err := cmd.Flags().Set(flags.FlagFrom, args[0])
if err != nil {
return err
}
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
toAddr, err := sdk.AccAddressFromBech32(args[1])
if err != nil {
return err
}
coins, err := sdk.ParseCoinsNormalized(args[2])
if err != nil {
return err
}
msg := types.NewMsgSend(clientCtx.GetFromAddress(), toAddr, coins)
if err := msg.ValidateBasic(); err != nil {
return err
}
// Generate transaction factory for gas simulation
txf, err := tx.NewFactoryCLI(clientCtx, cmd.Flags())
if err != nil {
return err
}
if !clientCtx.GenerateOnly && txf.Fees().IsZero() {
// estimate tax and gas
stdFee, err := feeutils.ComputeFeesWithCmd(clientCtx, cmd.Flags(), msg)
if err != nil {
return err
}
// override gas and fees
txf = txf.
WithFees(stdFee.Amount.String()).
WithGas(stdFee.Gas).
WithSimulateAndExecute(false).
WithGasPrices("")
}
// build and sign the transaction, then broadcast to Tendermint
return tx.GenerateOrBroadcastTxWithFactory(clientCtx, txf, msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// multiSendTxCmd create command handler for creating a MsgMultiSend transaction.
func multiSendTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "multi-send [from_key_or_address] [to_address_1 to_address_2 ...] [amount]",
Short: "Send funds from one account to two or more accounts.",
Long: `Send funds from one account to two or more accounts.
By default, sends the [amount] to each address of the list.
Using the '--split' flag, the [amount] is split equally between the addresses.`,
Args: cobra.MinimumNArgs(4),
RunE: func(cmd *cobra.Command, args []string) error {
cmd.Flags().Set(flags.FlagFrom, args[0])
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
coins, err := sdk.ParseCoinsNormalized(args[len(args)-1])
if err != nil {
return err
}
if coins.IsZero() {
return fmt.Errorf("must send positive amount")
}
split, err := cmd.Flags().GetBool(FlagSplit)
if err != nil {
return err
}
totalAddrs := sdk.NewInt(int64(len(args) - 2))
// coins to be received by the addresses
sendCoins := coins
if split {
sendCoins = coins.QuoInt(totalAddrs)
}
var output []types.Output
for _, arg := range args[1 : len(args)-1] {
toAddr, err := sdk.AccAddressFromBech32(arg)
if err != nil {
return err
}
output = append(output, types.NewOutput(toAddr, sendCoins))
}
// amount to be send from the from address
var amount sdk.Coins
if split {
// user input: 1000stake to send to 3 addresses
// actual: 333stake to each address (=> 999stake actually sent)
amount = sendCoins.MulInt(totalAddrs)
} else {
amount = coins.MulInt(totalAddrs)
}
msg := types.NewMsgMultiSend([]types.Input{types.NewInput(clientCtx.FromAddress, amount)}, output)
if err := msg.ValidateBasic(); err != nil {
return err
}
// Generate transaction factory for gas simulation
txf, err := tx.NewFactoryCLI(clientCtx, cmd.Flags())
if err != nil {
return err
}
if !clientCtx.GenerateOnly && txf.Fees().IsZero() {
// estimate tax and gas
stdFee, err := feeutils.ComputeFeesWithCmd(clientCtx, cmd.Flags(), msg)
if err != nil {
return err
}
// override gas and fees
txf = txf.
WithFees(stdFee.Amount.String()).
WithGas(stdFee.Gas).
WithSimulateAndExecute(false).
WithGasPrices("")
}
return tx.GenerateOrBroadcastTxWithFactory(clientCtx, txf, msg)
},
}
cmd.Flags().Bool(FlagSplit, false, "Send the equally split token amount to each address")
flags.AddTxFlagsToCmd(cmd)
return cmd
}