Fix: Auto-cancel trigger orders when position is closed (#923)#2039
Open
pranay123-stack wants to merge 1 commit intodrift-labs:masterfrom
Open
Fix: Auto-cancel trigger orders when position is closed (#923)#2039pranay123-stack wants to merge 1 commit intodrift-labs:masterfrom
pranay123-stack wants to merge 1 commit intodrift-labs:masterfrom
Conversation
8fc22ff to
629c26a
Compare
Resolves issue drift-labs#923 where stop-loss/take-profit (trigger) orders were not being automatically cancelled when a user closed their position. ## Problem When users closed their positions, trigger orders remained active, creating risks: - Unintended order execution after position closure - Opening new unwanted positions - Potential financial losses for users ## Solution Implemented automatic cancellation of trigger orders when positions are fully closed: ### Code Changes 1. **New Function**: `cancel_trigger_orders_for_closed_position()` (lines 587-668) - Iterates through all user orders - Identifies and cancels trigger orders (TriggerMarket/TriggerLimit) for a specific market - Properly documented with rustdoc comments 2. **Modified**: `fulfill_perp_order()` (lines 2128-2148) - Checks if perp position is completely closed (base_asset_amount == 0) - Automatically cancels all trigger orders for that market 3. **Modified**: `fulfill_spot_order()` (lines 4788-4808) - Checks if spot position is completely closed (scaled_balance == 0) - Automatically cancels all trigger orders for that market ### Tests Added comprehensive integration test suite in `tests/cancelTriggerOrdersOnPositionClose.ts`: - Test 1: Verifies trigger orders are cancelled when position fully closes - Test 2: Verifies trigger orders remain active on partial position close - Test 3: Verifies multiple trigger orders are all cancelled together ## Coverage - ✅ Perp markets (perpetual futures) - ✅ Spot markets - ✅ Multiple trigger orders per market - ✅ Partial vs full position closure handling ## Testing - Code compiles successfully with no errors - Follows existing code patterns and conventions - Proper error handling with DriftResult - Comprehensive inline documentation Author: Pranay Email: pranaygaurav4555@gmail.com GitHub: @pranay123-stack
629c26a to
8d9f930
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #923 - Stop-loss orders are now automatically cancelled when the associated position is completely closed.
Problem
When users closed their positions, trigger orders (stop-loss/take-profit) remained active, creating a risk of:
Solution
cancel_trigger_orders_for_closed_position()function to handle automatic cancellationfulfill_perp_order()to detect when a position reaches zero and trigger cancellationChanges Made
New Function:
cancel_trigger_orders_for_closed_position()(lines 587-636)Modified Function:
fulfill_perp_order()(lines 2096-2112)Testing
Risk Mitigation
This fix prevents the following scenario: