-
Notifications
You must be signed in to change notification settings - Fork 45
/
tx.go
551 lines (449 loc) · 15.1 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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
package cli
// DONTCOVER
import (
"fmt"
"strconv"
"strings"
"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"
cliutils "github.com/desmos-labs/desmos/v7/x/posts/client/utils"
"github.com/desmos-labs/desmos/v7/x/posts/types"
subspacestypes "github.com/desmos-labs/desmos/v7/x/subspaces/types"
)
// NewTxCmd returns a new command to perform subspaces transactions
func NewTxCmd() *cobra.Command {
subspacesTxCmd := &cobra.Command{
Use: types.ModuleName,
Short: "Posts transaction subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
subspacesTxCmd.AddCommand(
GetCmdCreatePost(),
GetCmdEditPost(),
GetCmdDeletePost(),
GetCmdAddPostAttachment(),
GetCmdRemovePostAttachment(),
GetCmdAnswerPoll(),
GetCmdMovePost(),
GetCmdRequestPostOwnerTransfer(),
GetCmdCancelPostOwnerTransfer(),
GetCmdAcceptPostOwnerTransfer(),
GetCmdRefusePostOwnerTransfer(),
)
return subspacesTxCmd
}
// GetCmdCreatePost returns the command allowing to create a new post
func GetCmdCreatePost() *cobra.Command {
cmd := &cobra.Command{
Use: "create [subspace-id] [section-id] [json-file-path]",
Args: cobra.ExactArgs(3),
Short: "Create a new post",
Long: `Create a new post containing the data specified inside the JSON file located at the provided path.`,
Example: fmt.Sprintf(`%s tx posts create 1 1 /path/to/my/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
}
sectionID, err := subspacestypes.ParseSectionID(args[1])
if err != nil {
return err
}
data, err := cliutils.ParseCreatePostJSON(clientCtx.Codec, args[2])
if err != nil {
return err
}
author := clientCtx.FromAddress.String()
attachments, err := types.UnpackAttachments(clientCtx.Codec, data.Attachments)
if err != nil {
return err
}
msg := types.NewMsgCreatePost(
subspaceID,
sectionID,
data.ExternalID,
data.Text,
data.ConversationID,
data.ReplySettings,
data.Entities,
data.Tags,
attachments,
data.ReferencedPosts,
author,
)
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
}
// GetCmdEditPost returns the command allowing to edit an existing post
func GetCmdEditPost() *cobra.Command {
cmd := &cobra.Command{
Use: "edit [subspace-id] [post-id] [json-file-path]",
Args: cobra.ExactArgs(3),
Short: "Edit an existing post",
Long: `Edit an existing post by using the data specified inside the JSON file located at the provided path.`,
Example: fmt.Sprintf(`%s tx posts edit 1 1 /path/to/my/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
}
postID, err := types.ParsePostID(args[1])
if err != nil {
return err
}
data, err := cliutils.ParseEditPostJSON(clientCtx.Codec, args[2])
if err != nil {
return err
}
editor := clientCtx.FromAddress.String()
msg := types.NewMsgEditPost(
subspaceID,
postID,
data.Text,
data.Entities,
data.Tags,
editor,
)
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
}
// GetCmdDeletePost returns the command allowing to delete an existing post
func GetCmdDeletePost() *cobra.Command {
cmd := &cobra.Command{
Use: "delete [subspace-id] [post-id]",
Args: cobra.ExactArgs(2),
Short: "Delete an existing post",
Example: fmt.Sprintf(`%s tx posts delete 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 := types.ParsePostID(args[1])
if err != nil {
return err
}
signer := clientCtx.FromAddress.String()
msg := types.NewMsgDeletePost(subspaceID, postID, 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
}
// GetCmdAddPostAttachment returns the command allowing to add an attachment to an existing post
func GetCmdAddPostAttachment() *cobra.Command {
cmd := &cobra.Command{
Use: "add-attachment [subspace-id] [post-id] [json-file-path]",
Args: cobra.ExactArgs(3),
Short: "Add an attachment to an existing post",
Long: `Add an attachment to an existing post by using the data specified inside the JSON file located at the provided path.`,
Example: fmt.Sprintf(`%s tx posts add-attachment 1 1 /path/to/my/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
}
postID, err := types.ParsePostID(args[1])
if err != nil {
return err
}
content, err := cliutils.ParseAttachmentContent(clientCtx.Codec, args[2])
if err != nil {
return err
}
editor := clientCtx.FromAddress.String()
msg := types.NewMsgAddPostAttachment(subspaceID, postID, content, editor)
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
}
// GetCmdRemovePostAttachment returns the command allowing to delete an existing post attachment
func GetCmdRemovePostAttachment() *cobra.Command {
cmd := &cobra.Command{
Use: "remove-attachment [subspace-id] [post-id] [attachment-id]",
Args: cobra.ExactArgs(3),
Short: "Remove an existing post attachment",
Example: fmt.Sprintf(`%s tx posts remove-attachment 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 := types.ParsePostID(args[1])
if err != nil {
return err
}
attachmentID, err := types.ParseAttachmentID(args[2])
if err != nil {
return err
}
signer := clientCtx.FromAddress.String()
msg := types.NewMsgRemovePostAttachment(subspaceID, postID, attachmentID, 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
}
// GetCmdAnswerPoll returns the command allowing to answer an existing poll
func GetCmdAnswerPoll() *cobra.Command {
cmd := &cobra.Command{
Use: "answer-poll [subspace-id] [post-id] [poll-id] [answers-indexes]",
Args: cobra.ExactArgs(4),
Short: "Answer an existing poll",
Long: `Answer an existing poll with the provided answers index.
If you want to specify multiple answers, separate them using a comma.
Answer indexes must be the indexes that each of your answer has inside the poll's provided answer array.
E.g. Suppose you have a poll with the following provided answer array:
- Question: What animal is the best?
Provided answers:
- Cat
- Dog
Then, the "Cat" answer has index 0 and and the "Dog" answer has index 1.
`,
Example: fmt.Sprintf(`%s tx posts answer-poll 1 1 1 0,1,2 --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 := types.ParsePostID(args[1])
if err != nil {
return err
}
pollID, err := types.ParseAttachmentID(args[2])
if err != nil {
return err
}
indexes := strings.Split(args[3], ",")
answers := make([]uint32, len(indexes))
for i, answer := range indexes {
answerIndex, err := strconv.ParseUint(answer, 10, 32)
if err != nil {
return err
}
answers[i] = uint32(answerIndex)
}
signer := clientCtx.FromAddress.String()
msg := types.NewMsgAnswerPoll(subspaceID, postID, pollID, answers, 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
}
// GetCmdMovePost returns the command allowing to move an existing post to another subspace
func GetCmdMovePost() *cobra.Command {
cmd := &cobra.Command{
Use: "move-post [subspace-id] [post-id] [target-subspace-id] [target-section-id]",
Args: cobra.ExactArgs(4),
Short: "Move an existing post to another subspace",
Example: fmt.Sprintf(`%s tx posts move-post 1 1 2 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 := types.ParsePostID(args[1])
if err != nil {
return err
}
targetSubspaceID, err := subspacestypes.ParseSubspaceID(args[2])
if err != nil {
return err
}
targetSectionID, err := subspacestypes.ParseSectionID(args[3])
if err != nil {
return err
}
signer := clientCtx.FromAddress.String()
msg := types.NewMsgMovePost(subspaceID, postID, targetSubspaceID, targetSectionID, 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
}
// GetCmdRequestPostOwnerTransfer returns the command to create a post owner transfer request
func GetCmdRequestPostOwnerTransfer() *cobra.Command {
cmd := &cobra.Command{
Use: "request-post-owner-transfer [subspace-id] [post-id] [receiver]",
Short: "Make a request to transfer the owner of the post to the receiver",
Example: fmt.Sprintf(`%s tx posts request-post-owner-transfer 1 1 desmos13p5pamrljhza3fp4es5m3llgmnde5fzcpq6nud`, version.AppName),
Args: cobra.ExactArgs(3),
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 := types.ParsePostID(args[1])
if err != nil {
return err
}
msg := types.NewMsgRequestPostOwnerTransfer(subspaceID, postID, args[2], clientCtx.FromAddress.String())
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
}
// GetCmdCancelPostOwnerTransfer returns the command to cancel an outgoing post owner transfer request
func GetCmdCancelPostOwnerTransfer() *cobra.Command {
cmd := &cobra.Command{
Use: "cancel-post-owner-transfer-request [subspace-id] [post-id]",
Short: "Cancel a post owner transfer request with the given post id",
Example: fmt.Sprintf(`%s tx posts cancel-dtag-transfer-request 1, 1`, version.AppName),
Args: cobra.ExactArgs(2),
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 := types.ParsePostID(args[1])
if err != nil {
return err
}
msg := types.NewMsgCancelPostOwnerTransferRequest(subspaceID, postID, clientCtx.FromAddress.String())
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
}
// GetCmdAcceptPostOwnerTransfer returns the command to accept a post owner transfer request
func GetCmdAcceptPostOwnerTransfer() *cobra.Command {
cmd := &cobra.Command{
Use: "accept-post-owner-transfer-request [subspace-id] [post-id]",
Short: `Accept a post owner transfer request with the given post id`,
Example: fmt.Sprintf(`%s tx posts accept-dtag-transfer-request 1 1`, version.AppName),
Args: cobra.ExactArgs(2),
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 := types.ParsePostID(args[1])
if err != nil {
return err
}
msg := types.NewMsgAcceptPostOwnerTransferRequest(subspaceID, postID, clientCtx.FromAddress.String())
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
}
// GetCmdRefusePostOwnerTransfer returns the command to refuse an incoming post owner transfer request
func GetCmdRefusePostOwnerTransfer() *cobra.Command {
cmd := &cobra.Command{
Use: "refuse-post-owner-transfer-request [subspace-id] [post-id]",
Short: "Refuse a post owner transfer request with the given post id",
Example: fmt.Sprintf(`%s tx posts refuse-dtag-transfer-request 1 1`, version.AppName),
Args: cobra.ExactArgs(2),
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 := types.ParsePostID(args[1])
if err != nil {
return err
}
msg := types.NewMsgRefusePostOwnerTransferRequest(subspaceID, postID, clientCtx.FromAddress.String())
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
}