-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx.go
207 lines (176 loc) · 6.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package cli
import (
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govcli "github.com/cosmos/cosmos-sdk/x/gov/client/cli"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/cosmos/cosmos-sdk/x/sanction"
)
var (
// DefaultAuthorityAddr is the default authority to provide in the sanction module's governance proposal messages.
// It should match the value provided to the sanction keeper constructor.
// It is defined as a sdk.AccAddress to be independent of global bech32 HRP definition.
DefaultAuthorityAddr = authtypes.NewModuleAddress(govtypes.ModuleName)
// exampleTxCmdBase is the base command that gets a user to one of the tx commands in here.
exampleTxCmdBase = fmt.Sprintf("%s tx %s", version.AppName, sanction.ModuleName)
// exampleTxAddr1 is a constant address for use in example strings.
exampleTxAddr1 = sdk.AccAddress("exampleTxAddr1______")
// exampleTxAddr2 is a constant address for use in example strings.
exampleTxAddr2 = sdk.AccAddress("exampleTxAddr2______")
)
// TxCmd returns the command with sub-commands for specific sanction module Tx interaction.
func TxCmd() *cobra.Command {
txCmd := &cobra.Command{
Use: sanction.ModuleName,
Short: "Sanction transaction subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
txCmd.AddCommand(
TxSanctionCmd(),
TxUnsanctionCmd(),
TxUpdateParamsCmd(),
)
return txCmd
}
// TxSanctionCmd returns the command for submitting a MsgSanction governance proposal tx.
func TxSanctionCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "sanction <address 1> [<address 2> ...]",
Short: "Submit a governance proposal to sanction one or more addresses",
Long: `Submit a governance proposal to sanction one or more addresses.
At least one address is required; any number of addresses can be provided.
Each address should be a valid bech32 encoded string.`,
Example: fmt.Sprintf(`
$ %[1]s sanction %[2]s
$ %[1]s sanction %[3]s %[2]s
`,
exampleTxCmdBase, exampleTxAddr1, exampleTxAddr2),
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
flagSet := cmd.Flags()
msgSanction := &sanction.MsgSanction{
Addresses: args,
Authority: getAuthority(flagSet),
}
if err = msgSanction.ValidateBasic(); err != nil {
return err
}
return govcli.GenerateOrBroadcastTxCLIAsGovProp(clientCtx, flagSet, msgSanction)
},
}
flags.AddTxFlagsToCmd(cmd)
govcli.AddGovPropFlagsToCmd(cmd)
addAuthorityFlagToCmd(cmd)
return cmd
}
// TxUnsanctionCmd returns the command for submitting a MsgUnsanction governance proposal tx.
func TxUnsanctionCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "unsanction <address 1> [<address 2> ...]",
Short: "Submit a governance proposal to unsanction one or more addresses",
Long: `Submit a governance proposal to unsanction one or more addresses.
At least one address is required; any number of addresses can be provided.
Each address should be a valid bech32 encoded string.`,
Example: fmt.Sprintf(`
$ %[1]s unsanction %[3]s
$ %[1]s unsanction %[2]s %[3]s
`,
exampleTxCmdBase, exampleTxAddr1, exampleTxAddr2),
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
flagSet := cmd.Flags()
msgUnsanction := &sanction.MsgUnsanction{
Addresses: args,
Authority: getAuthority(flagSet),
}
if err = msgUnsanction.ValidateBasic(); err != nil {
return err
}
return govcli.GenerateOrBroadcastTxCLIAsGovProp(clientCtx, flagSet, msgUnsanction)
},
}
flags.AddTxFlagsToCmd(cmd)
govcli.AddGovPropFlagsToCmd(cmd)
addAuthorityFlagToCmd(cmd)
return cmd
}
// TxUpdateParamsCmd returns the command for submitting a MsgUpdateParams governance proposal tx.
func TxUpdateParamsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "update-params <immediate_sanction_min_deposit> <immediate_unsanction_min_deposit>",
Short: "Submit a governance proposal to update the sanction module's params",
Long: `Submit a governance proposal to update the sanction module's params.
Both <immediate_sanction_min_deposit> and <immediate_unsanction_min_deposit> are required.
They must be coins or empty strings.`,
Example: fmt.Sprintf(`
$ %[1]s update-params 100%[2]s 150%[2]s
$ %[1]s update-params '' 50%[2]s
$ %[1]s update-params 75%[2]s ''
$ %[1]s update-params '' ''
`,
exampleTxCmdBase, sdk.DefaultBondDenom),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
flagSet := cmd.Flags()
msgUpdateParams := &sanction.MsgUpdateParams{
Params: &sanction.Params{},
Authority: getAuthority(flagSet),
}
if len(args[0]) > 0 {
msgUpdateParams.Params.ImmediateSanctionMinDeposit, err = sdk.ParseCoinsNormalized(args[0])
if err != nil {
return fmt.Errorf("invalid immediate_sanction_min_deposit string %q: %w", args[0], err)
}
}
if len(args[1]) > 0 {
msgUpdateParams.Params.ImmediateUnsanctionMinDeposit, err = sdk.ParseCoinsNormalized(args[1])
if err != nil {
return fmt.Errorf("invalid immediate_unsanction_min_deposit string %q: %w", args[1], err)
}
}
if err = msgUpdateParams.ValidateBasic(); err != nil {
return err
}
return govcli.GenerateOrBroadcastTxCLIAsGovProp(clientCtx, flagSet, msgUpdateParams)
},
}
flags.AddTxFlagsToCmd(cmd)
govcli.AddGovPropFlagsToCmd(cmd)
addAuthorityFlagToCmd(cmd)
return cmd
}
// addAuthorityFlagToCmd adds the authority flag to a command.
func addAuthorityFlagToCmd(cmd *cobra.Command) {
// Note: Not setting a default here because the HRP might not yet be set correctly.
cmd.Flags().String(flags.FlagAuthority, "", "The authority to use. If not provided, a default is used")
}
// getAuthority gets the authority string from the flagSet or returns the default.
func getAuthority(flagSet *pflag.FlagSet) string {
// Ignoring the error here since we really don't care,
// and it's easier if this just returns a string.
authority, _ := flagSet.GetString(flags.FlagAuthority)
if len(authority) > 0 {
return authority
}
return DefaultAuthorityAddr.String()
}