Skip to content

Commit

Permalink
Merge pull request #659 from neutron-org/v4.x_lo_fix
Browse files Browse the repository at this point in the history
FIX: LO cancellation accounting
  • Loading branch information
pr0n00gler authored Aug 8, 2024
2 parents 34cf14b + a249561 commit 01b71f7
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
9 changes: 6 additions & 3 deletions x/dex/keeper/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,11 +512,14 @@ func (k Keeper) CancelLimitOrderCore(
makerAmountToReturn := tranche.RemoveTokenIn(trancheUser)
_, takerAmountOut := tranche.Withdraw(trancheUser)

trancheUser.SharesWithdrawn = trancheUser.SharesOwned

// Remove the canceled shares from the limitOrder
tranche.TotalMakerDenom = tranche.TotalMakerDenom.Sub(trancheUser.SharesOwned)
tranche.TotalTakerDenom = tranche.TotalTakerDenom.Sub(takerAmountOut)
sharesWithdrawnTakerDenom := math_utils.NewPrecDecFromInt(trancheUser.SharesWithdrawn).
Quo(tranche.PriceTakerToMaker).
TruncateInt()
totalAmountOut := sharesWithdrawnTakerDenom.Add(takerAmountOut)
tranche.TotalTakerDenom = tranche.TotalTakerDenom.Sub(totalAmountOut)
trancheUser.SharesWithdrawn = trancheUser.SharesOwned

if makerAmountToReturn.IsPositive() || takerAmountOut.IsPositive() {
makerCoinOut := sdk.NewCoin(tradePairID.MakerDenom, makerAmountToReturn)
Expand Down
64 changes: 64 additions & 0 deletions x/dex/keeper/integration_cancellimitorder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,3 +384,67 @@ func (s *DexTestSuite) TestCancelJITNextBlock() {
s.aliceCancelsLimitSellFails(trancheKey, types.ErrActiveLimitOrderNotFound)
s.assertAliceBalances(0, 0)
}

func (s *DexTestSuite) TestWithdrawThenCancel() {
s.fundAliceBalances(50, 0)
s.fundBobBalances(50, 0)
s.fundCarolBalances(0, 40)

// // GIVEN alice and bob each limit sells 50 TokenA
trancheKey := s.aliceLimitSells("TokenA", 0, 50)
s.bobLimitSells("TokenA", 0, 50)

s.carolLimitSells("TokenB", -1, 10, types.LimitOrderType_FILL_OR_KILL)

// WHEN alice withdraws and cancels her limit order
s.aliceWithdrawsLimitSell(trancheKey)
s.aliceCancelsLimitSell(trancheKey)
s.assertAliceBalances(45, 5)

s.bobWithdrawsLimitSell(trancheKey)
s.assertBobBalances(0, 5)
s.bobCancelsLimitSell(trancheKey)
s.assertBobBalances(45, 5)
}

func (s *DexTestSuite) TestWithdrawThenCancel2() {
s.fundAliceBalances(50, 0)
s.fundBobBalances(50, 0)
s.fundCarolBalances(0, 40)

// // GIVEN alice and bob each limit sells 50 TokenA
trancheKey := s.aliceLimitSells("TokenA", 0, 50)
s.bobLimitSells("TokenA", 0, 50)

s.carolLimitSells("TokenB", -1, 10, types.LimitOrderType_FILL_OR_KILL)

// WHEN alice withdraws and cancels her limit order
s.aliceWithdrawsLimitSell(trancheKey)
s.aliceCancelsLimitSell(trancheKey)
s.assertAliceBalances(45, 5)

s.bobCancelsLimitSell(trancheKey)
s.assertBobBalances(45, 5)
}

func (s *DexTestSuite) TestWithdrawThenCancelLowTick() {
s.fundAliceBalances(50, 0)
s.fundBobBalances(50, 0)
s.fundCarolBalances(0, 40)

// // GIVEN alice and bob each limit sells 50 TokenA
trancheKey := s.aliceLimitSells("TokenA", 20000, 50)
s.bobLimitSells("TokenA", 20000, 50)

s.carolLimitSells("TokenB", -20001, 10, types.LimitOrderType_FILL_OR_KILL)

// WHEN alice withdraws and cancels her limit order
s.aliceWithdrawsLimitSell(trancheKey)
s.aliceCancelsLimitSell(trancheKey)
s.assertAliceBalancesInt(sdkmath.NewInt(13058413), sdkmath.NewInt(4999999))

s.bobWithdrawsLimitSell(trancheKey)
s.assertBobBalancesInt(sdkmath.ZeroInt(), sdkmath.NewInt(4999999))
s.bobCancelsLimitSell(trancheKey)
s.assertBobBalancesInt(sdkmath.NewInt(13058413), sdkmath.NewInt(4999999))
}

0 comments on commit 01b71f7

Please sign in to comment.