-
Notifications
You must be signed in to change notification settings - Fork 117
/
tx.go
127 lines (107 loc) · 3.56 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
package cli
import (
"fmt"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
legacyTx "github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/spf13/cobra"
"github.com/axelarnetwork/axelar-core/x/snapshot/types"
)
// GetTxCmd returns the transaction commands for this module
func GetTxCmd() *cobra.Command {
snapshotTxCmd := &cobra.Command{
Use: types.ModuleName,
Short: fmt.Sprintf("%s transactions subcommands", types.ModuleName),
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
snapshotTxCmd.AddCommand(
GetCmdRegisterProxy(),
GetCmdDeregisterProxy(),
GetCmdSendTokens(),
)
return snapshotTxCmd
}
// GetCmdRegisterProxy returns the command to register a proxy
func GetCmdRegisterProxy() *cobra.Command {
cmd := &cobra.Command{
Use: "register-proxy [proxy address]",
Short: "Register a proxy account for a specific validator principal to broadcast transactions in its stead",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
addr, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return sdkerrors.Wrap(types.ErrSnapshot, "proxy invalid")
}
msg := types.NewRegisterProxyRequest(sdk.ValAddress(clientCtx.FromAddress), addr)
return legacyTx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// GetCmdDeregisterProxy returns the command to register a proxy
func GetCmdDeregisterProxy() *cobra.Command {
cmd := &cobra.Command{
Use: "deactivate-proxy",
Short: "Deactivate the proxy account of the sender",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
msg := types.NewDeactivateProxyRequest(sdk.ValAddress(clientCtx.FromAddress))
return legacyTx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// GetCmdSendTokens returns the command to send stake to a number of addresses
func GetCmdSendTokens() *cobra.Command {
cmd := &cobra.Command{
Use: "send-tokens [amount] [address 1] ... [address n]",
Short: "Sends the specified amount of tokens to the designated addresses",
Args: cobra.MinimumNArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
decCoins, err := sdk.ParseDecCoins(args[0])
if err != nil {
return err
}
if decCoins.Len() != 1 {
return fmt.Errorf("only a single amount is permitted")
}
coins, decimals := decCoins.TruncateDecimal()
if !decimals.IsZero() {
return fmt.Errorf("amount must be an integer value")
}
inputs := make([]banktypes.Input, 0)
outputs := make([]banktypes.Output, 0)
for _, addr := range args[1:] {
to, err := sdk.AccAddressFromBech32(addr)
if err != nil {
return err
}
inputs = append(inputs, banktypes.NewInput(clientCtx.FromAddress, coins))
outputs = append(outputs, banktypes.NewOutput(to, coins))
}
msg := banktypes.NewMsgMultiSend(inputs, outputs)
return legacyTx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}