-
Notifications
You must be signed in to change notification settings - Fork 48
/
msgs.go
355 lines (285 loc) · 10.6 KB
/
msgs.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
package types
import (
"strings"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
var (
_ sdk.Msg = &MsgAddReaction{}
_ sdk.Msg = &MsgRemoveReaction{}
_ sdk.Msg = &MsgAddRegisteredReaction{}
_ sdk.Msg = &MsgEditRegisteredReaction{}
_ sdk.Msg = &MsgRemoveRegisteredReaction{}
_ sdk.Msg = &MsgSetReactionsParams{}
)
// NewMsgAddReaction returns a new MsgAddReaction instance
func NewMsgAddReaction(subspaceID uint64, postID uint64, value ReactionValue, user string) *MsgAddReaction {
valueAny, err := codectypes.NewAnyWithValue(value)
if err != nil {
panic("failed to pack value to any type")
}
return &MsgAddReaction{
SubspaceID: subspaceID,
PostID: postID,
Value: valueAny,
User: user,
}
}
// Route implements sdk.Msg
func (msg MsgAddReaction) Route() string { return RouterKey }
// Type implements sdk.Msg
func (msg MsgAddReaction) Type() string { return ActionAddReaction }
// ValidateBasic implements sdk.Msg
func (msg MsgAddReaction) ValidateBasic() error {
if msg.SubspaceID == 0 {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid subspace id: %d", msg.SubspaceID)
}
if msg.PostID == 0 {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid post id: %d", msg.PostID)
}
if msg.Value == nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid value: %s", msg.Value)
}
err := msg.Value.GetCachedValue().(ReactionValue).Validate()
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid value: %s", err)
}
_, err = sdk.AccAddressFromBech32(msg.User)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid user address")
}
return nil
}
// GetSignBytes implements sdk.Msg
func (msg MsgAddReaction) GetSignBytes() []byte {
return sdk.MustSortJSON(AminoCdc.MustMarshalJSON(&msg))
}
// GetSigners implements sdk.Msg
func (msg MsgAddReaction) GetSigners() []sdk.AccAddress {
addr, _ := sdk.AccAddressFromBech32(msg.User)
return []sdk.AccAddress{addr}
}
// UnpackInterfaces implements codectypes.UnpackInterfacesMessage
func (msg MsgAddReaction) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
var target ReactionValue
return unpacker.UnpackAny(msg.Value, &target)
}
// --------------------------------------------------------------------------------------------------------------------
// NewMsgRemoveReaction returns a new MsgRemoveReaction instance
func NewMsgRemoveReaction(subspaceID uint64, postID uint64, reactionID uint32, user string) *MsgRemoveReaction {
return &MsgRemoveReaction{
SubspaceID: subspaceID,
PostID: postID,
ReactionID: reactionID,
User: user,
}
}
// Route implements sdk.Msg
func (msg MsgRemoveReaction) Route() string { return RouterKey }
// Type implements sdk.Msg
func (msg MsgRemoveReaction) Type() string { return ActionRemoveReaction }
// ValidateBasic implements sdk.Msg
func (msg MsgRemoveReaction) ValidateBasic() error {
if msg.SubspaceID == 0 {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid subspace id: %d", msg.SubspaceID)
}
if msg.PostID == 0 {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid post id: %d", msg.PostID)
}
if msg.ReactionID == 0 {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid reaction id: %d", msg.ReactionID)
}
_, err := sdk.AccAddressFromBech32(msg.User)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid user address")
}
return nil
}
// GetSignBytes implements sdk.Msg
func (msg MsgRemoveReaction) GetSignBytes() []byte {
return sdk.MustSortJSON(AminoCdc.MustMarshalJSON(&msg))
}
// GetSigners implements sdk.Msg
func (msg MsgRemoveReaction) GetSigners() []sdk.AccAddress {
addr, _ := sdk.AccAddressFromBech32(msg.User)
return []sdk.AccAddress{addr}
}
// --------------------------------------------------------------------------------------------------------------------
// NewMsgAddRegisteredReaction returns a new MsgAddRegisteredReaction instance
func NewMsgAddRegisteredReaction(
subspaceID uint64,
shorthandCode string,
displayValue string,
user string,
) *MsgAddRegisteredReaction {
return &MsgAddRegisteredReaction{
SubspaceID: subspaceID,
ShorthandCode: shorthandCode,
DisplayValue: displayValue,
User: user,
}
}
// Route implements sdk.Msg
func (msg MsgAddRegisteredReaction) Route() string { return RouterKey }
// Type implements sdk.Msg
func (msg MsgAddRegisteredReaction) Type() string { return ActionAddRegisteredReaction }
// ValidateBasic implements sdk.Msg
func (msg MsgAddRegisteredReaction) ValidateBasic() error {
if msg.SubspaceID == 0 {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid subspace id: %d", msg.SubspaceID)
}
if strings.TrimSpace(msg.ShorthandCode) == "" {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid shorthand code: %s", msg.ShorthandCode)
}
if strings.TrimSpace(msg.DisplayValue) == "" {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid display value: %s", msg.DisplayValue)
}
_, err := sdk.AccAddressFromBech32(msg.User)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid user address")
}
return nil
}
// GetSignBytes implements sdk.Msg
func (msg MsgAddRegisteredReaction) GetSignBytes() []byte {
return sdk.MustSortJSON(AminoCdc.MustMarshalJSON(&msg))
}
// GetSigners implements sdk.Msg
func (msg MsgAddRegisteredReaction) GetSigners() []sdk.AccAddress {
addr, _ := sdk.AccAddressFromBech32(msg.User)
return []sdk.AccAddress{addr}
}
// --------------------------------------------------------------------------------------------------------------------
// NewMsgEditRegisteredReaction returns a new MsgEditRegisteredReaction instance
func NewMsgEditRegisteredReaction(
subspaceID uint64,
registeredReactionID uint32,
shorthandCode string,
displayValue string,
user string,
) *MsgEditRegisteredReaction {
return &MsgEditRegisteredReaction{
SubspaceID: subspaceID,
RegisteredReactionID: registeredReactionID,
ShorthandCode: shorthandCode,
DisplayValue: displayValue,
User: user,
}
}
// Route implements sdk.Msg
func (msg MsgEditRegisteredReaction) Route() string { return RouterKey }
// Type implements sdk.Msg
func (msg MsgEditRegisteredReaction) Type() string { return ActionEditRegisteredReaction }
// ValidateBasic implements sdk.Msg
func (msg MsgEditRegisteredReaction) ValidateBasic() error {
if msg.SubspaceID == 0 {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid subspace id: %d", msg.SubspaceID)
}
if msg.RegisteredReactionID == 0 {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid registered reaction id: %d", msg.RegisteredReactionID)
}
if strings.TrimSpace(msg.ShorthandCode) == "" {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid shorthand code: %s", msg.ShorthandCode)
}
if strings.TrimSpace(msg.DisplayValue) == "" {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid display value: %s", msg.DisplayValue)
}
_, err := sdk.AccAddressFromBech32(msg.User)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid user address")
}
return nil
}
// GetSignBytes implements sdk.Msg
func (msg MsgEditRegisteredReaction) GetSignBytes() []byte {
return sdk.MustSortJSON(AminoCdc.MustMarshalJSON(&msg))
}
// GetSigners implements sdk.Msg
func (msg MsgEditRegisteredReaction) GetSigners() []sdk.AccAddress {
addr, _ := sdk.AccAddressFromBech32(msg.User)
return []sdk.AccAddress{addr}
}
// --------------------------------------------------------------------------------------------------------------------
// NewMsgRemoveRegisteredReaction returns a new MsgRemoveRegisteredReaction instance
func NewMsgRemoveRegisteredReaction(
subspaceID uint64,
registeredReactionID uint32,
user string,
) *MsgRemoveRegisteredReaction {
return &MsgRemoveRegisteredReaction{
SubspaceID: subspaceID,
RegisteredReactionID: registeredReactionID,
User: user,
}
}
// Route implements sdk.Msg
func (msg MsgRemoveRegisteredReaction) Route() string { return RouterKey }
// Type implements sdk.Msg
func (msg MsgRemoveRegisteredReaction) Type() string { return ActionRemoveRegisteredReaction }
// ValidateBasic implements sdk.Msg
func (msg MsgRemoveRegisteredReaction) ValidateBasic() error {
if msg.SubspaceID == 0 {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid subspace id: %d", msg.SubspaceID)
}
if msg.RegisteredReactionID == 0 {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid registered reaction id: %d", msg.RegisteredReactionID)
}
_, err := sdk.AccAddressFromBech32(msg.User)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid user address")
}
return nil
}
// GetSignBytes implements sdk.Msg
func (msg MsgRemoveRegisteredReaction) GetSignBytes() []byte {
return sdk.MustSortJSON(AminoCdc.MustMarshalJSON(&msg))
}
// GetSigners implements sdk.Msg
func (msg MsgRemoveRegisteredReaction) GetSigners() []sdk.AccAddress {
addr, _ := sdk.AccAddressFromBech32(msg.User)
return []sdk.AccAddress{addr}
}
// --------------------------------------------------------------------------------------------------------------------
// NewMsgSetReactionsParams returns a new MsgSetReactionsParams instance
func NewMsgSetReactionsParams(
subspaceID uint64,
registeredReaction RegisteredReactionValueParams,
freeText FreeTextValueParams,
user string,
) *MsgSetReactionsParams {
return &MsgSetReactionsParams{
SubspaceID: subspaceID,
RegisteredReaction: registeredReaction,
FreeText: freeText,
User: user,
}
}
// Route implements sdk.Msg
func (msg MsgSetReactionsParams) Route() string { return RouterKey }
// Type implements sdk.Msg
func (msg MsgSetReactionsParams) Type() string { return ActionSetReactionParams }
// ValidateBasic implements sdk.Msg
func (msg MsgSetReactionsParams) ValidateBasic() error {
if msg.SubspaceID == 0 {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid subspace id: %d", msg.SubspaceID)
}
err := msg.FreeText.Validate()
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid free text params: %s", err)
}
_, err = sdk.AccAddressFromBech32(msg.User)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid user address")
}
return nil
}
// GetSignBytes implements sdk.Msg
func (msg MsgSetReactionsParams) GetSignBytes() []byte {
return sdk.MustSortJSON(AminoCdc.MustMarshalJSON(&msg))
}
// GetSigners implements sdk.Msg
func (msg MsgSetReactionsParams) GetSigners() []sdk.AccAddress {
addr, _ := sdk.AccAddressFromBech32(msg.User)
return []sdk.AccAddress{addr}
}