fix(perps): validate TP/SL prices, clear stale config, and block invalid orders cp-7.71.0#27791
Conversation
…of market When a restored TP/SL price ends up on the wrong side of the current market price (e.g. take profit below entry for a long), show a warning and disable the Place Order button. Also display the formatted price instead of "off" when the RoE calculation clamps to zero.
|
CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes. |
Cover the new TP/SL validation logic, warning UI, and button disabled state for both long/short positions and monochrome button A/B variant.
| // stale TP/SL values from being restored on the next order form visit | ||
| Engine.context.PerpsController?.clearPendingTradeConfiguration( | ||
| orderForm.asset, | ||
| ); |
There was a problem hiding this comment.
Pending config cleared even when order execution fails
Medium Severity
clearPendingTradeConfiguration runs unconditionally after executeOrder, but executeOrder (from usePerpsOrderExecution) never throws — it catches all errors internally and resolves regardless of success or failure. This means the pending trade config (including TP/SL) is cleared even when the order fails on the exchange, contradicting the comment's stated intent of clearing only "after successful submission." If a user's order fails, their TP/SL settings are lost when they return to the order form.
There was a problem hiding this comment.
This is intended
…s tests Define RelatedAsset locally since @metamask/ai-controllers no longer exports it, and simplify the usePerpsABTest mock to avoid spreading unknown[] into a rest parameter.
geositta
left a comment
There was a problem hiding this comment.
I added a suggestion regarding the bugbot finding. Please add a test that fails on current code and passes after the fix, proving limit order TP/SL is validated against entry price when entry and market prices differ.
…t price TP/SL wrong-side validation was comparing against the current market price for all order types. For limit orders the reference should be the limit (entry) price, matching the pattern already used for liquidation price and TP/SL summary display in the same file.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
There are 2 total unresolved issues (including 1 from previous review).
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.
The warning strings hardcoded "current price" but for limit orders the
validation runs against the entry (limit) price. Pass a dynamic priceType
param ("current" or "entry") matching the existing pattern in usePerpsTPSLForm.
Replace raw English strings ("current", "entry") with proper i18n calls
via strings('perps.tpsl.current') and strings('perps.tpsl.entry') in both
PerpsOrderView and usePerpsTPSLForm, ensuring the values are localizable.
…nings" This reverts commit c85cf59.
🔍 Smart E2E Test Selection
click to see 🤖 AI reasoning detailsE2E Test Selection:
Tag selection rationale:
The changes are contained within the Perps feature area with no impact on shared navigation, modals, or other wallet components. The localization changes are Perps-specific strings only. Performance Test Selection: |
|
✅ E2E Fixture Validation — Schema is up to date |
|
geositta
left a comment
There was a problem hiding this comment.
Thanks for adding that limit order validation - looks good from my end.





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:
Stale TP/SL persisted after order submission: The
pendingTradeConfigurationwas 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.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.
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
clearPendingTradeConfigurationafter successful order execution to prevent stale TP/SL restoration.isValidTakeProfitPrice/isValidStopLossPriceutilities.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
Screenshots/Recordings
N/A — validation logic and text changes only; no layout or visual design changes.
Before
N/A
After
Pre-merge author checklist
Pre-merge reviewer checklist
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 renderingoff, and clearsPerpsController.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
PerpsOrderViewtests to cover wrong-side validation, limit-order reference pricing, and the monochrome A/B button variant disablement.Written by Cursor Bugbot for commit 7c10dc8. This will update automatically on new commits. Configure here.