@@ -41,6 +41,10 @@ func (k Keeper) ValidateMsgCreateLiquidityPool(ctx sdk.Context, msg *types.MsgCr
41
41
return types .ErrBadOrderingReserveCoin
42
42
}
43
43
44
+ if denomA == denomB {
45
+ return types .ErrEqualDenom
46
+ }
47
+
44
48
poolKey := types .GetPoolKey (msg .ReserveCoinDenoms , msg .PoolTypeIndex )
45
49
reserveAcc := types .GetPoolReserveAcc (poolKey )
46
50
_ , found := k .GetLiquidityPoolByReserveAccIndex (ctx , reserveAcc )
@@ -133,7 +137,7 @@ func (k Keeper) CreateLiquidityPool(ctx sdk.Context, msg *types.MsgCreateLiquidi
133
137
134
138
PoolCoinDenom := types .GetPoolCoinDenom (reserveAcc )
135
139
136
- liquidityPool := types.LiquidityPool {
140
+ pool := types.LiquidityPool {
137
141
//PoolId: will set on SetLiquidityPoolAtomic
138
142
PoolTypeIndex : msg .PoolTypeIndex ,
139
143
ReserveCoinDenoms : reserveCoinDenoms ,
@@ -142,7 +146,7 @@ func (k Keeper) CreateLiquidityPool(ctx sdk.Context, msg *types.MsgCreateLiquidi
142
146
}
143
147
144
148
batchEscrowAcc := k .accountKeeper .GetModuleAddress (types .ModuleName )
145
- mintPoolCoin := sdk .NewCoins (sdk .NewCoin (liquidityPool .PoolCoinDenom , params .InitPoolCoinMintAmount ))
149
+ mintPoolCoin := sdk .NewCoins (sdk .NewCoin (pool .PoolCoinDenom , params .InitPoolCoinMintAmount ))
146
150
if err := k .bankKeeper .MintCoins (ctx , types .ModuleName , mintPoolCoin ); err != nil {
147
151
return err
148
152
}
@@ -166,23 +170,24 @@ func (k Keeper) CreateLiquidityPool(ctx sdk.Context, msg *types.MsgCreateLiquidi
166
170
return err
167
171
}
168
172
169
- liquidityPool = k .SetLiquidityPoolAtomic (ctx , liquidityPool )
170
- batch := types .NewLiquidityPoolBatch (liquidityPool .PoolId , 1 )
173
+ pool = k .SetLiquidityPoolAtomic (ctx , pool )
174
+ batch := types .NewLiquidityPoolBatch (pool .PoolId , 1 )
171
175
172
176
k .SetLiquidityPoolBatch (ctx , batch )
173
177
174
178
// TODO: remove result state check, debugging
175
- reserveCoins := k .GetReserveCoins (ctx , liquidityPool )
179
+ reserveCoins := k .GetReserveCoins (ctx , pool )
176
180
lastReserveRatio := sdk .NewDecFromInt (reserveCoins [0 ].Amount ).Quo (sdk .NewDecFromInt (reserveCoins [1 ].Amount ))
177
181
logger := k .Logger (ctx )
178
- logger .Info ("createPool" , msg , "pool" , liquidityPool , "reserveCoins" , reserveCoins , "lastReserveRatio" , lastReserveRatio )
182
+ logger .Info ("createPool" , msg , "pool" , pool , "reserveCoins" , reserveCoins , "lastReserveRatio" , lastReserveRatio )
179
183
return nil
180
184
}
181
185
182
186
// Get reserve Coin from the liquidity pool
183
187
func (k Keeper ) GetReserveCoins (ctx sdk.Context , pool types.LiquidityPool ) (reserveCoins sdk.Coins ) {
188
+ reserveAcc := pool .GetReserveAccount ()
184
189
for _ , denom := range pool .ReserveCoinDenoms {
185
- reserveCoins = reserveCoins .Add (k .bankKeeper .GetBalance (ctx , pool . GetReserveAccount () , denom ))
190
+ reserveCoins = reserveCoins .Add (k .bankKeeper .GetBalance (ctx , reserveAcc , denom ))
186
191
}
187
192
return
188
193
}
@@ -247,12 +252,6 @@ func (k Keeper) ValidateMsgDepositLiquidityPool(ctx sdk.Context, msg types.MsgDe
247
252
return types .ErrPoolNotExists
248
253
}
249
254
250
- for _ , coin := range msg .DepositCoins {
251
- if ! types .StringInSlice (coin .Denom , pool .ReserveCoinDenoms ) {
252
- return types .ErrInvalidDenom
253
- }
254
- }
255
-
256
255
if msg .DepositCoins .Len () != len (pool .ReserveCoinDenoms ) {
257
256
return types .ErrNumOfReserveCoin
258
257
}
@@ -264,6 +263,10 @@ func (k Keeper) ValidateMsgDepositLiquidityPool(ctx sdk.Context, msg types.MsgDe
264
263
}
265
264
// TODO: validate msgIndex
266
265
266
+ denomA , denomB := types .AlphabeticalDenomPair (msg .DepositCoins [0 ].Denom , msg .DepositCoins [1 ].Denom )
267
+ if denomA != pool .ReserveCoinDenoms [0 ] || denomB != pool .ReserveCoinDenoms [1 ] {
268
+ return types .ErrNotMatchedReserveCoin
269
+ }
267
270
return nil
268
271
}
269
272
@@ -282,7 +285,47 @@ func (k Keeper) DepositLiquidityPool(ctx sdk.Context, msg types.BatchPoolDeposit
282
285
return types .ErrPoolNotExists
283
286
}
284
287
288
+ var inputs []banktypes.Input
289
+ var outputs []banktypes.Output
290
+
291
+ batchEscrowAcc := k .accountKeeper .GetModuleAddress (types .ModuleName )
292
+ reserveAcc := pool .GetReserveAccount ()
293
+ depositor := msg .Msg .GetDepositor ()
294
+ params := k .GetParams (ctx )
295
+
285
296
reserveCoins := k .GetReserveCoins (ctx , pool )
297
+
298
+ // case of reserve coins has run out, ReinitializePool
299
+ if reserveCoins .IsZero () {
300
+ mintPoolCoin := sdk .NewCoins (sdk .NewCoin (pool .PoolCoinDenom , params .InitPoolCoinMintAmount ))
301
+ if err := k .bankKeeper .MintCoins (ctx , types .ModuleName , mintPoolCoin ); err != nil {
302
+ return err
303
+ }
304
+ inputs = append (inputs , banktypes .NewInput (batchEscrowAcc , msg .Msg .DepositCoins ))
305
+ outputs = append (outputs , banktypes .NewOutput (reserveAcc , msg .Msg .DepositCoins ))
306
+
307
+ inputs = append (inputs , banktypes .NewInput (batchEscrowAcc , mintPoolCoin ))
308
+ outputs = append (outputs , banktypes .NewOutput (depositor , mintPoolCoin ))
309
+
310
+ // execute multi-send
311
+ if err := k .bankKeeper .InputOutputCoins (ctx , inputs , outputs ); err != nil {
312
+ return err
313
+ }
314
+
315
+ msg .Succeed = true
316
+ msg .ToDelete = true
317
+ k .SetLiquidityPoolBatchDepositMsg (ctx , msg .Msg .PoolId , msg )
318
+
319
+ // TODO: remove result state check, debugging
320
+ reserveCoins := k .GetReserveCoins (ctx , pool )
321
+ lastReserveRatio := sdk .NewDecFromInt (reserveCoins [0 ].Amount ).Quo (sdk .NewDecFromInt (reserveCoins [1 ].Amount ))
322
+ logger := k .Logger (ctx )
323
+ logger .Info ("ReinitializePool" , msg , "pool" , pool , "reserveCoins" , reserveCoins , "lastReserveRatio" , lastReserveRatio )
324
+ return nil
325
+ } else if reserveCoins .Len () != msg .Msg .DepositCoins .Len () {
326
+ return types .ErrNumOfReserveCoin
327
+ }
328
+
286
329
reserveCoins .Sort ()
287
330
288
331
coinA := depositCoins [0 ]
@@ -292,12 +335,6 @@ func (k Keeper) DepositLiquidityPool(ctx sdk.Context, msg types.BatchPoolDeposit
292
335
depositableAmount := coinB .Amount .ToDec ().Mul (lastReserveRatio ).TruncateInt ()
293
336
depositableAmountA := coinA .Amount
294
337
depositableAmountB := coinB .Amount
295
- var inputs []banktypes.Input
296
- var outputs []banktypes.Output
297
-
298
- batchEscrowAcc := k .accountKeeper .GetModuleAddress (types .ModuleName )
299
- reserveAcc := pool .GetReserveAccount ()
300
- depositor := msg .Msg .GetDepositor ()
301
338
302
339
if coinA .Amount .LT (depositableAmount ) {
303
340
depositableAmountB = coinA .Amount .ToDec ().Quo (lastReserveRatio ).TruncateInt ()
@@ -330,22 +367,22 @@ func (k Keeper) DepositLiquidityPool(ctx sdk.Context, msg types.BatchPoolDeposit
330
367
outputs = append (outputs , banktypes .NewOutput (reserveAcc , sdk .NewCoins (coinB )))
331
368
}
332
369
333
- // execute multi-send
334
- if err := k .bankKeeper .InputOutputCoins (ctx , inputs , outputs ); err != nil {
335
- return err
336
- }
337
-
338
370
// calculate pool token mint amount
339
371
poolCoinAmt := k .GetPoolCoinTotalSupply (ctx , pool ).Mul (depositableAmountA ).Quo (reserveCoins [0 ].Amount ) // TODO: coinA after executed ?
340
- poolCoin := sdk .NewCoins (sdk .NewCoin (pool .PoolCoinDenom , poolCoinAmt ))
372
+ mintPoolCoin := sdk .NewCoins (sdk .NewCoin (pool .PoolCoinDenom , poolCoinAmt ))
341
373
342
374
// mint pool token to Depositor
343
- if err := k .bankKeeper .MintCoins (ctx , types .ModuleName , poolCoin ); err != nil {
375
+ if err := k .bankKeeper .MintCoins (ctx , types .ModuleName , mintPoolCoin ); err != nil {
344
376
panic (err )
345
377
}
346
- if err := k .bankKeeper .SendCoinsFromModuleToAccount (ctx , types .ModuleName , depositor , poolCoin ); err != nil {
347
- panic (err )
378
+ inputs = append (inputs , banktypes .NewInput (batchEscrowAcc , mintPoolCoin ))
379
+ outputs = append (outputs , banktypes .NewOutput (depositor , mintPoolCoin ))
380
+
381
+ // execute multi-send
382
+ if err := k .bankKeeper .InputOutputCoins (ctx , inputs , outputs ); err != nil {
383
+ return err
348
384
}
385
+
349
386
msg .Succeed = true
350
387
msg .ToDelete = true
351
388
k .SetLiquidityPoolBatchDepositMsg (ctx , msg .Msg .PoolId , msg )
@@ -363,7 +400,19 @@ func (k Keeper) ValidateMsgWithdrawLiquidityPool(ctx sdk.Context, msg types.MsgW
363
400
if err := msg .ValidateBasic (); err != nil {
364
401
return err
365
402
}
366
- // TODO: add validate logic
403
+ pool , found := k .GetLiquidityPool (ctx , msg .PoolId )
404
+ if ! found {
405
+ return types .ErrPoolNotExists
406
+ }
407
+
408
+ if msg .PoolCoin .Denom != pool .PoolCoinDenom {
409
+ return types .ErrBadPoolCoinDenom
410
+ }
411
+
412
+ poolCoinTotalSupply := k .GetPoolCoinTotalSupply (ctx , pool )
413
+ if msg .PoolCoin .Amount .GT (poolCoinTotalSupply ) {
414
+ return types .ErrBadPoolCoinAmount
415
+ }
367
416
return nil
368
417
}
369
418
@@ -375,6 +424,12 @@ func (k Keeper) ValidateMsgSwap(ctx sdk.Context, msg types.MsgSwap) error {
375
424
if ! found {
376
425
return types .ErrPoolNotExists
377
426
}
427
+
428
+ denomA , denomB := types .AlphabeticalDenomPair (msg .OfferCoin .Denom , msg .DemandCoinDenom )
429
+ if denomA != pool .ReserveCoinDenoms [0 ] || denomB != pool .ReserveCoinDenoms [1 ] {
430
+ return types .ErrNotMatchedReserveCoin
431
+ }
432
+
378
433
// can not exceed max order ratio of reserve coins that can be ordered at a order
379
434
reserveCoinAmt := k .GetReserveCoins (ctx , pool ).AmountOf (msg .OfferCoin .Denom )
380
435
maximumOrderableAmt := reserveCoinAmt .ToDec ().Mul (types .GetMaxOrderRatio ()).TruncateInt ()
@@ -408,37 +463,43 @@ func (k Keeper) WithdrawLiquidityPool(ctx sdk.Context, msg types.BatchPoolWithdr
408
463
var inputs []banktypes.Input
409
464
var outputs []banktypes.Output
410
465
466
+ reserveAcc := pool .GetReserveAccount ()
467
+ withdrawer := msg .Msg .GetWithdrawer ()
468
+
411
469
for _ , reserveCoin := range reserveCoins {
412
470
withdrawAmt := reserveCoin .Amount .Mul (poolCoin .Amount ).Quo (totalSupply )
413
- inputs = append (inputs , banktypes .NewInput (pool . GetReserveAccount () ,
471
+ inputs = append (inputs , banktypes .NewInput (reserveAcc ,
414
472
sdk .NewCoins (sdk .NewCoin (reserveCoin .Denom , withdrawAmt ))))
415
- outputs = append (outputs , banktypes .NewOutput (msg . Msg . GetWithdrawer () ,
473
+ outputs = append (outputs , banktypes .NewOutput (withdrawer ,
416
474
sdk .NewCoins (sdk .NewCoin (reserveCoin .Denom , withdrawAmt ))))
417
475
}
418
476
419
477
// execute multi-send
420
478
if err := k .bankKeeper .InputOutputCoins (ctx , inputs , outputs ); err != nil {
421
479
return err
422
480
}
423
- if err := k .bankKeeper .SendCoinsFromAccountToModule (ctx , k .accountKeeper .GetModuleAddress (types .ModuleName ),
424
- types .ModuleName , poolCoins ); err != nil {
425
- panic (err )
426
- }
481
+
482
+ // burn the escrowed pool coins
427
483
if err := k .bankKeeper .BurnCoins (ctx , types .ModuleName , poolCoins ); err != nil {
428
484
panic (err )
429
485
}
486
+
430
487
msg .Succeed = true
431
488
msg .ToDelete = true
432
489
k .SetLiquidityPoolBatchWithdrawMsg (ctx , msg .Msg .PoolId , msg )
433
490
// TODO: add events for batch result, each err cases
434
491
435
492
// TODO: remove result state check, debugging
436
493
reserveCoins = k .GetReserveCoins (ctx , pool )
437
- if ! reserveCoins .Empty () {
438
- lastReserveRatio := sdk .NewDecFromInt (reserveCoins [0 ].Amount ).Quo (sdk .NewDecFromInt (reserveCoins [1 ].Amount ))
439
- logger := k .Logger (ctx )
440
- logger .Info ("withdraw" , msg , "pool" , pool , "inputs" , inputs , "outputs" , outputs , "reserveCoins" , reserveCoins , "lastReserveRatio" , lastReserveRatio )
494
+
495
+ var lastReserveRatio sdk.Dec
496
+ if reserveCoins .Empty () {
497
+ lastReserveRatio = sdk .ZeroDec ()
498
+ } else {
499
+ lastReserveRatio = sdk .NewDecFromInt (reserveCoins [0 ].Amount ).Quo (sdk .NewDecFromInt (reserveCoins [1 ].Amount ))
441
500
}
501
+ logger := k .Logger (ctx )
502
+ logger .Info ("withdraw" , msg , "pool" , pool , "inputs" , inputs , "outputs" , outputs , "reserveCoins" , reserveCoins , "lastReserveRatio" , lastReserveRatio )
442
503
return nil
443
504
}
444
505
0 commit comments