forked from Kava-Labs/kava
/
msg.go
340 lines (271 loc) · 11.2 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
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
package types
import (
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
const (
// TypeMsgDeposit represents the type string for MsgDeposit
TypeMsgDeposit = "swap_deposit"
// TypeMsgWithdraw represents the type string for MsgWithdraw
TypeMsgWithdraw = "swap_withdraw"
// TypeSwapExactForTokens represents the type string for MsgSwapExactForTokens
TypeSwapExactForTokens = "swap_exact_for_tokens"
// TypeSwapForExactTokens represents the type string for MsgSwapForExactTokens
TypeSwapForExactTokens = "swap_for_exact_tokens"
)
var (
_ sdk.Msg = &MsgDeposit{}
_ MsgWithDeadline = &MsgDeposit{}
_ sdk.Msg = &MsgWithdraw{}
_ MsgWithDeadline = &MsgWithdraw{}
_ sdk.Msg = &MsgSwapExactForTokens{}
_ MsgWithDeadline = &MsgSwapExactForTokens{}
_ sdk.Msg = &MsgSwapForExactTokens{}
_ MsgWithDeadline = &MsgSwapForExactTokens{}
)
// MsgWithDeadline allows messages to define a deadline of when they are considered invalid
type MsgWithDeadline interface {
GetDeadline() time.Time
DeadlineExceeded(blockTime time.Time) bool
}
// NewMsgDeposit returns a new MsgDeposit
func NewMsgDeposit(depositor string, tokenA sdk.Coin, tokenB sdk.Coin, slippage sdk.Dec, deadline int64) *MsgDeposit {
return &MsgDeposit{
Depositor: depositor,
TokenA: tokenA,
TokenB: tokenB,
Slippage: slippage,
Deadline: deadline,
}
}
// Route return the message type used for routing the message.
func (msg MsgDeposit) Route() string { return RouterKey }
// Type returns a human-readable string for the message, intended for utilization within tags.
func (msg MsgDeposit) Type() string { return TypeMsgDeposit }
// ValidateBasic does a simple validation check that doesn't require access to any other information.
func (msg MsgDeposit) ValidateBasic() error {
if msg.Depositor == "" {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "depositor address cannot be empty")
}
if _, err := sdk.AccAddressFromBech32(msg.Depositor); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid depositor address: %s", err)
}
if !msg.TokenA.IsValid() || msg.TokenA.IsZero() {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidCoins, "token a deposit amount %s", msg.TokenA)
}
if !msg.TokenB.IsValid() || msg.TokenB.IsZero() {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidCoins, "token b deposit amount %s", msg.TokenB)
}
if msg.TokenA.Denom == msg.TokenB.Denom {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, "denominations can not be equal")
}
if msg.Slippage.IsNil() {
return sdkerrors.Wrapf(ErrInvalidSlippage, "slippage must be set")
}
if msg.Slippage.IsNegative() {
return sdkerrors.Wrapf(ErrInvalidSlippage, "slippage can not be negative")
}
if msg.Deadline <= 0 {
return sdkerrors.Wrapf(ErrInvalidDeadline, "deadline %d", msg.Deadline)
}
return nil
}
// GetSignBytes gets the canonical byte representation of the Msg.
func (msg MsgDeposit) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(&msg)
return sdk.MustSortJSON(bz)
}
// GetSigners returns the addresses of signers that must sign.
func (msg MsgDeposit) GetSigners() []sdk.AccAddress {
depositor, _ := sdk.AccAddressFromBech32(msg.Depositor)
return []sdk.AccAddress{depositor}
}
// GetDeadline returns the time at which the msg is considered invalid
func (msg MsgDeposit) GetDeadline() time.Time {
return time.Unix(msg.Deadline, 0)
}
// DeadlineExceeded returns if the msg has exceeded it's deadline
func (msg MsgDeposit) DeadlineExceeded(blockTime time.Time) bool {
return blockTime.Unix() >= msg.Deadline
}
// NewMsgWithdraw returns a new MsgWithdraw
func NewMsgWithdraw(from string, shares sdk.Int, minTokenA, minTokenB sdk.Coin, deadline int64) *MsgWithdraw {
return &MsgWithdraw{
From: from,
Shares: shares,
MinTokenA: minTokenA,
MinTokenB: minTokenB,
Deadline: deadline,
}
}
// Route return the message type used for routing the message.
func (msg MsgWithdraw) Route() string { return RouterKey }
// Type returns a human-readable string for the message, intended for utilization within tags.
func (msg MsgWithdraw) Type() string { return TypeMsgWithdraw }
// ValidateBasic does a simple validation check that doesn't require access to any other information.
func (msg MsgWithdraw) ValidateBasic() error {
if msg.From == "" {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "from address cannot be empty")
}
if _, err := sdk.AccAddressFromBech32(msg.From); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid from address: %s", err)
}
if msg.Shares.IsNil() {
return sdkerrors.Wrapf(ErrInvalidShares, "shares must be set")
}
if msg.Shares.IsZero() || msg.Shares.IsNegative() {
return sdkerrors.Wrapf(ErrInvalidShares, msg.Shares.String())
}
if !msg.MinTokenA.IsValid() || msg.MinTokenA.IsZero() {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidCoins, "min token a amount %s", msg.MinTokenA)
}
if !msg.MinTokenB.IsValid() || msg.MinTokenB.IsZero() {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidCoins, "min token b amount %s", msg.MinTokenB)
}
if msg.MinTokenA.Denom == msg.MinTokenB.Denom {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, "denominations can not be equal")
}
if msg.Deadline <= 0 {
return sdkerrors.Wrapf(ErrInvalidDeadline, "deadline %d", msg.Deadline)
}
return nil
}
// GetSignBytes gets the canonical byte representation of the Msg.
func (msg MsgWithdraw) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(&msg)
return sdk.MustSortJSON(bz)
}
// GetSigners returns the addresses of signers that must sign.
func (msg MsgWithdraw) GetSigners() []sdk.AccAddress {
from, _ := sdk.AccAddressFromBech32(msg.From)
return []sdk.AccAddress{from}
}
// GetDeadline returns the time at which the msg is considered invalid
func (msg MsgWithdraw) GetDeadline() time.Time {
return time.Unix(msg.Deadline, 0)
}
// DeadlineExceeded returns if the msg has exceeded it's deadline
func (msg MsgWithdraw) DeadlineExceeded(blockTime time.Time) bool {
return blockTime.Unix() >= msg.Deadline
}
// NewMsgSwapExactForTokens returns a new MsgSwapExactForTokens
func NewMsgSwapExactForTokens(requester string, exactTokenA sdk.Coin, tokenB sdk.Coin, slippage sdk.Dec, deadline int64) *MsgSwapExactForTokens {
return &MsgSwapExactForTokens{
Requester: requester,
ExactTokenA: exactTokenA,
TokenB: tokenB,
Slippage: slippage,
Deadline: deadline,
}
}
// Route return the message type used for routing the message.
func (msg MsgSwapExactForTokens) Route() string { return RouterKey }
// Type returns a human-readable string for the message, intended for utilization within tags.
func (msg MsgSwapExactForTokens) Type() string { return TypeSwapExactForTokens }
// ValidateBasic does a simple validation check that doesn't require access to any other information.
func (msg MsgSwapExactForTokens) ValidateBasic() error {
if msg.Requester == "" {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "requester address cannot be empty")
}
if _, err := sdk.AccAddressFromBech32(msg.Requester); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid requester address: %s", err)
}
if !msg.ExactTokenA.IsValid() || msg.ExactTokenA.IsZero() {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidCoins, "exact token a deposit amount %s", msg.ExactTokenA)
}
if !msg.TokenB.IsValid() || msg.TokenB.IsZero() {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidCoins, "token b deposit amount %s", msg.TokenB)
}
if msg.ExactTokenA.Denom == msg.TokenB.Denom {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, "denominations can not be equal")
}
if msg.Slippage.IsNil() {
return sdkerrors.Wrapf(ErrInvalidSlippage, "slippage must be set")
}
if msg.Slippage.IsNegative() {
return sdkerrors.Wrapf(ErrInvalidSlippage, "slippage can not be negative")
}
if msg.Deadline <= 0 {
return sdkerrors.Wrapf(ErrInvalidDeadline, "deadline %d", msg.Deadline)
}
return nil
}
// GetSignBytes gets the canonical byte representation of the Msg.
func (msg MsgSwapExactForTokens) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(&msg)
return sdk.MustSortJSON(bz)
}
// GetSigners returns the addresses of signers that must sign.
func (msg MsgSwapExactForTokens) GetSigners() []sdk.AccAddress {
requester, _ := sdk.AccAddressFromBech32(msg.Requester)
return []sdk.AccAddress{requester}
}
// GetDeadline returns the time at which the msg is considered invalid
func (msg MsgSwapExactForTokens) GetDeadline() time.Time {
return time.Unix(msg.Deadline, 0)
}
// DeadlineExceeded returns if the msg has exceeded it's deadline
func (msg MsgSwapExactForTokens) DeadlineExceeded(blockTime time.Time) bool {
return blockTime.Unix() >= msg.Deadline
}
// NewMsgSwapForExactTokens returns a new MsgSwapForExactTokens
func NewMsgSwapForExactTokens(requester string, tokenA sdk.Coin, exactTokenB sdk.Coin, slippage sdk.Dec, deadline int64) *MsgSwapForExactTokens {
return &MsgSwapForExactTokens{
Requester: requester,
TokenA: tokenA,
ExactTokenB: exactTokenB,
Slippage: slippage,
Deadline: deadline,
}
}
// Route return the message type used for routing the message.
func (msg MsgSwapForExactTokens) Route() string { return RouterKey }
// Type returns a human-readable string for the message, intended for utilization within tags.
func (msg MsgSwapForExactTokens) Type() string { return TypeSwapForExactTokens }
// ValidateBasic does a simple validation check that doesn't require access to any other information.
func (msg MsgSwapForExactTokens) ValidateBasic() error {
if msg.Requester == "" {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "requester address cannot be empty")
}
if _, err := sdk.AccAddressFromBech32(msg.Requester); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid requester address: %s", err)
}
if !msg.TokenA.IsValid() || msg.TokenA.IsZero() {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidCoins, "token a deposit amount %s", msg.TokenA)
}
if !msg.ExactTokenB.IsValid() || msg.ExactTokenB.IsZero() {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidCoins, "exact token b deposit amount %s", msg.ExactTokenB)
}
if msg.TokenA.Denom == msg.ExactTokenB.Denom {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, "denominations can not be equal")
}
if msg.Slippage.IsNil() {
return sdkerrors.Wrapf(ErrInvalidSlippage, "slippage must be set")
}
if msg.Slippage.IsNegative() {
return sdkerrors.Wrapf(ErrInvalidSlippage, "slippage can not be negative")
}
if msg.Deadline <= 0 {
return sdkerrors.Wrapf(ErrInvalidDeadline, "deadline %d", msg.Deadline)
}
return nil
}
// GetSignBytes gets the canonical byte representation of the Msg.
func (msg MsgSwapForExactTokens) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(&msg)
return sdk.MustSortJSON(bz)
}
// GetSigners returns the addresses of signers that must sign.
func (msg MsgSwapForExactTokens) GetSigners() []sdk.AccAddress {
requester, _ := sdk.AccAddressFromBech32(msg.Requester)
return []sdk.AccAddress{requester}
}
// GetDeadline returns the time at which the msg is considered invalid
func (msg MsgSwapForExactTokens) GetDeadline() time.Time {
return time.Unix(msg.Deadline, 0)
}
// DeadlineExceeded returns if the msg has exceeded it's deadline
func (msg MsgSwapForExactTokens) DeadlineExceeded(blockTime time.Time) bool {
return blockTime.Unix() >= msg.Deadline
}