-
Notifications
You must be signed in to change notification settings - Fork 182
/
handler.go
368 lines (327 loc) · 13.6 KB
/
handler.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package ammswap
import (
"github.com/okex/exchain/x/ammswap/keeper"
"github.com/okex/exchain/x/ammswap/types"
"github.com/okex/exchain/x/common"
"github.com/okex/exchain/x/common/perf"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// NewHandler creates an sdk.Handler for all the ammswap type messages
func NewHandler(k Keeper) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
ctx = ctx.WithEventManager(sdk.NewEventManager())
var handlerFun func() (*sdk.Result, error)
var name string
switch msg := msg.(type) {
case types.MsgAddLiquidity:
name = "handleMsgAddLiquidity"
handlerFun = func() (*sdk.Result, error) {
return handleMsgAddLiquidity(ctx, k, msg)
}
case types.MsgRemoveLiquidity:
name = "handleMsgRemoveLiquidity"
handlerFun = func() (*sdk.Result, error) {
return handleMsgRemoveLiquidity(ctx, k, msg)
}
case types.MsgCreateExchange:
name = "handleMsgCreateExchange"
handlerFun = func() (*sdk.Result, error) {
return handleMsgCreateExchange(ctx, k, msg)
}
case types.MsgTokenToToken:
name = "handleMsgTokenToToken"
handlerFun = func() (*sdk.Result, error) {
return handleMsgTokenToToken(ctx, k, msg)
}
default:
return nil, types.ErrSwapUnknownMsgType()
}
seq := perf.GetPerf().OnDeliverTxEnter(ctx, types.ModuleName, name)
defer perf.GetPerf().OnDeliverTxExit(ctx, types.ModuleName, name, seq)
res, err := handlerFun()
common.SanityCheckHandler(res, err)
return res, err
}
}
func handleMsgTokenToToken(ctx sdk.Context, k Keeper, msg types.MsgTokenToToken) (*sdk.Result, error) {
_, err := k.GetSwapTokenPair(ctx, msg.GetSwapTokenPairName())
if err != nil {
return swapTokenByRouter(ctx, k, msg)
} else {
return swapToken(ctx, k, msg)
}
}
func handleMsgCreateExchange(ctx sdk.Context, k Keeper, msg types.MsgCreateExchange) (*sdk.Result, error) {
event := sdk.NewEvent(sdk.EventTypeMessage, sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName))
// 0. check if 2 tokens exist
err := k.IsTokenExist(ctx, msg.Token0Name)
if err != nil {
return nil, err
}
err = k.IsTokenExist(ctx, msg.Token1Name)
if err != nil {
return nil, err
}
// 1. check if the token pair exists
tokenPairName := msg.GetSwapTokenPairName()
_, err = k.GetSwapTokenPair(ctx, tokenPairName)
if err == nil {
return types.ErrSwapTokenPairExist().Result()
}
// 2. check if the pool token exists
poolTokenName := types.GetPoolTokenName(msg.Token0Name, msg.Token1Name)
_, err = k.GetPoolTokenInfo(ctx, poolTokenName)
if err == nil {
return types.ErrPoolTokenPairExist().Result()
}
// 3. create the pool token
k.NewPoolToken(ctx, poolTokenName)
// 4. create the token pair
swapTokenPair := types.NewSwapPair(msg.Token0Name, msg.Token1Name)
k.SetSwapTokenPair(ctx, tokenPairName, swapTokenPair)
// 5. notify backend module
k.OnCreateExchange(ctx, swapTokenPair)
event = event.AppendAttributes(sdk.NewAttribute("pool-token-name", poolTokenName))
event = event.AppendAttributes(sdk.NewAttribute("token-pair", tokenPairName))
ctx.EventManager().EmitEvent(event)
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
}
func handleMsgAddLiquidity(ctx sdk.Context, k Keeper, msg types.MsgAddLiquidity) (*sdk.Result, error) {
event := sdk.NewEvent(sdk.EventTypeMessage, sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName))
if msg.Deadline < ctx.BlockTime().Unix() {
return types.ErrBlockTimeBigThanDeadline().Result()
}
swapTokenPair, err := k.GetSwapTokenPair(ctx, msg.GetSwapTokenPairName())
if err != nil {
return nil, err
}
baseTokens := sdk.NewDecCoinFromDec(msg.MaxBaseAmount.Denom, sdk.ZeroDec())
var liquidity sdk.Dec
poolToken, err := k.GetPoolTokenInfo(ctx, swapTokenPair.PoolTokenName)
if err != nil {
return nil, err
}
if swapTokenPair.QuotePooledCoin.Amount.IsZero() && swapTokenPair.BasePooledCoin.Amount.IsZero() {
baseTokens.Amount = msg.MaxBaseAmount.Amount
liquidity = sdk.NewDec(1)
} else if swapTokenPair.BasePooledCoin.IsPositive() && swapTokenPair.QuotePooledCoin.IsPositive() {
baseTokens.Amount = common.MulAndQuo(msg.QuoteAmount.Amount, swapTokenPair.BasePooledCoin.Amount, swapTokenPair.QuotePooledCoin.Amount)
totalSupply := k.GetPoolTokenAmount(ctx, swapTokenPair.PoolTokenName)
if baseTokens.IsZero() {
baseTokens.Amount = sdk.NewDecWithPrec(1, sdk.Precision)
}
if totalSupply.IsZero() {
return types.ErrIsZeroValue("totalSupply").Result()
}
liquidity = common.MulAndQuo(msg.QuoteAmount.Amount, totalSupply, swapTokenPair.QuotePooledCoin.Amount)
if liquidity.IsZero() {
return types.ErrIsZeroValue("liquidity").Result()
}
} else {
return types.ErrInvalidTokenPair(swapTokenPair.String()).Result()
}
if baseTokens.Amount.GT(msg.MaxBaseAmount.Amount) {
return types.ErrBaseTokensAmountBiggerThanMax().Result()
}
if liquidity.LT(msg.MinLiquidity) {
return types.ErrLessThan("liquidity", "min liquidity").Result()
}
// transfer coins
coins := sdk.SysCoins{
msg.QuoteAmount,
baseTokens,
}
coins = coinSort(coins)
err = k.SendCoinsToPool(ctx, coins, msg.Sender)
if err != nil {
return types.ErrSendCoinsFailed(err).Result()
}
// update swapTokenPair
swapTokenPair.QuotePooledCoin = swapTokenPair.QuotePooledCoin.Add(msg.QuoteAmount)
swapTokenPair.BasePooledCoin = swapTokenPair.BasePooledCoin.Add(baseTokens)
k.SetSwapTokenPair(ctx, msg.GetSwapTokenPairName(), swapTokenPair)
// update poolToken
poolCoins := sdk.NewDecCoinFromDec(poolToken.Symbol, liquidity)
err = k.MintPoolCoinsToUser(ctx, sdk.SysCoins{poolCoins}, msg.Sender)
if err != nil {
return types.ErrMintPoolTokenFailed(err).Result()
}
event.AppendAttributes(sdk.NewAttribute("liquidity", liquidity.String()))
event.AppendAttributes(sdk.NewAttribute("baseAmount", baseTokens.String()))
ctx.EventManager().EmitEvent(event)
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
}
func handleMsgRemoveLiquidity(ctx sdk.Context, k Keeper, msg types.MsgRemoveLiquidity) (*sdk.Result, error) {
event := sdk.NewEvent(sdk.EventTypeMessage, sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName))
if msg.Deadline < ctx.BlockTime().Unix() {
return types.ErrMsgDeadlineLessThanBlockTime().Result()
}
swapTokenPair, err := k.GetSwapTokenPair(ctx, msg.GetSwapTokenPairName())
if err != nil {
return nil, err
}
liquidity := msg.Liquidity
poolTokenAmount := k.GetPoolTokenAmount(ctx, swapTokenPair.PoolTokenName)
if poolTokenAmount.LT(liquidity) {
return types.ErrLessThan("pool token amount", "liquidity").Result()
}
baseDec := common.MulAndQuo(swapTokenPair.BasePooledCoin.Amount, liquidity, poolTokenAmount)
quoteDec := common.MulAndQuo(swapTokenPair.QuotePooledCoin.Amount, liquidity, poolTokenAmount)
baseAmount := sdk.NewDecCoinFromDec(swapTokenPair.BasePooledCoin.Denom, baseDec)
quoteAmount := sdk.NewDecCoinFromDec(swapTokenPair.QuotePooledCoin.Denom, quoteDec)
if baseAmount.IsLT(msg.MinBaseAmount) {
return types.ErrLessThan("base amount", "min base amount").Result()
}
if quoteAmount.IsLT(msg.MinQuoteAmount) {
return types.ErrLessThan("quote amount", "min quote amount").Result()
}
// transfer coins
coins := sdk.SysCoins{
baseAmount,
quoteAmount,
}
coins = coinSort(coins)
err = k.SendCoinsFromPoolToAccount(ctx, coins, msg.Sender)
if err != nil {
return types.ErrSendCoinsFromPoolToAccountFailed(err.Error()).Result()
}
// update swapTokenPair
swapTokenPair.QuotePooledCoin = swapTokenPair.QuotePooledCoin.Sub(quoteAmount)
swapTokenPair.BasePooledCoin = swapTokenPair.BasePooledCoin.Sub(baseAmount)
k.SetSwapTokenPair(ctx, msg.GetSwapTokenPairName(), swapTokenPair)
// update poolToken
poolCoins := sdk.NewDecCoinFromDec(swapTokenPair.PoolTokenName, liquidity)
err = k.BurnPoolCoinsFromUser(ctx, sdk.SysCoins{poolCoins}, msg.Sender)
if err != nil {
return types.ErrBurnPoolTokenFailed(err).Result()
}
event.AppendAttributes(sdk.NewAttribute("quoteAmount", quoteAmount.String()))
event.AppendAttributes(sdk.NewAttribute("baseAmount", baseAmount.String()))
ctx.EventManager().EmitEvent(event)
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
}
func swapToken(ctx sdk.Context, k Keeper, msg types.MsgTokenToToken) (*sdk.Result, error) {
event := sdk.NewEvent(sdk.EventTypeMessage, sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName))
if err := common.HasSufficientCoins(msg.Sender, k.GetTokenKeeper().GetCoins(ctx, msg.Sender),
sdk.SysCoins{msg.SoldTokenAmount}); err != nil {
return common.ErrInsufficientCoins(DefaultParamspace, err.Error()).Result()
}
if msg.Deadline < ctx.BlockTime().Unix() {
return types.ErrBlockTimeBigThanDeadline().Result()
}
swapTokenPair, err := k.GetSwapTokenPair(ctx, msg.GetSwapTokenPairName())
if err != nil {
return nil, err
}
if swapTokenPair.BasePooledCoin.IsZero() || swapTokenPair.QuotePooledCoin.IsZero() {
return types.ErrIsZeroValue("base pooled coin or quote pooled coin").Result()
}
params := k.GetParams(ctx)
tokenBuy := keeper.CalculateTokenToBuy(swapTokenPair, msg.SoldTokenAmount, msg.MinBoughtTokenAmount.Denom, params)
if tokenBuy.IsZero() {
return types.ErrIsZeroValue("token buy").Result()
}
if tokenBuy.Amount.LT(msg.MinBoughtTokenAmount.Amount) {
return types.ErrLessThan("token buy amount", "min bought token amount").Result()
}
res, err := swapTokenNativeToken(ctx, k, swapTokenPair, tokenBuy, msg)
if err != nil {
return res, err
}
event.AppendAttributes(sdk.NewAttribute("bought_token_amount", tokenBuy.String()))
event.AppendAttributes(sdk.NewAttribute("recipient", msg.Recipient.String()))
ctx.EventManager().EmitEvent(event)
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
}
func swapTokenByRouter(ctx sdk.Context, k Keeper, msg types.MsgTokenToToken) (*sdk.Result, error) {
event := sdk.NewEvent(sdk.EventTypeMessage, sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName))
if msg.Deadline < ctx.BlockTime().Unix() {
return types.ErrBlockTimeBigThanDeadline().Result()
}
if err := common.HasSufficientCoins(msg.Sender, k.GetTokenKeeper().GetCoins(ctx, msg.Sender),
sdk.SysCoins{msg.SoldTokenAmount}); err != nil {
return common.ErrInsufficientCoins(DefaultParamspace, err.Error()).Result()
}
tokenPairOne := types.GetSwapTokenPairName(msg.SoldTokenAmount.Denom, sdk.DefaultBondDenom)
swapTokenPairOne, err := k.GetSwapTokenPair(ctx, tokenPairOne)
if err != nil {
return nil, types.ErrNonExistSwapTokenPair(msg.GetSwapTokenPairName())
}
if swapTokenPairOne.BasePooledCoin.IsZero() || swapTokenPairOne.QuotePooledCoin.IsZero() {
return types.ErrIsZeroValue("base pooled coin or quote pooled coin").Result()
}
tokenPairTwo := types.GetSwapTokenPairName(msg.MinBoughtTokenAmount.Denom, sdk.DefaultBondDenom)
swapTokenPairTwo, err := k.GetSwapTokenPair(ctx, tokenPairTwo)
if err != nil {
return nil, types.ErrNonExistSwapTokenPair(msg.GetSwapTokenPairName())
}
if swapTokenPairTwo.BasePooledCoin.IsZero() || swapTokenPairTwo.QuotePooledCoin.IsZero() {
return types.ErrIsZeroValue("base pooled coin or quote pooled coin").Result()
}
nativeAmount := sdk.NewDecCoinFromDec(sdk.DefaultBondDenom, sdk.MustNewDecFromStr("0"))
params := k.GetParams(ctx)
msgOne := msg
msgOne.MinBoughtTokenAmount = nativeAmount
tokenNative := keeper.CalculateTokenToBuy(swapTokenPairOne, msgOne.SoldTokenAmount, msgOne.MinBoughtTokenAmount.Denom, params)
if tokenNative.IsZero() {
return types.ErrIsZeroValue("token native").Result()
}
msgTwo := msg
msgTwo.SoldTokenAmount = tokenNative
tokenBuy := keeper.CalculateTokenToBuy(swapTokenPairTwo, msgTwo.SoldTokenAmount, msgTwo.MinBoughtTokenAmount.Denom, params)
// sanity check. user may set MinBoughtTokenAmount to zero on front end.
// if set zero,this will not return err
if tokenBuy.IsZero() {
return types.ErrIsZeroValue("token to buy amount").Result()
}
if tokenBuy.Amount.LT(msg.MinBoughtTokenAmount.Amount) {
return types.ErrLessThan("token buy amount", "min bought token amount").Result()
}
res, err := swapTokenNativeToken(ctx, k, swapTokenPairOne, tokenNative, msgOne)
if err != nil {
return res, err
}
res, err = swapTokenNativeToken(ctx, k, swapTokenPairTwo, tokenBuy, msgTwo)
if err != nil {
return res, err
}
event.AppendAttributes(sdk.NewAttribute("bought_token_amount", tokenBuy.String()))
event.AppendAttributes(sdk.NewAttribute("recipient", msg.Recipient.String()))
ctx.EventManager().EmitEvent(event)
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
}
func swapTokenNativeToken(
ctx sdk.Context, k Keeper, swapTokenPair SwapTokenPair, tokenBuy sdk.SysCoin,
msg types.MsgTokenToToken,
) (*sdk.Result, error) {
// transfer coins
err := k.SendCoinsToPool(ctx, sdk.SysCoins{msg.SoldTokenAmount}, msg.Sender)
if err != nil {
return types.ErrSendCoinsToPoolFailed(err.Error()).Result()
}
err = k.SendCoinsFromPoolToAccount(ctx, sdk.SysCoins{tokenBuy}, msg.Recipient)
if err != nil {
return types.ErrSendCoinsFromPoolToAccountFailed(err.Error()).Result()
}
// update swapTokenPair
if msg.MinBoughtTokenAmount.Denom < msg.SoldTokenAmount.Denom {
swapTokenPair.QuotePooledCoin = swapTokenPair.QuotePooledCoin.Add(msg.SoldTokenAmount)
swapTokenPair.BasePooledCoin = swapTokenPair.BasePooledCoin.Sub(tokenBuy)
} else {
swapTokenPair.QuotePooledCoin = swapTokenPair.QuotePooledCoin.Sub(tokenBuy)
swapTokenPair.BasePooledCoin = swapTokenPair.BasePooledCoin.Add(msg.SoldTokenAmount)
}
k.SetSwapTokenPair(ctx, msg.GetSwapTokenPairName(), swapTokenPair)
k.OnSwapToken(ctx, msg.Recipient, swapTokenPair, msg.SoldTokenAmount, tokenBuy)
return &sdk.Result{}, nil
}
func coinSort(coins sdk.SysCoins) sdk.SysCoins {
var newCoins sdk.SysCoins
for _, coin := range coins {
if coin.Amount.IsPositive() {
newCoins = append(newCoins, coin)
}
}
newCoins = newCoins.Sort()
return newCoins
}