Skip to content

Commit ef3a973

Browse files
fix(perps): validate TP/SL prices, clear stale config, and block invalid orders cp-7.71.0 (#27791)
## **Description** Fixes three related bugs in the Perps order form involving Take Profit (TP) and Stop Loss (SL) prices that were restored from a previous session's pending trade configuration: 1. **Stale TP/SL persisted after order submission**: The `pendingTradeConfiguration` was not cleared after a successful order, causing previously set TP/SL values to reappear on the next order form visit — even auto-submitting a stop loss the user never intended. 2. **TP/SL displayed as "off" despite being set**: When the RoE calculation clamped to zero (e.g., the TP/SL price was on the wrong side of the current market price), the "Auto close" summary row showed "off" instead of the actual price. The TP/SL edit form, however, showed the correct value — a confusing inconsistency. 3. **No validation or blocking for invalid TP/SL direction**: A restored TP/SL price that ended up on the wrong side of the market (e.g., take profit below entry for a long) was silently accepted and could be submitted, leading to immediate execution or unexpected behavior. ### Changes - Call `clearPendingTradeConfiguration` after successful order execution to prevent stale TP/SL restoration. - Display the formatted price in the "Auto close" row when RoE rounds to zero, instead of showing "off". - Validate TP/SL prices against current market price and trade direction using existing `isValidTakeProfitPrice` / `isValidStopLossPrice` utilities. - Show inline error warnings when TP or SL is on the wrong side of the current price. - Disable the "Place order" button while TP/SL is invalid. ## **Changelog** CHANGELOG entry: Fixed a bug where stale Take Profit and Stop Loss prices could persist across orders and display incorrectly in the Perps order form ## **Related issues** Fixes: #27793 ## **Manual testing steps** ```gherkin Feature: Perps order TP/SL validation Scenario: stale TP/SL is cleared after placing an order Given the user has a Perps position open with TP and SL set And the user navigates to the order form When the user places the order successfully And the user returns to the order form for the same asset Then the TP and SL fields should be empty (not restored from previous order) Scenario: TP/SL on the wrong side shows warning and blocks submission Given the user is on the Perps order form for a Long position And the user sets a Take Profit price below the current market price When the order form validates the TP price Then a warning is displayed: "Take profit must be above current price. Update or clear it to place the order." And the "Place order" button is disabled Scenario: TP/SL on the wrong side for Short position Given the user is on the Perps order form for a Short position And the user sets a Stop Loss price below the current market price When the order form validates the SL price Then a warning is displayed: "Stop loss must be above current price. Update or clear it to place the order." And the "Place order" button is disabled Scenario: TP/SL with zero RoE displays price instead of "off" Given the user is on the Perps order form with a TP or SL set And the TP/SL price results in an RoE that rounds to 0% When the "Auto close" summary row renders Then it displays the formatted price value instead of "off" ``` ## **Screenshots/Recordings** N/A — validation logic and text changes only; no layout or visual design changes. ### **Before** N/A ### **After** <img width="1320" height="2868" alt="Simulator Screenshot - iPhone 17 Pro Max - 2026-03-23 at 11 46 16" src="https://github.com/user-attachments/assets/6c84ef28-0adc-42e2-818e-3fd0d1c9de6e" /> ## **Pre-merge author checklist** - [x] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile Coding Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [x] I've completed the PR template to the best of my ability - [x] I've included tests if applicable - [x] I've documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [x] I've applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots. <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Medium Risk** > Changes the Perps order submission/validation path to block orders when TP/SL is on the wrong side and clears stored pending trade config after submission, which could affect order placement behavior. Risk is mitigated by added unit coverage for market vs limit reference price and button-disabled states. > > **Overview** > Prevents Perps orders from being submitted with **invalid TP/SL trigger prices** by validating TP/SL against the appropriate reference price (*current* for market orders, *entry/limit* for limit orders), showing inline warnings, and disabling the **Place order** button when TP/SL is wrong-side. > > Fixes TP/SL display inconsistencies by showing the formatted TP/SL *price* in the summary row when computed RoE clamps to `0%` instead of rendering `off`, and clears `PerpsController.clearPendingTradeConfiguration(asset)` after successful submission to avoid restoring stale TP/SL on subsequent visits. > > Adds new i18n strings for the wrong-side TP/SL warnings and expands `PerpsOrderView` tests to cover wrong-side validation, limit-order reference pricing, and the monochrome A/B button variant disablement. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 7c10dc8. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent f083ce9 commit ef3a973

3 files changed

Lines changed: 513 additions & 2 deletions

File tree

0 commit comments

Comments
 (0)