-
Notifications
You must be signed in to change notification settings - Fork 46
/
msgs.go
281 lines (226 loc) · 7.72 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
package types
import (
"fmt"
"strings"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
var (
_ sdk.Msg = &MsgCreateReport{}
_ sdk.Msg = &MsgDeleteReport{}
_ sdk.Msg = &MsgSupportStandardReason{}
_ sdk.Msg = &MsgAddReason{}
_ sdk.Msg = &MsgRemoveReason{}
)
// NewMsgCreateReport returns a new MsgCreateReport instance
func NewMsgCreateReport(
subspaceID uint64,
reasonsIDs []uint32,
message string,
target ReportTarget,
reporter string,
) *MsgCreateReport {
targetAny, err := codectypes.NewAnyWithValue(target)
if err != nil {
panic("failed to pack target to any type")
}
return &MsgCreateReport{
SubspaceID: subspaceID,
ReasonsIDs: reasonsIDs,
Message: message,
Reporter: reporter,
Target: targetAny,
}
}
// UnpackInterfaces implements codectypes.UnpackInterfacesMessage
func (msg *MsgCreateReport) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
var target ReportTarget
return unpacker.UnpackAny(msg.Target, &target)
}
// Route should return the name of the module
func (msg MsgCreateReport) Route() string { return RouterKey }
// Type should return the action
func (msg MsgCreateReport) Type() string {
return ActionCreateReport
}
// ValidateBasic runs stateless checks on the message
func (msg MsgCreateReport) ValidateBasic() error {
if msg.SubspaceID == 0 {
return fmt.Errorf("invalid subspace id: %d", msg.SubspaceID)
}
if len(msg.ReasonsIDs) == 0 {
return fmt.Errorf("at least one reporting reason is required")
}
for _, reasonID := range msg.ReasonsIDs {
if reasonID == 0 {
return fmt.Errorf("invalid reason id: %d", reasonID)
}
}
_, err := sdk.AccAddressFromBech32(msg.Reporter)
if err != nil {
return fmt.Errorf("invalid reporter address: %s", err)
}
return msg.Target.GetCachedValue().(ReportTarget).Validate()
}
// GetSignBytes encodes the message for signing
func (msg MsgCreateReport) GetSignBytes() []byte {
return sdk.MustSortJSON(AminoCdc.MustMarshalJSON(&msg))
}
// GetSigners defines whose signature is required
func (msg MsgCreateReport) GetSigners() []sdk.AccAddress {
sender, _ := sdk.AccAddressFromBech32(msg.Reporter)
return []sdk.AccAddress{sender}
}
// --------------------------------------------------------------------------------------------------------------------
// NewMsgDeleteReport returns a new MsgDeleteReport instance
func NewMsgDeleteReport(subspaceID uint64, reportID uint64, signer string) *MsgDeleteReport {
return &MsgDeleteReport{
SubspaceID: subspaceID,
ReportID: reportID,
Signer: signer,
}
}
// Route should return the name of the module
func (msg MsgDeleteReport) Route() string { return RouterKey }
// Type should return the action
func (msg MsgDeleteReport) Type() string {
return ActionDeleteReport
}
// ValidateBasic runs stateless checks on the message
func (msg MsgDeleteReport) ValidateBasic() error {
if msg.SubspaceID == 0 {
return fmt.Errorf("invalid subspace id: %d", msg.SubspaceID)
}
if msg.ReportID == 0 {
return fmt.Errorf("invalid report id: %d", msg.ReportID)
}
_, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
return fmt.Errorf("invalid signer address: %s", err)
}
return nil
}
// GetSignBytes encodes the message for signing
func (msg MsgDeleteReport) GetSignBytes() []byte {
return sdk.MustSortJSON(AminoCdc.MustMarshalJSON(&msg))
}
// GetSigners defines whose signature is required
func (msg MsgDeleteReport) GetSigners() []sdk.AccAddress {
sender, _ := sdk.AccAddressFromBech32(msg.Signer)
return []sdk.AccAddress{sender}
}
// --------------------------------------------------------------------------------------------------------------------
// NewMsgSupportStandardReason returns a new MsgSupportStandardReason instance
func NewMsgSupportStandardReason(subspaceID uint64, standardReasonID uint32, signer string) *MsgSupportStandardReason {
return &MsgSupportStandardReason{
SubspaceID: subspaceID,
StandardReasonID: standardReasonID,
Signer: signer,
}
}
// Route should return the name of the module
func (msg MsgSupportStandardReason) Route() string { return RouterKey }
// Type should return the action
func (msg MsgSupportStandardReason) Type() string {
return ActionSupportStandardReason
}
// ValidateBasic runs stateless checks on the message
func (msg MsgSupportStandardReason) ValidateBasic() error {
if msg.SubspaceID == 0 {
return fmt.Errorf("invalid subspace id: %d", msg.SubspaceID)
}
if msg.StandardReasonID == 0 {
return fmt.Errorf("invalid standard reason id: %d", msg.StandardReasonID)
}
_, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
return fmt.Errorf("invalid signer address: %s", err)
}
return nil
}
// GetSignBytes encodes the message for signing
func (msg MsgSupportStandardReason) GetSignBytes() []byte {
return sdk.MustSortJSON(AminoCdc.MustMarshalJSON(&msg))
}
// GetSigners defines whose signature is required
func (msg MsgSupportStandardReason) GetSigners() []sdk.AccAddress {
sender, _ := sdk.AccAddressFromBech32(msg.Signer)
return []sdk.AccAddress{sender}
}
// --------------------------------------------------------------------------------------------------------------------
// NewMsgAddReason returns a new MsgAddReason instance
func NewMsgAddReason(subspaceID uint64, title string, description string, signer string) *MsgAddReason {
return &MsgAddReason{
SubspaceID: subspaceID,
Title: title,
Description: description,
Signer: signer,
}
}
// Route should return the name of the module
func (msg MsgAddReason) Route() string { return RouterKey }
// Type should return the action
func (msg MsgAddReason) Type() string {
return ActionAddReason
}
// ValidateBasic runs stateless checks on the message
func (msg MsgAddReason) ValidateBasic() error {
if msg.SubspaceID == 0 {
return fmt.Errorf("invalid subspace id: %d", msg.SubspaceID)
}
if strings.TrimSpace(msg.Title) == "" {
return fmt.Errorf("invalid reason title: %s", msg.Title)
}
_, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
return fmt.Errorf("invalid signer address: %s", err)
}
return nil
}
// GetSignBytes encodes the message for signing
func (msg MsgAddReason) GetSignBytes() []byte {
return sdk.MustSortJSON(AminoCdc.MustMarshalJSON(&msg))
}
// GetSigners defines whose signature is required
func (msg MsgAddReason) GetSigners() []sdk.AccAddress {
sender, _ := sdk.AccAddressFromBech32(msg.Signer)
return []sdk.AccAddress{sender}
}
// --------------------------------------------------------------------------------------------------------------------
// NewMsgRemoveReason returns a new MsgRemoveReason instance
func NewMsgRemoveReason(subspaceID uint64, reasonID uint32, signer string) *MsgRemoveReason {
return &MsgRemoveReason{
SubspaceID: subspaceID,
ReasonID: reasonID,
Signer: signer,
}
}
// Route should return the name of the module
func (msg MsgRemoveReason) Route() string { return RouterKey }
// Type should return the action
func (msg MsgRemoveReason) Type() string {
return ActionRemoveReason
}
// ValidateBasic runs stateless checks on the message
func (msg MsgRemoveReason) ValidateBasic() error {
if msg.SubspaceID == 0 {
return fmt.Errorf("invalid subspace id: %d", msg.SubspaceID)
}
if msg.ReasonID == 0 {
return fmt.Errorf("invalid reason id: %d", msg.ReasonID)
}
_, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
return fmt.Errorf("invalid signer address: %s", err)
}
return nil
}
// GetSignBytes encodes the message for signing
func (msg MsgRemoveReason) GetSignBytes() []byte {
return sdk.MustSortJSON(AminoCdc.MustMarshalJSON(&msg))
}
// GetSigners defines whose signature is required
func (msg MsgRemoveReason) GetSigners() []sdk.AccAddress {
sender, _ := sdk.AccAddressFromBech32(msg.Signer)
return []sdk.AccAddress{sender}
}