Commit 8d9f930
Fix: Auto-cancel trigger orders when position is closed (#923)
Resolves issue #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-stack1 parent c74d5e9 commit 8d9f930
File tree
2 files changed
+580
-0
lines changed- programs/drift/src/controller
- tests
2 files changed
+580
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
584 | 584 | | |
585 | 585 | | |
586 | 586 | | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
587 | 670 | | |
588 | 671 | | |
589 | 672 | | |
| |||
2042 | 2125 | | |
2043 | 2126 | | |
2044 | 2127 | | |
| 2128 | + | |
| 2129 | + | |
| 2130 | + | |
| 2131 | + | |
| 2132 | + | |
| 2133 | + | |
| 2134 | + | |
| 2135 | + | |
| 2136 | + | |
| 2137 | + | |
| 2138 | + | |
| 2139 | + | |
| 2140 | + | |
| 2141 | + | |
| 2142 | + | |
| 2143 | + | |
| 2144 | + | |
| 2145 | + | |
| 2146 | + | |
| 2147 | + | |
| 2148 | + | |
| 2149 | + | |
2045 | 2150 | | |
2046 | 2151 | | |
2047 | 2152 | | |
| |||
4680 | 4785 | | |
4681 | 4786 | | |
4682 | 4787 | | |
| 4788 | + | |
| 4789 | + | |
| 4790 | + | |
| 4791 | + | |
| 4792 | + | |
| 4793 | + | |
| 4794 | + | |
| 4795 | + | |
| 4796 | + | |
| 4797 | + | |
| 4798 | + | |
| 4799 | + | |
| 4800 | + | |
| 4801 | + | |
| 4802 | + | |
| 4803 | + | |
| 4804 | + | |
| 4805 | + | |
| 4806 | + | |
| 4807 | + | |
| 4808 | + | |
| 4809 | + | |
4683 | 4810 | | |
4684 | 4811 | | |
4685 | 4812 | | |
| |||
0 commit comments