Skip to content

Conversation

@runway-github
Copy link
Contributor

@runway-github runway-github bot commented Oct 29, 2025

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,
TAT-1913

Manual testing steps

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
Simulator.Screen.Recording.-.iPhone.16.Pro.-.2025-10-28.at.16.31.27.mp4

Pre-merge author checklist

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.

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.

  • UI (TP/SL View):
    • Dismiss keypad before clearing TP/SL via handleTakeProfitClear/handleStopLossClear; remove focus-based disable on Clear buttons in PerpsTPSLView.tsx.
    • onConfirm now passes trackingData {direction, source: 'tp_sl_view', positionSize}.
  • Form Logic:
    • usePerpsTPSLForm: validate against current price for existing positions; use entry for new limit orders; memoized referencePrice/priceType.
  • Controller & Analytics:
    • PerpsController.updatePositionTPSL: refactor to track one consolidated PERPS_RISK_MANAGEMENT event in finally with status, asset, completion_duration, source, optional direction, position_size, TP/SL prices, and error.
    • Add PerpsEventValues.SOURCE.TP_SL_VIEW and update event reference docs.
  • Types & Navigation:
    • Introduce TPSLTrackingData; add trackingData to UpdatePositionTPSLParams and PerpsNavigationParamList.PerpsTPSL.onConfirm.
  • Position Card & Update Hook:
    • Pass trackingData through PerpsPositionCardusePerpsTPSLUpdate → controller.
  • Tests:
    • Update and add tests for keyboard dismissal on Clear, onConfirm with trackingData, 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

#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]>
@runway-github runway-github bot requested a review from a team as a code owner October 29, 2025 00:11
@github-actions
Copy link
Contributor

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.

@metamaskbot metamaskbot added the team-bots Bot team (for MetaMask Bot, Runway Bot, etc.) label Oct 29, 2025
@sonarqubecloud
Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
77.8% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

Copy link
Contributor

@Cal-L Cal-L left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Cal-L Cal-L merged commit 99ac2a4 into release/7.58.0 Oct 29, 2025
84 of 89 checks passed
@Cal-L Cal-L deleted the runway-cherry-pick-7.58.0-1761696681 branch October 29, 2025 06:55
@github-actions github-actions bot locked and limited conversation to collaborators Oct 29, 2025
@metamaskbot metamaskbot added the release-7.58.0 Issue or pull request that will be included in release 7.58.0 label Oct 29, 2025
@metamaskbot
Copy link
Collaborator

No release label on PR. Adding release label release-7.58.0 on PR, as PR was cherry-picked in branch 7.58.0.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

release-7.58.0 Issue or pull request that will be included in release 7.58.0 size-M team-bots Bot team (for MetaMask Bot, Runway Bot, etc.)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants