-
Notifications
You must be signed in to change notification settings - Fork 0
/
msgs.go
342 lines (283 loc) · 9.43 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
package types
import (
"fmt"
"net/url"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
)
const (
typeMsgDeposit = "deposit"
typeMsgWithdraw = "withdraw"
typeMsgTransferOwnership = "transferOwnership"
typeMsgUpdateOperator = "updateOperator"
typeMsgCreateOperator = "createOperator"
)
// MsgList - high level transaction of the dex module
type MsgList struct {
Owner sdk.AccAddress `json:"owner"`
ListAsset string `json:"list_asset"` // Symbol of asset listed on Dex.
QuoteAsset string `json:"quote_asset"` // Symbol of asset quoted by asset listed on Dex.
InitPrice sdk.Dec `json:"init_price"`
}
// NewMsgList creates a new MsgList
func NewMsgList(owner sdk.AccAddress, listAsset, quoteAsset string, initPrice sdk.Dec) MsgList {
return MsgList{
Owner: owner,
ListAsset: listAsset,
QuoteAsset: quoteAsset,
InitPrice: initPrice,
}
}
// Route Implements Msg
func (msg MsgList) Route() string { return RouterKey }
// Type Implements Msg
func (msg MsgList) Type() string { return "list" }
// ValidateBasic Implements Msg
func (msg MsgList) ValidateBasic() sdk.Error {
if msg.ListAsset == msg.QuoteAsset {
return ErrInvalidCoins()
}
if !msg.InitPrice.IsPositive() {
return ErrInitPriceIsNotPositive()
}
if msg.Owner.Empty() {
return ErrAddressIsRequired("owner")
}
return nil
}
// GetSignBytes Implements Msg
func (msg MsgList) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}
// GetSigners Implements Msg
func (msg MsgList) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Owner}
}
// MsgDeposit - high level transaction of the dex module
type MsgDeposit struct {
Product string `json:"product"` // product for trading pair in full name of the tokens
Amount sdk.SysCoin `json:"amount"` // Coins to add to the deposit
Depositor sdk.AccAddress `json:"depositor"` // Address of the depositor
}
// NewMsgDeposit creates a new MsgDeposit
func NewMsgDeposit(product string, amount sdk.SysCoin, depositor sdk.AccAddress) MsgDeposit {
return MsgDeposit{product, amount, depositor}
}
// Route Implements Msg
func (msg MsgDeposit) Route() string { return RouterKey }
// Type Implements Msg
func (msg MsgDeposit) Type() string { return typeMsgDeposit }
// ValidateBasic Implements Msg
func (msg MsgDeposit) ValidateBasic() sdk.Error {
if msg.Depositor.Empty() {
return ErrAddressIsRequired("depositor")
}
if !msg.Amount.IsValid() || !msg.Amount.IsPositive() {
return ErrInvalidCoins()
}
return nil
}
// GetSignBytes Implements Msg
func (msg MsgDeposit) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}
// GetSigners Implements Msg
func (msg MsgDeposit) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Depositor}
}
// MsgWithdraw - high level transaction of the dex module
type MsgWithdraw struct {
Product string `json:"product"` // product for trading pair in full name of the tokens
Amount sdk.SysCoin `json:"amount"` // Coins to add to the deposit
Depositor sdk.AccAddress `json:"depositor"` // Address of the depositor
}
// NewMsgWithdraw creates a new MsgWithdraw
func NewMsgWithdraw(product string, amount sdk.SysCoin, depositor sdk.AccAddress) MsgWithdraw {
return MsgWithdraw{product, amount, depositor}
}
// Route Implements Msg
func (msg MsgWithdraw) Route() string { return RouterKey }
// Type Implements Msg
func (msg MsgWithdraw) Type() string { return typeMsgWithdraw }
// ValidateBasic Implements Msg
func (msg MsgWithdraw) ValidateBasic() sdk.Error {
if msg.Depositor.Empty() {
return ErrAddressIsRequired("depositor")
}
if !msg.Amount.IsValid() || !msg.Amount.IsPositive() {
return ErrInvalidCoins()
}
return nil
}
// GetSignBytes Implements Msg
func (msg MsgWithdraw) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}
// GetSigners Implements Msg
func (msg MsgWithdraw) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Depositor}
}
// MsgTransferOwnership - high level transaction of the dex module
type MsgTransferOwnership struct {
FromAddress sdk.AccAddress `json:"from_address"`
ToAddress sdk.AccAddress `json:"to_address"`
Product string `json:"product"`
//ToSignature auth.StdSignature `json:"to_signature"`
}
// NewMsgTransferOwnership create a new MsgTransferOwnership
func NewMsgTransferOwnership(from, to sdk.AccAddress, product string) MsgTransferOwnership {
return MsgTransferOwnership{
FromAddress: from,
ToAddress: to,
Product: product,
}
}
// Route Implements Msg
func (msg MsgTransferOwnership) Route() string { return RouterKey }
// Type Implements Msg
func (msg MsgTransferOwnership) Type() string { return typeMsgTransferOwnership }
// ValidateBasic Implements Msg
func (msg MsgTransferOwnership) ValidateBasic() sdk.Error {
if msg.FromAddress.Empty() {
return ErrAddressIsRequired("sender")
}
if msg.ToAddress.Empty() {
return ErrAddressIsRequired("recipient")
}
if msg.Product == "" {
return ErrTokenPairIsRequired()
}
return nil
}
// GetSignBytes Implements Msg
func (msg MsgTransferOwnership) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}
// GetSigners Implements Msg
func (msg MsgTransferOwnership) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.FromAddress}
}
//MsgConfirmOwnership - high level transaction of the coin module
type MsgConfirmOwnership struct {
Product string `json:"product"`
Address sdk.AccAddress `json:"new_owner"`
}
func NewMsgConfirmOwnership(newOwner sdk.AccAddress, product string) MsgConfirmOwnership {
return MsgConfirmOwnership{
Product: product,
Address: newOwner,
}
}
func (msg MsgConfirmOwnership) Route() string { return RouterKey }
func (msg MsgConfirmOwnership) Type() string { return "confirm" }
func (msg MsgConfirmOwnership) ValidateBasic() sdk.Error {
if msg.Address.Empty() {
return ErrAddressIsRequired("sender")
}
if len(msg.Product) == 0 {
return ErrTokenPairIsRequired()
}
return nil
}
func (msg MsgConfirmOwnership) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}
func (msg MsgConfirmOwnership) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Address}
}
// MsgCreateOperator register a new DEXOperator or update it
// Addr represent an DEXOperator
// if DEXOperator not exist, register a new DEXOperator
// else update Website or HandlingFeeAddress
type MsgCreateOperator struct {
Owner sdk.AccAddress `json:"owner"`
Website string `json:"website"`
HandlingFeeAddress sdk.AccAddress `json:"handling_fee_address"`
}
// NewMsgCreateOperator creates a new MsgCreateOperator
func NewMsgCreateOperator(website string, owner, handlingFeeAddress sdk.AccAddress) MsgCreateOperator {
if handlingFeeAddress.Empty() {
handlingFeeAddress = owner
}
return MsgCreateOperator{owner, strings.TrimSpace(website), handlingFeeAddress}
}
// Route Implements Msg
func (msg MsgCreateOperator) Route() string { return RouterKey }
// Type Implements Msg
func (msg MsgCreateOperator) Type() string { return typeMsgCreateOperator }
// ValidateBasic Implements Msg
func (msg MsgCreateOperator) ValidateBasic() sdk.Error {
if msg.Owner.Empty() {
return ErrAddressIsRequired("owner")
}
if msg.HandlingFeeAddress.Empty() {
return ErrAddressIsRequired("handling fee")
}
return checkWebsite(msg.Website)
}
// GetSignBytes Implements Msg
func (msg MsgCreateOperator) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}
// GetSigners Implements Msg
func (msg MsgCreateOperator) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Owner}
}
// MsgUpdateOperator register a new DEXOperator or update it
// Addr represent an DEXOperator
// if DEXOperator not exist, register a new DEXOperator
// else update Website or HandlingFeeAddress
type MsgUpdateOperator struct {
Owner sdk.AccAddress `json:"owner"`
Website string `json:"website"`
HandlingFeeAddress sdk.AccAddress `json:"handling_fee_address"`
}
// NewMsgUpdateOperator creates a new MsgUpdateOperator
func NewMsgUpdateOperator(website string, owner, handlingFeeAddress sdk.AccAddress) MsgUpdateOperator {
if handlingFeeAddress.Empty() {
handlingFeeAddress = owner
}
return MsgUpdateOperator{owner, strings.TrimSpace(website), handlingFeeAddress}
}
// Route Implements Msg
func (msg MsgUpdateOperator) Route() string { return RouterKey }
// Type Implements Msg
func (msg MsgUpdateOperator) Type() string { return typeMsgUpdateOperator }
// ValidateBasic Implements Msg
func (msg MsgUpdateOperator) ValidateBasic() sdk.Error {
if msg.HandlingFeeAddress.Empty() {
return ErrAddressIsRequired("handling fee")
}
return checkWebsite(msg.Website)
}
// GetSignBytes Implements Msg
func (msg MsgUpdateOperator) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}
// GetSigners Implements Msg
func (msg MsgUpdateOperator) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Owner}
}
func checkWebsite(website string) sdk.Error {
if len(website) == 0 {
return nil
}
if len(website) > 1024 {
return ErrInvalidWebsiteLength(len(website), 1024)
}
u, err := url.Parse(website)
if err != nil {
return ErrInvalidWebsiteURL(err.Error())
}
if u.Scheme != "http" && u.Scheme != "https" {
return ErrInvalidWebsiteURL(fmt.Sprintf("got: %s, expected: http or https", u.Scheme))
}
return nil
}