Skip to content

Commit

Permalink
Merge pull request #713 from neutron-org/fix/negative_lo_withdraw
Browse files Browse the repository at this point in the history
Fix: Add case to handle negative withdraw amounts
  • Loading branch information
pr0n00gler authored Oct 10, 2024
2 parents 4260c8d + 545368f commit 4fec205
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
18 changes: 18 additions & 0 deletions x/dex/keeper/integration_cancellimitorder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,24 @@ func (s *DexTestSuite) TestCancelPartiallyFilled() {
s.Assert().False(found)
}

func (s *DexTestSuite) TestCancelWithdrawThenCancel() {
s.fundAliceBalances(10, 0)
s.fundBobBalances(0, 20)

// GIVEN alice limit sells 10 TokenA
trancheKey := s.aliceLimitSells("TokenA", -6931, 10)
// Bob swaps some TokenB for 5 TokenA
s.bobLimitSellsWithMaxOut("TokenB", 7000, 20, 5)

// WHEN alice withdraws
s.aliceWithdrawsLimitSell(trancheKey)
s.assertAliceBalancesInt(sdkmath.ZeroInt(), sdkmath.NewInt(9999181))

// THEN Alice cancel still works
s.aliceCancelsLimitSell(trancheKey)
s.assertAliceBalancesInt(sdkmath.NewInt(4999999), sdkmath.NewInt(9999181))
}

func (s *DexTestSuite) TestCancelPartiallyFilledWithdrawFails() {
s.fundAliceBalances(50, 0)
s.fundBobBalances(0, 10)
Expand Down
5 changes: 5 additions & 0 deletions x/dex/types/limit_order_tranche.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ func (t *LimitOrderTranche) CalcWithdrawAmount(trancheUser *LimitOrderTrancheUse
ratioFilled := t.RatioFilled()
maxAllowedToWithdraw := ratioFilled.MulInt(trancheUser.SharesOwned)
sharesToWithdrawDec := maxAllowedToWithdraw.Sub(math_utils.NewPrecDecFromInt(trancheUser.SharesWithdrawn))

// Given rounding it is possible for sharesToWithdrawn > maxAllowedToWithdraw. In this case we just exit.
if !sharesToWithdrawDec.IsPositive() {
return math.ZeroInt(), math.ZeroInt()
}
amountOutTokenOutDec := sharesToWithdrawDec.Quo(t.PriceTakerToMaker)

// Round shares withdrawn up and amountOut down to ensure math favors dex
Expand Down

0 comments on commit 4fec205

Please sign in to comment.