/
msgs.go
267 lines (226 loc) · 8.76 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
package types
import (
sdk "github.com/cosmos/cosmos-sdk/types"
)
// PoolSwap message types and routes
const (
TypeMsgAddLiquidity = "add_liquidity"
TypeMsgTokenSwap = "token_swap"
)
// MsgAddLiquidity Deposit quote_amount and base_amount at current ratio to mint pool tokens.
type MsgAddLiquidity struct {
MinLiquidity sdk.Dec `json:"min_liquidity"` // Minimum number of sender will mint if total pool token supply is greater than 0.
MaxBaseAmount sdk.SysCoin `json:"max_base_amount"` // Maximum number of tokens deposited. Deposits max amount if total pool token supply is 0.
QuoteAmount sdk.SysCoin `json:"quote_amount"` // Quote token amount
Deadline int64 `json:"deadline"` // Time after which this transaction can no longer be executed.
Sender sdk.AccAddress `json:"sender"` // Sender
}
// NewMsgAddLiquidity is a constructor function for MsgAddLiquidity
func NewMsgAddLiquidity(minLiquidity sdk.Dec, maxBaseAmount, quoteAmount sdk.SysCoin, deadline int64, sender sdk.AccAddress) MsgAddLiquidity {
return MsgAddLiquidity{
MinLiquidity: minLiquidity,
MaxBaseAmount: maxBaseAmount,
QuoteAmount: quoteAmount,
Deadline: deadline,
Sender: sender,
}
}
// Route should return the name of the module
func (msg MsgAddLiquidity) Route() string { return RouterKey }
// Type should return the action
func (msg MsgAddLiquidity) Type() string { return "add_liquidity" }
// ValidateBasic runs stateless checks on the message
func (msg MsgAddLiquidity) ValidateBasic() sdk.Error {
if msg.Sender.Empty() {
return ErrAddressIsRequire("sender")
}
if msg.MinLiquidity.IsNegative() {
return ErrMinLiquidityIsNegative()
}
if !(msg.MaxBaseAmount.IsPositive() && msg.QuoteAmount.IsPositive()) {
return ErrMaxBaseAmountOrQuoteAmountIsNegative()
}
if !msg.MaxBaseAmount.IsValid() {
return ErrMaxBaseAmount()
}
if !msg.QuoteAmount.IsValid() {
return ErrQuoteAmount()
}
err := ValidateBaseAndQuoteAmount(msg.MaxBaseAmount.Denom, msg.QuoteAmount.Denom)
if err != nil {
return err
}
return nil
}
// GetSignBytes encodes the message for signing
func (msg MsgAddLiquidity) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg))
}
// GetSigners defines whose signature is required
func (msg MsgAddLiquidity) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Sender}
}
// GetSwapTokenPair defines token pair
func (msg MsgAddLiquidity) GetSwapTokenPairName() string {
return GetSwapTokenPairName(msg.MaxBaseAmount.Denom, msg.QuoteAmount.Denom)
}
// MsgRemoveLiquidity burns pool tokens to withdraw okt and Tokens at current ratio.
type MsgRemoveLiquidity struct {
Liquidity sdk.Dec `json:"liquidity"` // Amount of pool token burned.
MinBaseAmount sdk.SysCoin `json:"min_base_amount"` // Minimum base amount.
MinQuoteAmount sdk.SysCoin `json:"min_quote_amount"` // Minimum quote amount.
Deadline int64 `json:"deadline"` // Time after which this transaction can no longer be executed.
Sender sdk.AccAddress `json:"sender"` // Sender
}
// NewMsgRemoveLiquidity is a constructor function for MsgAddLiquidity
func NewMsgRemoveLiquidity(liquidity sdk.Dec, minBaseAmount, minQuoteAmount sdk.SysCoin, deadline int64, sender sdk.AccAddress) MsgRemoveLiquidity {
return MsgRemoveLiquidity{
Liquidity: liquidity,
MinBaseAmount: minBaseAmount,
MinQuoteAmount: minQuoteAmount,
Deadline: deadline,
Sender: sender,
}
}
// Route should return the name of the module
func (msg MsgRemoveLiquidity) Route() string { return RouterKey }
// Type should return the action
func (msg MsgRemoveLiquidity) Type() string { return "remove_liquidity" }
// ValidateBasic runs stateless checks on the message
func (msg MsgRemoveLiquidity) ValidateBasic() sdk.Error {
if msg.Sender.Empty() {
return ErrAddressIsRequire("sender")
}
if !(msg.Liquidity.IsPositive()) {
return ErrMinLiquidityIsNegative()
}
if !msg.MinBaseAmount.IsValid() {
return ErrMinBaseAmount()
}
if !msg.MinQuoteAmount.IsValid() {
return ErrMinQuoteAmount()
}
err := ValidateBaseAndQuoteAmount(msg.MinBaseAmount.Denom, msg.MinQuoteAmount.Denom)
if err != nil {
return err
}
return nil
}
// GetSignBytes encodes the message for signing
func (msg MsgRemoveLiquidity) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg))
}
// GetSigners defines whose signature is required
func (msg MsgRemoveLiquidity) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Sender}
}
// GetSwapTokenPair defines token pair
func (msg MsgRemoveLiquidity) GetSwapTokenPairName() string {
return GetSwapTokenPairName(msg.MinBaseAmount.Denom, msg.MinQuoteAmount.Denom)
}
// MsgCreateExchange creates a new exchange with token
type MsgCreateExchange struct {
Token0Name string `json:"token0_name"`
Token1Name string `json:"token1_name"`
Sender sdk.AccAddress `json:"sender"` // Sender
}
// NewMsgCreateExchange create a new exchange with token
func NewMsgCreateExchange(token0Name string, token1Name string, sender sdk.AccAddress) MsgCreateExchange {
return MsgCreateExchange{
Token0Name: token0Name,
Token1Name: token1Name,
Sender: sender,
}
}
// Route should return the name of the module
func (msg MsgCreateExchange) Route() string { return RouterKey }
// Type should return the action
func (msg MsgCreateExchange) Type() string { return "create_exchange" }
// ValidateBasic runs stateless checks on the message
func (msg MsgCreateExchange) ValidateBasic() sdk.Error {
if msg.Sender.Empty() {
return ErrAddressIsRequire("sender")
}
if err := ValidateSwapAmountName(msg.Token0Name); err != nil {
return err
}
if err := ValidateSwapAmountName(msg.Token1Name); err != nil {
return err
}
if msg.Token0Name == msg.Token1Name {
return ErrToken0NameEqualToken1Name()
}
return nil
}
// GetSignBytes encodes the message for signing
func (msg MsgCreateExchange) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg))
}
// GetSigners defines whose signature is required
func (msg MsgCreateExchange) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Sender}
}
// GetSwapTokenPair defines token pair
func (msg MsgCreateExchange) GetSwapTokenPairName() string {
return GetSwapTokenPairName(msg.Token0Name, msg.Token1Name)
}
// MsgTokenToToken define the message for swap between token and DefaultBondDenom
type MsgTokenToToken struct {
SoldTokenAmount sdk.SysCoin `json:"sold_token_amount"` // Amount of Tokens sold.
MinBoughtTokenAmount sdk.SysCoin `json:"min_bought_token_amount"` // Minimum token purchased.
Deadline int64 `json:"deadline"` // Time after which this transaction can no longer be executed.
Recipient sdk.AccAddress `json:"recipient"` // Recipient address,transfer Tokens to recipient.default recipient is sender.
Sender sdk.AccAddress `json:"sender"` // Sender
}
// NewMsgTokenToToken is a constructor function for MsgTokenOKTSwap
func NewMsgTokenToToken(
soldTokenAmount, minBoughtTokenAmount sdk.SysCoin, deadline int64, recipient, sender sdk.AccAddress,
) MsgTokenToToken {
return MsgTokenToToken{
SoldTokenAmount: soldTokenAmount,
MinBoughtTokenAmount: minBoughtTokenAmount,
Deadline: deadline,
Recipient: recipient,
Sender: sender,
}
}
// Route should return the name of the module
func (msg MsgTokenToToken) Route() string { return RouterKey }
// Type should return the action
func (msg MsgTokenToToken) Type() string { return TypeMsgTokenSwap }
// ValidateBasic runs stateless checks on the message
func (msg MsgTokenToToken) ValidateBasic() sdk.Error {
if msg.Sender.Empty() {
return ErrAddressIsRequire("sender")
}
if msg.Recipient.Empty() {
return ErrAddressIsRequire("recipient")
}
if !(msg.SoldTokenAmount.IsPositive()) {
return ErrSoldTokenAmountIsNegative()
}
if !msg.SoldTokenAmount.IsValid() {
return ErrSoldTokenAmount()
}
if !msg.MinBoughtTokenAmount.IsValid() {
return ErrMinBoughtTokenAmount()
}
baseAmountName, quoteAmountName := GetBaseQuoteTokenName(msg.SoldTokenAmount.Denom, msg.MinBoughtTokenAmount.Denom)
err := ValidateBaseAndQuoteAmount(baseAmountName, quoteAmountName)
if err != nil {
return err
}
return nil
}
// GetSignBytes encodes the message for signing
func (msg MsgTokenToToken) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg))
}
// GetSigners defines whose signature is required
func (msg MsgTokenToToken) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Sender}
}
// GetSwapTokenPair defines token pair
func (msg MsgTokenToToken) GetSwapTokenPairName() string {
return GetSwapTokenPairName(msg.MinBoughtTokenAmount.Denom, msg.SoldTokenAmount.Denom)
}