@@ -532,9 +532,9 @@ func (s *DexTestSuite) TestWithdrawThenCancelLowTick() {
532532 s .assertAliceBalancesInt (sdkmath .NewInt (13058413 ), sdkmath .NewInt (4999999 ))
533533
534534 s .bobWithdrawsLimitSell (trancheKey )
535- s .assertBobBalancesInt ( sdkmath . ZeroInt (), sdkmath . NewInt ( 4999999 ) )
535+ s .assertBobBalances ( 0 , 5 )
536536 s .bobCancelsLimitSell (trancheKey )
537- s .assertBobBalancesInt (sdkmath .NewInt (13058413 ), sdkmath .NewInt (4999999 ))
537+ s .assertBobBalancesInt (sdkmath .NewInt (13058413 ), sdkmath .NewInt (5_000_000 ))
538538}
539539
540540func (s * DexTestSuite ) TestWrongSharesProtectionCancel () {
@@ -553,3 +553,51 @@ func (s *DexTestSuite) TestWrongSharesProtectionCancel() {
553553
554554 s .assertAliceBalances (1 , 0 )
555555}
556+
557+ func (s * DexTestSuite ) TestCanceLimitOrderClearsPosition () {
558+ s .fundAccountBalancesInt (s .alice , sdkmath .NewInt (10_000 ), sdkmath .ZeroInt ())
559+ s .fundAccountBalancesInt (s .bob , sdkmath .NewInt (500_000 ), sdkmath .ZeroInt ())
560+ s .fundAccountBalancesInt (s .carol , sdkmath .ZeroInt (), sdkmath .NewInt (22_015 ))
561+
562+ // GIVEN alice and bob place GTC limit sells at tick -100000
563+ trancheKey := s .limitSellsIntSuccess (s .alice , "TokenA" , - 100000 , sdkmath .NewInt (10_000 ))
564+ s .limitSellsIntSuccess (s .bob , "TokenA" , - 100000 , sdkmath .NewInt (500_000 ))
565+
566+ // AND carol swaps 22,015 TokenB for TokeA
567+ s .limitSellsIntSuccess (s .carol , "TokenB" , - 100001 , sdkmath .NewInt (22_015 ), types .LimitOrderType_FILL_OR_KILL )
568+
569+ // WHEN Alice withdraws then cancels
570+ s .aliceWithdrawsLimitSell (trancheKey )
571+ s .aliceCancelsLimitSell (trancheKey )
572+
573+ // THEN totalTakerDenom == ReservesTakerDenom
574+ tranche , _ , found := s .App .DexKeeper .FindLimitOrderTranche (
575+ s .Ctx ,
576+ & types.LimitOrderTrancheKey {
577+ TradePairId : types .MustNewTradePairID ("TokenB" , "TokenA" ),
578+ TickIndexTakerToMaker : 100000 ,
579+ TrancheKey : trancheKey ,
580+ },
581+ )
582+ s .True (found )
583+
584+ s .Equal (tranche .DecTotalTakerDenom , tranche .DecReservesTakerDenom )
585+
586+ // WHEN bob cancels his limit order, the tranche only contains dust
587+ s .bobCancelsLimitSell (trancheKey )
588+
589+ // Final tranche state
590+ tranche , _ , found = s .App .DexKeeper .FindLimitOrderTranche (
591+ s .Ctx ,
592+ & types.LimitOrderTrancheKey {
593+ TradePairId : types .MustNewTradePairID ("TokenB" , "TokenA" ),
594+ TickIndexTakerToMaker : 100000 ,
595+ TrancheKey : trancheKey ,
596+ },
597+ )
598+ s .True (found )
599+
600+ // NOTE: we are using the Int fields just to ensure only dust is left
601+ s .Assert ().Equal (tranche .ReservesTakerDenom , sdkmath .ZeroInt ())
602+ s .Assert ().Equal (tranche .TotalTakerDenom , sdkmath .ZeroInt ())
603+ }
0 commit comments