Skip to content

Conversation

@runway-github
Copy link
Contributor

@runway-github runway-github bot commented Dec 5, 2025

Description

This PR fixes a bug where the Stop Loss banner was not displaying at the
correct Return on Equity (ROE) threshold, and adds an enhancement to
bypass the debounce delay for established positions.

Changelog

CHANGELOG entry: Fixed Stop Loss banner to display at -10% ROE instead
of -20% ROE and skip debounce for old orders

Related issues

Jira issue: https://consensyssoftware.atlassian.net/browse/TAT-2163
Fixes #23473

Manual testing steps

Feature: Stop Loss Banner Display

  Scenario: user has position with -10% ROE
    Given user has an open Perps position
    And position ROE is between -10% and -20%
    And liquidation distance is greater than 3%
    And position has no existing stop loss set
    And ROE has been below -10% for at least 60 seconds

    When user views the position details
    Then Stop Loss prompt banner should be displayed
    And banner should suggest setting a stop loss

Screenshots/Recordings

Before

N/A

After

N/A

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

Updates stop-loss prompt to trigger at -10% ROE and skip debounce for positions >2 minutes old using order fills; wires view to provide timestamp and adds tests.

  • Perps Stop-Loss Prompt:
    • Update STOP_LOSS_PROMPT_CONFIG.ROE_THRESHOLD to -10 in constants/perpsConfig.ts.
    • Enhance useStopLossPrompt to accept positionOpenedTimestamp, bypassing ROE debounce when position age ≥2 minutes and below threshold; minor refactor with finishDebounce and show-once guard.
  • Market Details View:
    • Fetch order fills via usePerpsOrderFills and compute positionOpenedTimestamp (most recent matching Open fill) in PerpsMarketDetailsView.tsx.
    • Pass computed positionOpenedTimestamp into useStopLossPrompt.
  • Tests:
    • Add tests for position opened timestamp derivation in PerpsMarketDetailsView.test.tsx.
    • Adjust and expand useStopLossPrompt.test.ts for -10% ROE threshold and debounce-bypass scenarios.

Written by Cursor Bugbot for commit 9cdf4b7. This will update automatically on new commits. Configure here.

3a558e8

…hreshold to -10% and skip debounce for old orders cp-7.61.0 (#23427)

## **Description**

This PR fixes a bug where the Stop Loss banner was not displaying at the
correct Return on Equity (ROE) threshold, and adds an enhancement to
bypass the debounce delay for established positions.

## **Changelog**

CHANGELOG entry: Fixed Stop Loss banner to display at -10% ROE instead
of -20% ROE and skip debounce for old orders


## **Related issues**

Jira issue: https://consensyssoftware.atlassian.net/browse/TAT-2163
Fixes #23473

## **Manual testing steps**

```gherkin
Feature: Stop Loss Banner Display

  Scenario: user has position with -10% ROE
    Given user has an open Perps position
    And position ROE is between -10% and -20%
    And liquidation distance is greater than 3%
    And position has no existing stop loss set
    And ROE has been below -10% for at least 60 seconds

    When user views the position details
    Then Stop Loss prompt banner should be displayed
    And banner should suggest setting a stop loss
```

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**
N/A

<!-- [screenshots/recordings] -->

### **After**
N/A

<!-- [screenshots/recordings] -->

## **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]
> Stop-loss prompt now triggers at -10% ROE and skips debounce for
positions >2 minutes old using order fills; integrated into market
details view with updated tests.
> 
> - **Perps Stop-Loss Prompt Logic**:
> - Update `STOP_LOSS_PROMPT_CONFIG.ROE_THRESHOLD` to `-10` in
`constants/perpsConfig.ts`.
>   - Enhance `useStopLossPrompt`:
> - Add `positionOpenedTimestamp` param and bypass 60s debounce when ROE
below threshold and position age ≥ 2 minutes.
> - Add `finishDebounce` helper and `hasBeenShownRef` to show once per
position lifecycle; reset appropriately.
> - **View Integration**:
> - In `PerpsMarketDetailsView.tsx`, fetch fills via
`usePerpsOrderFills`, compute `positionOpenedTimestamp` from latest
matching "Open" fill, and pass to `useStopLossPrompt`.
> - **Tests**:
> - Extend `useStopLossPrompt.test.ts` for new threshold and
debounce-bypass behavior, including edge cases and priority rules.
> - In `PerpsMarketDetailsView.test.tsx`, mock `usePerpsOrderFills` and
add test verifying position opened timestamp computation.
> 
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
cf3a2a9. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
@runway-github runway-github bot requested a review from a team as a code owner December 5, 2025 12:20
@github-actions
Copy link
Contributor

github-actions bot commented Dec 5, 2025

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 Dec 5, 2025
@github-actions github-actions bot added the size-M label Dec 5, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Dec 5, 2025

🔍 Smart E2E Test Selection

  • Selected E2E tags: SmokePerps
  • Risk Level: medium
  • AI Confidence: 95%
click to see 🤖 AI reasoning details

All changes are isolated to the Perps (Perpetuals trading) feature area:

  1. Scope: Changes affect stop-loss prompt behavior, chart display, and timing logic for showing user warnings
  2. Components Modified:
    • useStopLossPrompt hook: Added position age-based debounce bypass logic
    • PerpsMarketDetailsView: Integrated order fills to get accurate position timestamps
    • perpsConfig: Changed ROE threshold from -20% to -10% (more sensitive)
  3. User Impact: These changes affect how and when users see stop-loss prompts during perpetuals trading
  4. Test Coverage: Comprehensive unit tests added (8 new test cases for timestamp logic), all existing tests updated
  5. No Cross-Cutting Concerns: Changes are entirely within the Perps module with no dependencies on core wallet, accounts, network, or other major systems

Why Medium Risk: While isolated, this changes user-facing behavior around financial risk management (stop-loss prompts). The threshold change (from -20% to -10% ROE) means users will see warnings earlier, and the debounce bypass means existing positions show warnings faster. This warrants testing the actual perps flow.

Why only SmokePerps: The changes don't touch accounts, core wallet, network switching, swaps, or any other feature areas. All modifications are within app/components/UI/Perps/ directory. The SmokePerps tag should cover all perpetuals trading flows including position management and UI interactions.

View GitHub Actions results


useEffect(() => {
hasBeenShownRef.current = false;
}, [position?.coin]);
Copy link

Choose a reason for hiding this comment

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

Bug: Missing state reset on market change causes premature banner

When switching between markets (position?.coin changes), only hasBeenShownRef is reset while roeDebounceComplete and roeBelowThresholdSinceRef are not. If a user was viewing a market where the debounce completed and the banner was showing, then switches to a different market that also has a position with ROE below threshold, the banner will immediately appear without respecting the debounce logic for the new market. The roeDebounceComplete state from the previous market persists and triggers the banner prematurely.

Fix in Cursor Fix in Web

@sonarqubecloud
Copy link

sonarqubecloud bot commented Dec 5, 2025

@joaoloureirop joaoloureirop merged commit f39566e into release/7.61.0 Dec 6, 2025
72 of 74 checks passed
@joaoloureirop joaoloureirop deleted the runway-cherry-pick-7.61.0-1764937242 branch December 6, 2025 00:50
@github-actions github-actions bot locked and limited conversation to collaborators Dec 6, 2025
@metamaskbot metamaskbot added the release-7.61.0 Issue or pull request that will be included in release 7.61.0 label Dec 6, 2025
@metamaskbot
Copy link
Collaborator

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

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

Labels

release-7.61.0 Issue or pull request that will be included in release 7.61.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