-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
chore(runway): cherry-pick fix(perps): cp-7.58.0 invalid tpsl behavior #21832
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
Conversation
#21759) ## **Description** This PR fixes three issues with the TP/SL (Take Profit/Stop Loss) functionality in the Perps trading feature: 1. **TAT-1911**: Fixed "Clear" button being unresponsive when keyboard is visible 2. **TAT-1913**: Fixed validation that prevented setting TP/SL between entry and current price for positions at a loss 3. **Analytics Enhancement**: Added missing properties (`direction`, `source`, `position_size`) to `PERPS_RISK_MANAGEMENT` MetaMetrics event **Key improvements:** - Keyboard now dismisses before clearing TP/SL values, ensuring button responsiveness - Validation logic now uses current price (instead of entry price) as reference for existing positions, enabling TP/SL to be set between entry and current price when position is at a loss (still allows setting above/below current price as normal) - Analytics events now include complete tracking data following the existing `trackingData` pattern to avoid duplicate API calls - Refactored MetaMetrics tracking to build event properties once in finally block (similar to Sentry traces pattern) - Extracted `TPSLTrackingData` type to eliminate type duplication across multiple files ## **Changelog** CHANGELOG entry: Fixed TP/SL Clear button responsiveness and validation for positions at a loss ## **Related issues** Fixes: [TAT-1911](https://consensyssoftware.atlassian.net/browse/TAT-1911), [TAT-1913](https://consensyssoftware.atlassian.net/browse/TAT-1913) ## **Manual testing steps** ```gherkin Feature: TP/SL Clear Button Scenario: user clears TP/SL with keyboard visible Given user is on TP/SL page And keyboard is visible from editing a TP/SL field When user taps "Clear" button Then keyboard dismisses And TP/SL values are cleared Feature: TP/SL Validation for Positions at Loss Scenario: user sets TP/SL between entry and current price Given user has an open long BTC position And position is at -5% loss (current price below entry) When user navigates to Edit TP/SL And sets TP price between current price and entry price Then TP is accepted without validation error And TP order is successfully placed Feature: Risk Management Analytics Scenario: user updates position TP/SL Given user has an open position When user updates TP/SL from position card Then PERPS_RISK_MANAGEMENT event includes direction, source, and position_size ``` ## **Screenshots/Recordings** ### **Before** - Clear button unresponsive when keyboard is up - Validation error preventing TP/SL between entry and current price for losing positions - Analytics missing key filtering properties ### **After** - Clear button dismisses keyboard and clears values - TP/SL can be set between entry and current price - Complete analytics data captured https://github.com/user-attachments/assets/7c6436cf-4c39-44e6-b413-85d45cce2f42 ## **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] > Dismiss keyboard before TP/SL clear, validate TP/SL against current price for existing positions, and add/pass tracking data (direction, source, position_size) to PERPS_RISK_MANAGEMENT. > > - **UI (PerpsTPSLView)**: > - Dismisses keypad before clearing TP/SL via new `handleTakeProfitClear`/`handleStopLossClear`; remove focus-based disable on Clear buttons. > - `handleConfirm` now passes `trackingData` `{ direction, source: 'tp_sl_view', positionSize }` to `onConfirm`. > - **Hooks**: > - `usePerpsTPSLForm`: validation refactor to use `currentPrice` as reference for existing positions; `useMemo` for `referencePrice`/`priceType`. > - `usePerpsTPSLUpdate`: plumbs optional `trackingData` through `updatePositionTPSL`. > - **Navigation & Components**: > - `PerpsPositionCard` -> TPSL `onConfirm` signature updated to `(tp?, sl?, trackingData?)` and forwards tracking data. > - **Controller (PerpsController.updatePositionTPSL)**: > - Accepts optional `trackingData`; builds and emits a single `PERPS_RISK_MANAGEMENT` event in `finally` with `status`, `asset`, `completion_duration`, `source`, optional `direction`, `position_size`, `take_profit_price`, `stop_loss_price`, and `error_message`. > - **Analytics constants/docs**: > - Adds `PerpsEventValues.SOURCE.TP_SL_VIEW` and documents `source` in PERPS_RISK_MANAGEMENT. > - **Types**: > - Adds `TPSLTrackingData`; extends `UpdatePositionTPSLParams` and navigation `onConfirm` to include `trackingData`. > - **Tests**: > - Update TPSL view tests to assert keyboard dismissal on Clear and tracking data in `onConfirm`; adjust expectations for new validation logic. > - Streamline/use `act` where needed and update test names/expectations accordingly. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 0409ed4. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY --> [TAT-1911]: https://consensyssoftware.atlassian.net/browse/TAT-1911?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ --------- Co-authored-by: Nick Gambino <[email protected]>
|
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. |
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
|
No release label on PR. Adding release label release-7.58.0 on PR, as PR was cherry-picked in branch 7.58.0. |


Description
This PR fixes three issues with the TP/SL (Take Profit/Stop Loss)
functionality in the Perps trading feature:
is visible
entry and current price for positions at a loss
direction,source,position_size) toPERPS_RISK_MANAGEMENTMetaMetrics eventKey improvements:
responsiveness
reference for existing positions, enabling TP/SL to be set between entry
and current price when position is at a loss (still allows setting
above/below current price as normal)
existing
trackingDatapattern to avoid duplicate API callsfinally block (similar to Sentry traces pattern)
TPSLTrackingDatatype to eliminate type duplication acrossmultiple files
Changelog
CHANGELOG entry: Fixed TP/SL Clear button responsiveness and validation
for positions at a loss
Related issues
Fixes:
TAT-1911,
TAT-1913
Manual testing steps
Screenshots/Recordings
Before
losing positions
After
Simulator.Screen.Recording.-.iPhone.16.Pro.-.2025-10-28.at.16.31.27.mp4
Pre-merge author checklist
Docs and MetaMask Mobile
Coding
Standards.
if applicable
guidelines).
Not required for external contributors.
Pre-merge reviewer checklist
app, test code being changed).
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
Note
Dismisses keyboard before clearing TP/SL, validates TP/SL against current price for existing positions, and enriches PERPS_RISK_MANAGEMENT analytics with direction/source/position_size.
handleTakeProfitClear/handleStopLossClear; remove focus-based disable on Clear buttons inPerpsTPSLView.tsx.onConfirmnow passestrackingData{direction,source: 'tp_sl_view',positionSize}.usePerpsTPSLForm: validate againstcurrent pricefor existing positions; useentryfor new limit orders; memoizedreferencePrice/priceType.PerpsController.updatePositionTPSL: refactor to track one consolidatedPERPS_RISK_MANAGEMENTevent infinallywithstatus,asset,completion_duration,source, optionaldirection,position_size, TP/SL prices, and error.PerpsEventValues.SOURCE.TP_SL_VIEWand update event reference docs.TPSLTrackingData; addtrackingDatatoUpdatePositionTPSLParamsandPerpsNavigationParamList.PerpsTPSL.onConfirm.trackingDatathroughPerpsPositionCard→usePerpsTPSLUpdate→ controller.onConfirmwithtrackingData, and new validation behavior.Written by Cursor Bugbot for commit 251ffd4. This will update automatically on new commits. Configure here.
Co-authored-by: Nick Gambino [email protected] d5f9885