forked from cosmos/sdk-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 2
/
msg.go
159 lines (129 loc) · 4.05 KB
/
msg.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
package types
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
// const RouterKey = ModuleName // this was defined in your key.go file
////////////////////////////////////////
// MSG SET NAME FUNCTIONALITY BELOW
////////////////////////////////////////
// MsgSetName defines a SetName message
type MsgSetName struct {
Name string `json:"name"`
Value string `json:"value"`
Owner sdk.AccAddress `json:"owner"`
}
// NewMsgSetName is a constructor function for MsgSetName
func NewMsgSetName(name string, value string, owner sdk.AccAddress) MsgSetName {
return MsgSetName{
Name: name,
Value: value,
Owner: owner,
}
}
// Route should return the name of the module
func (msg MsgSetName) Route() string {
return RouterKey
}
// Type should return the action
func (msg MsgSetName) Type() string {
return "set_name"
}
// ValidateBasic runs stateless checks on the message
func (msg MsgSetName) ValidateBasic() error {
if msg.Owner.Empty() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Owner.String())
}
if len(msg.Name) == 0 || len(msg.Value) == 0 {
return sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "Name and/or Value cannot be empty")
}
return nil
}
// GetSignBytes encodes the message for signing
func (msg MsgSetName) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg))
}
// GetSigners defines whose signature is required
func (msg MsgSetName) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Owner}
}
////////////////////////////////////////
// MSG BUY NAME FUNCTIONALITY BELOW
////////////////////////////////////////
// MsgBuyName defines the BuyName message
type MsgBuyName struct {
Name string `json:"name"`
Bid sdk.Coins `json:"bid"`
Buyer sdk.AccAddress `json:"buyer"`
}
// NewMsgBuyName is the constructor function for MsgBuyName
func NewMsgBuyName(name string, bid sdk.Coins, buyer sdk.AccAddress) MsgBuyName {
return MsgBuyName{
Name: name,
Bid: bid,
Buyer: buyer,
}
}
// Route should return the name of the module
func (msg MsgBuyName) Route() string {
return RouterKey
}
// Type should return the action
func (msg MsgBuyName) Type() string {
return "buy_name"
}
// ValidateBasic runs stateless checks on the message
func (msg MsgBuyName) ValidateBasic() error {
if msg.Buyer.Empty() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Buyer.String())
}
if len(msg.Name) == 0 {
return sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "Name cannot be empty")
}
if !msg.Bid.IsAllPositive() {
return sdkerrors.ErrInsufficientFunds
}
return nil
}
// GetSignBytes encodes the message for signing
func (msg MsgBuyName) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg))
}
// GetSigners defines whose signature is required
func (msg MsgBuyName) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Buyer}
}
// MsgDeleteName defines a DeleteName message
type MsgDeleteName struct {
Name string `json:"name"`
Owner sdk.AccAddress `json:"owner"`
}
// NewMsgDeleteName is a constructor function for MsgDeleteName
func NewMsgDeleteName(name string, owner sdk.AccAddress) MsgDeleteName {
return MsgDeleteName{
Name: name,
Owner: owner,
}
}
// Route should return the name of the module
func (msg MsgDeleteName) Route() string { return RouterKey }
// Type should return the action
func (msg MsgDeleteName) Type() string { return "delete_name" }
// ValidateBasic runs stateless checks on the message
func (msg MsgDeleteName) ValidateBasic() error {
if msg.Owner.Empty() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Owner.String())
}
if len(msg.Name) == 0 {
return sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "Name cannot be empty")
}
return nil
}
// GetSignBytes encodes the message for signing
func (msg MsgDeleteName) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg))
}
// GetSigners defines whose signature is required
func (msg MsgDeleteName) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Owner}
}