Skip to content

feat(perps): add optimistic updates for take profit and stop loss prices#24159

Merged
gambinish merged 3 commits intomainfrom
fix/perps/tpsl-optimistic-update
Jan 6, 2026
Merged

feat(perps): add optimistic updates for take profit and stop loss prices#24159
gambinish merged 3 commits intomainfrom
fix/perps/tpsl-optimistic-update

Conversation

@michalconsensys
Copy link
Copy Markdown
Contributor

@michalconsensys michalconsensys commented Dec 18, 2025

Description

When users update take profit (TP) or stop loss (SL) prices in Perps, the position would display undefined values until the WebSocket returned the updated position from the server. This created a poor user experience with a visible delay.

This PR implements optimistic updates that immediately reflect TP/SL price changes in the UI after a successful API response, before the WebSocket confirms the change. This provides instant feedback to users.

Changelog

CHANGELOG entry: Use optimistic approach when updating TP/SL in perps

Related issues

Fixes: https://consensyssoftware.atlassian.net/browse/TAT-2054

Manual testing steps

Feature: Perps TP/SL Optimistic Updates

  Scenario: user updates take profit price
    Given user has an open position in Perps
    And user is on the TP/SL view

    When user sets a take profit price and confirms
    Then the take profit price is immediately displayed on the position
    And user does not see undefined or empty values

  Scenario: user updates stop loss price
    Given user has an open position in Perps
    And user is on the TP/SL view

    When user sets a stop loss price and confirms
    Then the stop loss price is immediately displayed on the position
    And user does not see undefined or empty values

  Scenario: user removes take profit and stop loss
    Given user has an open position with TP/SL set
    And user is on the TP/SL view

    When user removes both TP and SL and confirms
    Then the position immediately shows no TP/SL values
    And user does not see stale values

Screenshots/Recordings

Before

See video in https://consensyssoftware.atlassian.net/browse/TAT-2054

After

Simulator.Screen.Recording.-.iPhone.17.Pro.-.2025-12-18.at.15.42.03.mov

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

Introduces immediate UI feedback for TP/SL changes by updating the positions cache optimistically after a successful API response.

  • Adds positions.updatePositionTPSLOptimistic(coin, takeProfitPrice, stopLossPrice) in PerpsStreamManager to mutate cached positions and notify subscribers
  • Integrates optimistic call into usePerpsTPSLUpdate (invoked before success toast) while preserving existing error handling/haptics
  • Expands tests: new cases for optimistic updates across scenarios (TP only, SL only, both, removals, failures, ordering vs toast) and stream channel cache behavior
  • Minor test wording tweak for haptics verification

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

@michalconsensys michalconsensys added the team-perps Perps team label Dec 18, 2025
@github-actions
Copy link
Copy Markdown
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.

@michalconsensys michalconsensys marked this pull request as ready for review December 18, 2025 15:01
@michalconsensys michalconsensys requested a review from a team as a code owner December 18, 2025 15:01
Copy link
Copy Markdown
Member

@gambinish gambinish left a comment

Choose a reason for hiding this comment

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

Looks good to me. Can we also get @abretonc7s to review this as well?

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Jan 5, 2026

🔍 Smart E2E Test Selection

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

The changes are entirely contained within the Perps (Perpetuals) feature module:

  1. usePerpsTPSLUpdate.ts - Added optimistic update functionality for Take Profit/Stop Loss (TP/SL) updates. This uses the usePerpsStream hook to immediately update the UI before WebSocket confirms the change, improving user experience.

  2. PerpsStreamManager.tsx - Added a new method updatePositionTPSLOptimistic to the PositionStreamChannel class that allows optimistic updates to position TP/SL values in the cache.

  3. Test files - Both implementation files have comprehensive unit tests added (usePerpsTPSLUpdate.test.ts and PerpsStreamManager.test.tsx) covering various scenarios including success cases, error handling, and edge cases.

The changes:

  • Are self-contained within the Perps module (app/components/UI/Perps/)
  • Only affect perpetuals trading functionality (TP/SL position management)
  • Do not touch any core wallet functionality, controllers, or Engine
  • Are well-tested with extensive unit test coverage
  • Represent a UX enhancement (optimistic updates) rather than a fundamental change

The usePerpsTPSLUpdate hook is only imported by Perps-specific components (PerpsMarketDetailsView) and the hooks index file. The PerpsStreamManager is used exclusively by Perps-related hooks and components.

View GitHub Actions results

@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud bot commented Jan 5, 2026

@gambinish gambinish added this pull request to the merge queue Jan 6, 2026
Merged via the queue into main with commit 07f0ee6 Jan 6, 2026
91 checks passed
@gambinish gambinish deleted the fix/perps/tpsl-optimistic-update branch January 6, 2026 23:10
@github-actions github-actions bot locked and limited conversation to collaborators Jan 6, 2026
@metamaskbot metamaskbot added the release-7.62.0 Issue or pull request that will be included in release 7.62.0 label Jan 6, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

quality-sprint-dec-2025 release-7.62.0 Issue or pull request that will be included in release 7.62.0 size-L team-perps Perps team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants