-
Notifications
You must be signed in to change notification settings - Fork 44
/
tx.go
349 lines (285 loc) · 9.18 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
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/v5/x/posts/client/utils"
"github.com/desmos-labs/desmos/v5/x/posts/types"
subspacestypes "github.com/desmos-labs/desmos/v5/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(),
)
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
}