Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEAT: Allow cancel inactive limit orders #711

Merged
merged 7 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions x/dex/keeper/cancel_limit_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,20 @@ func (k Keeper) ExecuteCancelLimitOrder(
) (makerCoinOut, takerCoinOut sdk.Coin, tradePairID *types.TradePairID, error error) {
trancheUser, found := k.GetLimitOrderTrancheUser(ctx, callerAddr.String(), trancheKey)
if !found {
return sdk.Coin{}, sdk.Coin{}, nil, types.ErrActiveLimitOrderNotFound
return sdk.Coin{}, sdk.Coin{}, nil, types.ErrValidLimitOrderTrancheNotFound
sotnikov-s marked this conversation as resolved.
Show resolved Hide resolved
}

tradePairID, tickIndex := trancheUser.TradePairId, trancheUser.TickIndexTakerToMaker
tranche := k.GetLimitOrderTranche(
tranche, wasFilled, found := k.FindLimitOrderTranche(
ctx,
&types.LimitOrderTrancheKey{
TradePairId: tradePairID,
TickIndexTakerToMaker: tickIndex,
TrancheKey: trancheKey,
},
)
if tranche == nil {
return sdk.Coin{}, sdk.Coin{}, nil, types.ErrActiveLimitOrderNotFound
if !found {
return sdk.Coin{}, sdk.Coin{}, nil, types.ErrValidLimitOrderTrancheNotFound
}

makerAmountToReturn := tranche.RemoveTokenIn(trancheUser)
Expand All @@ -89,7 +89,11 @@ func (k Keeper) ExecuteCancelLimitOrder(
}

k.SaveTrancheUser(ctx, trancheUser)
k.SaveTranche(ctx, tranche)
if wasFilled {
k.SaveInactiveTranche(ctx, tranche)
} else {
k.SaveTranche(ctx, tranche)
}

if trancheUser.OrderType.HasExpiration() {
k.RemoveLimitOrderExpiration(ctx, *tranche.ExpirationTime, tranche.Key.KeyMarshal())
Expand Down
72 changes: 65 additions & 7 deletions x/dex/keeper/integration_cancellimitorder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (s *DexTestSuite) TestCancelTwiceFails() {
s.assertAliceBalances(50, 50)
s.assertDexBalances(0, 0)

s.aliceCancelsLimitSellFails(trancheKey, types.ErrActiveLimitOrderNotFound)
s.aliceCancelsLimitSellFails(trancheKey, types.ErrValidLimitOrderTrancheNotFound)
}

func (s *DexTestSuite) TestCancelPartiallyFilled() {
Expand Down Expand Up @@ -323,6 +323,43 @@ func (s *DexTestSuite) TestCancelFirstMultiWithdraw() {
s.assertAliceBalances(0, 10)
}

func (s *DexTestSuite) TestCancelMultiAfterFilled() {
s.fundAliceBalances(50, 0)
s.fundBobBalances(50, 0)
s.fundCarolBalances(0, 100)

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

// carol swaps through the tranche
s.carolLimitSells("TokenB", -1, 100, types.LimitOrderType_IMMEDIATE_OR_CANCEL)

// WHEN alice and bob cancel their limit order
s.aliceCancelsLimitSell(trancheKey)
s.assertAliceBalances(0, 50)
s.bobCancelsLimitSell(trancheKey)
s.assertBobBalances(0, 50)

// THEN they get back the expected profit
s.assertAliceBalances(0, 50)
s.assertBobBalances(0, 50)

// AND tranche and trancheUsers are deleted

s.App.DexKeeper.GetLimitOrderTrancheUser(s.Ctx, s.alice.String(), trancheKey)
_, _, found := s.App.DexKeeper.FindLimitOrderTranche(s.Ctx, &types.LimitOrderTrancheKey{
TradePairId: types.MustNewTradePairID("TokenB", "TokenA"),
TickIndexTakerToMaker: 0,
TrancheKey: trancheKey,
})
s.False(found)
_, found = s.App.DexKeeper.GetLimitOrderTrancheUser(s.Ctx, s.alice.String(), trancheKey)
s.Assert().False(found)
_, found = s.App.DexKeeper.GetLimitOrderTrancheUser(s.Ctx, s.carol.String(), trancheKey)
s.Assert().False(found)
}

func (s *DexTestSuite) TestCancelGoodTil() {
s.fundAliceBalances(50, 0)
tomorrow := time.Now().AddDate(0, 0, 1)
Expand All @@ -339,7 +376,7 @@ func (s *DexTestSuite) TestCancelGoodTil() {
s.assertNLimitOrderExpiration(0)
}

func (s *DexTestSuite) TestCancelGoodTilAfterExpirationFails() {
func (s *DexTestSuite) TestCancelGoodTilAfterExpiration() {
s.fundAliceBalances(50, 0)
tomorrow := time.Now().AddDate(0, 0, 1)
// GIVEN alice limit sells 50 TokenA with goodTil date of tommrow
Expand All @@ -350,8 +387,19 @@ func (s *DexTestSuite) TestCancelGoodTilAfterExpirationFails() {
// WHEN expiration date has passed
s.beginBlockWithTime(time.Now().AddDate(0, 0, 2))

// THEN alice cancellation fails
s.aliceCancelsLimitSellFails(trancheKey, types.ErrActiveLimitOrderNotFound)
// THEN alice cancellation succeeds
s.aliceCancelsLimitSell(trancheKey)

s.assertAliceBalances(50, 0)

// TrancheUser and Tranche are removed
s.App.DexKeeper.GetLimitOrderTrancheUser(s.Ctx, s.alice.String(), trancheKey)
_, _, found := s.App.DexKeeper.FindLimitOrderTranche(s.Ctx, &types.LimitOrderTrancheKey{
TradePairId: types.MustNewTradePairID("TokenB", "TokenA"),
TickIndexTakerToMaker: 0,
TrancheKey: trancheKey,
})
s.False(found)
}

func (s *DexTestSuite) TestCancelJITSameBlock() {
Expand Down Expand Up @@ -380,7 +428,17 @@ func (s *DexTestSuite) TestCancelJITNextBlock() {
s.nextBlockWithTime(time.Now())
s.beginBlockWithTime(time.Now())

// THEN alice cancellation fails
s.aliceCancelsLimitSellFails(trancheKey, types.ErrActiveLimitOrderNotFound)
s.assertAliceBalances(0, 0)
// THEN alice cancellation succeeds
s.aliceCancelsLimitSell(trancheKey)

s.assertAliceBalances(50, 0)

// TrancheUser and Tranche are removed
s.App.DexKeeper.GetLimitOrderTrancheUser(s.Ctx, s.alice.String(), trancheKey)
_, _, found := s.App.DexKeeper.FindLimitOrderTranche(s.Ctx, &types.LimitOrderTrancheKey{
TradePairId: types.MustNewTradePairID("TokenB", "TokenA"),
TickIndexTakerToMaker: 0,
TrancheKey: trancheKey,
})
s.False(found)
}
5 changes: 0 additions & 5 deletions x/dex/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,6 @@ var (
1125,
"MaxAmountIn in must be > 0 for swap.",
)
ErrActiveLimitOrderNotFound = sdkerrors.Register(
ModuleName,
1128,
"No active limit found. It does not exist or has already been filled",
)
ErrZeroWithdraw = sdkerrors.Register(
ModuleName,
1129,
Expand Down
Loading