-
Notifications
You must be signed in to change notification settings - Fork 28
/
msgs.go
349 lines (294 loc) · 9.22 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
package types
import (
"strings"
"unicode/utf8"
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
const (
TypeMsgCreateDenom = "create_denom"
TypeMsgUpdateDenom = "update_denom"
TypeMsgTransferDenom = "transfer_denom"
TypeMsgMintONFT = "mint_onft"
TypeMsgTransferONFT = "transfer_onft"
TypeMsgBurnONFT = "burn_onft"
)
var (
_ sdk.Msg = &MsgUpdateParams{}
_ sdk.Msg = &MsgCreateDenom{}
_ sdk.Msg = &MsgUpdateDenom{}
_ sdk.Msg = &MsgTransferDenom{}
_ sdk.Msg = &MsgMintONFT{}
_ sdk.Msg = &MsgTransferONFT{}
_ sdk.Msg = &MsgBurnONFT{}
)
func NewMsgCreateDenom(
symbol,
name, schema, description, uri, uriHash, previewUri, data, sender string,
creationFee sdk.Coin,
) *MsgCreateDenom {
return &MsgCreateDenom{
Sender: sender,
Id: GenUniqueID(DenomPrefix),
Symbol: symbol,
Name: name,
Schema: schema,
Description: description,
PreviewURI: previewUri,
Uri: uri,
UriHash: uriHash,
Data: data,
CreationFee: creationFee,
}
}
func (msg MsgCreateDenom) Route() string { return RouterKey }
func (msg MsgCreateDenom) Type() string { return TypeMsgCreateDenom }
func (msg MsgCreateDenom) ValidateBasic() error {
if err := ValidateDenomID(msg.Id); err != nil {
return err
}
if err := ValidateDenomSymbol(msg.Symbol); err != nil {
return err
}
if len(msg.Name) > 0 && !utf8.ValidString(msg.Name) {
return errorsmod.Wrap(ErrInvalidName, "denom name is invalid")
}
if err := ValidateName(msg.Name); err != nil {
return err
}
if len(msg.Description) > 0 && !utf8.ValidString(msg.Description) {
return errorsmod.Wrap(ErrInvalidDescription, "denom description is invalid")
}
if err := ValidateDescription(msg.Description); err != nil {
return err
}
if err := ValidateURI(msg.PreviewURI); err != nil {
return err
}
if err := ValidateCreationFee(msg.CreationFee); err != nil {
return err
}
if _, err := sdk.AccAddressFromBech32(msg.Sender); err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid sender address (%s)", err)
}
return nil
}
// GetSignBytes Implements Msg.
func (msg MsgCreateDenom) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(&msg)
return sdk.MustSortJSON(bz)
}
func (msg MsgCreateDenom) GetSigners() []sdk.AccAddress {
from, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
panic(err)
}
return []sdk.AccAddress{from}
}
func NewMsgUpdateDenom(id, name, description, previewUri, sender string) *MsgUpdateDenom {
return &MsgUpdateDenom{
Id: id,
Name: name,
Description: description,
PreviewURI: previewUri,
Sender: sender,
}
}
func (msg MsgUpdateDenom) Route() string { return RouterKey }
func (msg MsgUpdateDenom) Type() string { return TypeMsgUpdateDenom }
func (msg MsgUpdateDenom) ValidateBasic() error {
if err := ValidateDenomID(msg.Id); err != nil {
return err
}
if len(msg.Name) > 0 && !utf8.ValidString(msg.Name) {
return errorsmod.Wrap(ErrInvalidName, "denom name is invalid")
}
if err := ValidateName(msg.Name); err != nil {
return err
}
if len(msg.Description) > 0 && !utf8.ValidString(msg.Description) {
return errorsmod.Wrap(ErrInvalidDescription, "denom description is invalid")
}
if err := ValidateDescription(msg.Description); err != nil {
return err
}
if err := ValidateURI(msg.PreviewURI); err != nil {
return err
}
if _, err := sdk.AccAddressFromBech32(msg.Sender); err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid sender address (%s)", err)
}
return nil
}
// GetSignBytes Implements Msg.
func (msg MsgUpdateDenom) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(&msg)
return sdk.MustSortJSON(bz)
}
func (msg MsgUpdateDenom) GetSigners() []sdk.AccAddress {
from, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
panic(err)
}
return []sdk.AccAddress{from}
}
func NewMsgTransferDenom(id, sender, recipient string) *MsgTransferDenom {
return &MsgTransferDenom{
Id: id,
Sender: sender,
Recipient: recipient,
}
}
func (msg MsgTransferDenom) Route() string { return RouterKey }
func (msg MsgTransferDenom) Type() string { return TypeMsgTransferDenom }
func (msg MsgTransferDenom) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(msg.Sender); err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid sender address; %s", err)
}
if _, err := sdk.AccAddressFromBech32(msg.Recipient); err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid recipient address; %s", err)
}
return nil
}
func (msg MsgTransferDenom) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(&msg)
return sdk.MustSortJSON(bz)
}
func (msg MsgTransferDenom) GetSigners() []sdk.AccAddress {
from, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
panic(err)
}
return []sdk.AccAddress{from}
}
func NewMsgMintONFT(
denomId, sender, recipient string, metadata Metadata, data string,
transferable, extensible, nsfw bool, royaltyShare sdk.Dec,
) *MsgMintONFT {
return &MsgMintONFT{
Id: GenUniqueID(IDPrefix),
DenomId: denomId,
Metadata: metadata,
Data: data,
Transferable: transferable,
Extensible: extensible,
Nsfw: nsfw,
RoyaltyShare: royaltyShare,
Sender: sender,
Recipient: recipient,
}
}
func (msg MsgMintONFT) Route() string { return RouterKey }
func (msg MsgMintONFT) Type() string { return TypeMsgMintONFT }
func (msg MsgMintONFT) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(msg.Sender); err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid sender address; %s", err)
}
if _, err := sdk.AccAddressFromBech32(msg.Recipient); err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid recipient address; %s", err)
}
if err := ValidateName(msg.Metadata.Name); err != nil {
return err
}
if err := ValidateDescription(msg.Metadata.Description); err != nil {
return err
}
if err := ValidateMediaURI(msg.Metadata.MediaURI); err != nil {
return err
}
if err := ValidateURI(msg.Metadata.PreviewURI); err != nil {
return err
}
if msg.RoyaltyShare.IsNegative() || msg.RoyaltyShare.GTE(sdk.NewDec(1)) {
return errorsmod.Wrapf(ErrInvalidPercentage, "invalid royalty share percentage decimal value; %d, must be positive and less than 1", msg.RoyaltyShare)
}
return ValidateONFTID(msg.Id)
}
func (msg MsgMintONFT) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(&msg)
return sdk.MustSortJSON(bz)
}
func (msg MsgMintONFT) GetSigners() []sdk.AccAddress {
from, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
panic(err)
}
return []sdk.AccAddress{from}
}
func NewMsgTransferONFT(id, denomId, sender, recipient string) *MsgTransferONFT {
return &MsgTransferONFT{
Id: strings.ToLower(strings.TrimSpace(id)),
DenomId: strings.TrimSpace(denomId),
Sender: sender,
Recipient: recipient,
}
}
func (msg MsgTransferONFT) Route() string { return RouterKey }
func (msg MsgTransferONFT) Type() string { return TypeMsgTransferONFT }
func (msg MsgTransferONFT) ValidateBasic() error {
if err := ValidateDenomID(msg.DenomId); err != nil {
return err
}
if _, err := sdk.AccAddressFromBech32(msg.Sender); err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid sender address; %s", err)
}
if _, err := sdk.AccAddressFromBech32(msg.Recipient); err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid recipient address; %s", err)
}
return ValidateONFTID(msg.Id)
}
func (msg MsgTransferONFT) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(&msg)
return sdk.MustSortJSON(bz)
}
func (msg MsgTransferONFT) GetSigners() []sdk.AccAddress {
from, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
panic(err)
}
return []sdk.AccAddress{from}
}
func NewMsgBurnONFT(denomId, id, sender string) *MsgBurnONFT {
return &MsgBurnONFT{
DenomId: denomId,
Id: id,
Sender: sender,
}
}
func (msg MsgBurnONFT) Route() string { return RouterKey }
func (msg MsgBurnONFT) Type() string { return TypeMsgBurnONFT }
func (msg MsgBurnONFT) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(msg.Sender); err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid sender address; %s", err)
}
return ValidateONFTID(msg.Id)
}
func (msg MsgBurnONFT) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(&msg)
return sdk.MustSortJSON(bz)
}
func (msg MsgBurnONFT) GetSigners() []sdk.AccAddress {
from, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
panic(err)
}
return []sdk.AccAddress{from}
}
// MsgUpdateParams
// GetSignBytes implements the LegacyMsg interface.
func (m MsgUpdateParams) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&m))
}
// GetSigners returns the expected signers for a MsgUpdateParams message.
func (m *MsgUpdateParams) GetSigners() []sdk.AccAddress {
addr, _ := sdk.AccAddressFromBech32(m.Authority)
return []sdk.AccAddress{addr}
}
// ValidateBasic does a sanity check on the provided data.
func (m *MsgUpdateParams) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(m.Authority); err != nil {
return errorsmod.Wrap(err, "invalid authority address")
}
return m.Params.ValidateBasic()
}