-
Notifications
You must be signed in to change notification settings - Fork 44
/
tx.go
363 lines (290 loc) · 10.4 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package cli
// DONTCOVER
import (
"fmt"
cliutils "github.com/desmos-labs/desmos/v7/x/reactions/client/utils"
poststypes "github.com/desmos-labs/desmos/v7/x/posts/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/version"
"github.com/spf13/cobra"
"github.com/desmos-labs/desmos/v7/x/reactions/types"
subspacestypes "github.com/desmos-labs/desmos/v7/x/subspaces/types"
)
const (
FlagRegisteredReaction = "registered-reaction"
FlagFreeTextReaction = "free-text"
FlagShorthandCode = "shorthand-code"
FlagDisplayValue = "display-value"
)
// NewTxCmd returns a new command to perform reactions transactions
func NewTxCmd() *cobra.Command {
txCmd := &cobra.Command{
Use: types.ModuleName,
Short: "Reactions transaction subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
txCmd.AddCommand(
GetCmdAddReaction(),
GetCmdRemoveReaction(),
GetRegisteredReactionsTxCmd(),
GetCmdSetParams(),
)
return txCmd
}
// GetCmdAddReaction returns the command allowing to add a reaction to a post
func GetCmdAddReaction() *cobra.Command {
cmd := &cobra.Command{
Use: "add [subspace-id] [post-id]",
Args: cobra.ExactArgs(2),
Short: "Add a reaction to a post",
Long: fmt.Sprintf(`
Add a reaction to the post with the given id inside the specified subspace.
In order to specify the reaction value, either --%s or --%s must be used`, FlagRegisteredReaction, FlagFreeTextReaction),
Example: fmt.Sprintf(`
%[1]s tx reactions add 1 2 --%[2]s 1 --from alice
%[1]s tx reactions add 1 2 --%[3]s "🚀" --from alice
`, version.AppName, FlagRegisteredReaction, FlagFreeTextReaction),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
subspaceID, err := subspacestypes.ParseSubspaceID(args[0])
if err != nil {
return err
}
postID, err := poststypes.ParsePostID(args[1])
if err != nil {
return err
}
registeredReactionValue, err := cmd.Flags().GetUint32(FlagRegisteredReaction)
if err != nil {
return err
}
freeTextValue, err := cmd.Flags().GetString(FlagFreeTextReaction)
if err != nil {
return err
}
var value types.ReactionValue
switch {
case registeredReactionValue != 0 && freeTextValue != "":
return fmt.Errorf("please use only one of either --%s or --%s", FlagRegisteredReaction, FlagFreeTextReaction)
case registeredReactionValue != 0:
value = types.NewRegisteredReactionValue(registeredReactionValue)
case freeTextValue != "":
value = types.NewFreeTextValue(freeTextValue)
default:
return fmt.Errorf("at least one of either --%s or --%s must be specfied", FlagRegisteredReaction, FlagFreeTextReaction)
}
user := clientCtx.FromAddress.String()
msg := types.NewMsgAddReaction(subspaceID, postID, value, user)
if err = msg.ValidateBasic(); err != nil {
return fmt.Errorf("message validation failed: %w", err)
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
cmd.Flags().Uint32(FlagRegisteredReaction, 0, "Registered reaction id with which to react")
cmd.Flags().String(FlagFreeTextReaction, "", "Free text value with which to react")
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// GetCmdRemoveReaction returns the command allowing to remove a reaction from a post
func GetCmdRemoveReaction() *cobra.Command {
cmd := &cobra.Command{
Use: "remove [subspace-id] [post-id] [reaction-id]",
Args: cobra.ExactArgs(3),
Short: "Remove the reaction with the given id from the provided post",
Example: fmt.Sprintf(`%s tx reactions remove 1 1 1 --from alice`, version.AppName),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
subspaceID, err := subspacestypes.ParseSubspaceID(args[0])
if err != nil {
return err
}
postID, err := poststypes.ParsePostID(args[1])
if err != nil {
return err
}
reactionID, err := types.ParseReactionID(args[2])
if err != nil {
return err
}
user := clientCtx.FromAddress.String()
msg := types.NewMsgRemoveReaction(subspaceID, postID, reactionID, user)
if err = msg.ValidateBasic(); err != nil {
return fmt.Errorf("message validation failed: %w", err)
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// --------------------------------------------------------------------------------------------------------------------
// GetRegisteredReactionsTxCmd returns a new command to perform registered reactions transactions
func GetRegisteredReactionsTxCmd() *cobra.Command {
txCmd := &cobra.Command{
Use: "registered",
Short: "Registered reactions transaction subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
txCmd.AddCommand(
GetCmdAddRegisteredReaction(),
GetCmdEditRegisteredReaction(),
GetCmdRemoveRegisteredReaction(),
)
return txCmd
}
// GetCmdAddRegisteredReaction returns the command allowing to add a registered reaction
func GetCmdAddRegisteredReaction() *cobra.Command {
cmd := &cobra.Command{
Use: "add [subspace-id] [shorthand-code] [display-value]",
Args: cobra.ExactArgs(3),
Short: "Register a new reaction",
Long: "Register a new reaction with the specified shorthand code and display value inside the given subspace",
Example: fmt.Sprintf(`%s tx reactions registered add 1 ":hello:" "https://example.com?image=hello.png" --from alice`, version.AppName),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
subspaceID, err := subspacestypes.ParseSubspaceID(args[0])
if err != nil {
return err
}
shorthandCode := args[1]
displayValue := args[2]
signer := clientCtx.FromAddress.String()
msg := types.NewMsgAddRegisteredReaction(subspaceID, shorthandCode, displayValue, signer)
if err = msg.ValidateBasic(); err != nil {
return fmt.Errorf("message validation failed: %w", err)
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// GetCmdEditRegisteredReaction returns the command allowing to edit a registered reaction
func GetCmdEditRegisteredReaction() *cobra.Command {
cmd := &cobra.Command{
Use: "edit [subspace-id] [reaction-id]",
Args: cobra.ExactArgs(2),
Short: "Edit an existing registered reaction",
Example: fmt.Sprintf(`
%s tx reactions registered edit 1 1 \
--shorthand-code ":wave:" \
--display-value "https://example.com?image=wave.png" \
--from alice
`, version.AppName),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
subspaceID, err := subspacestypes.ParseSubspaceID(args[0])
if err != nil {
return err
}
reactionID, err := types.ParseReactionID(args[1])
if err != nil {
return err
}
shorthandCode, err := cmd.Flags().GetString(FlagShorthandCode)
if err != nil {
return err
}
displayValue, err := cmd.Flags().GetString(FlagDisplayValue)
if err != nil {
return err
}
signer := clientCtx.FromAddress.String()
msg := types.NewMsgEditRegisteredReaction(subspaceID, reactionID, shorthandCode, displayValue, signer)
if err = msg.ValidateBasic(); err != nil {
return fmt.Errorf("message validation failed: %w", err)
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
cmd.Flags().String(FlagShorthandCode, types.DoNotModify, "New shorthand code to be set")
cmd.Flags().String(FlagDisplayValue, types.DoNotModify, "New display value to be set")
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// GetCmdRemoveRegisteredReaction returns the command allowing to remove a registered reaction
func GetCmdRemoveRegisteredReaction() *cobra.Command {
cmd := &cobra.Command{
Use: "remove [subspace-id] [reaction-id]",
Args: cobra.ExactArgs(2),
Short: "Remove a registered reaction from a subspace",
Example: fmt.Sprintf(`%s tx reactions registered remove 1 1 --from alice`, version.AppName),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
subspaceID, err := subspacestypes.ParseSubspaceID(args[0])
if err != nil {
return err
}
reactionID, err := types.ParseRegisteredReactionID(args[1])
if err != nil {
return err
}
signer := clientCtx.FromAddress.String()
msg := types.NewMsgRemoveRegisteredReaction(subspaceID, reactionID, signer)
if err = msg.ValidateBasic(); err != nil {
return fmt.Errorf("message validation failed: %w", err)
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// --------------------------------------------------------------------------------------------------------------------
// GetCmdSetParams returns the command allowing to set reactions parameters
func GetCmdSetParams() *cobra.Command {
cmd := &cobra.Command{
Use: "set-params [subspace-id] [json-file-path]",
Args: cobra.ExactArgs(2),
Short: "Set the reactions params of a subspace",
Long: "Set the reactions params for the given subspace, reading it from the JSON file located at the given path",
Example: fmt.Sprintf(`
%s tx reactions set-params 1 /path/to/file.json \
--from alice
`, version.AppName),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
subspaceID, err := subspacestypes.ParseSubspaceID(args[0])
if err != nil {
return err
}
data, err := cliutils.ParseSetReactionsParamsJSON(clientCtx.Codec, args[1])
if err != nil {
return err
}
user := clientCtx.FromAddress.String()
msg := types.NewMsgSetReactionsParams(subspaceID, data.RegisteredReactionParams, data.FreeTextParams, user)
if err = msg.ValidateBasic(); err != nil {
return fmt.Errorf("message validation failed: %w", err)
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}