feat(perps): add optimistic updates for take profit and stop loss prices #24159
+731
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.



Description
When users update take profit (TP) or stop loss (SL) prices in Perps, the position would display
undefinedvalues 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
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
Note
Adds optimistic TP/SL updates to the positions stream and applies them in the TPSL update hook, with comprehensive test coverage.
PositionStreamChannel: AddupdatePositionTPSLOptimistic(coin, takeProfitPrice, stopLossPrice)to update cached positions (takeProfitPrice,stopLossPrice,takeProfitCount,stopLossCount) and immediatelynotifySubscribers.usePerpsTPSLUpdate: IntegrateusePerpsStream; on successful API response, callstream.positions.updatePositionTPSLOptimistic(...)before showing success toast; update dependencies.PerpsStreamManager.test.tsx: Add extensive cases forupdatePositionTPSLOptimistic(TP only, SL only, both, remove both, unrelated positions unaffected, cache empty/miss, property preservation).usePerpsTPSLUpdate.test.ts: MockusePerpsStreamand assert optimistic updates are applied (including undefined values), ensure not applied on failures/exceptions, and verify ordering (optimistic update precedes toast); minor test name tweak.Written by Cursor Bugbot for commit cc778a1. This will update automatically on new commits. Configure here.