Commit 6d2a205
authored
feat(perps): show a trading halt notice when the deviation is above threshold (MetaMask#23242)
<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->
## **Description**
This PR adds a price deviation warning feature to the Perps trading
interface. When the perps price deviates more than 10% from the spot
price (mark price), a warning banner is displayed to inform users that
new positions cannot be opened at that time. This helps protect users
from executing trades at unfavorable prices during periods of
significant price divergence.
## **Changelog**
CHANGELOG entry: Added price deviation warning to Perps trading
interface to prevent opening positions when perps price deviates
significantly from spot price
## **Related issues**
Fixes: https://consensyssoftware.atlassian.net/browse/TAT-2047
## **Manual testing steps**
```gherkin
Feature: Perps Price Deviation Warning
Scenario: user views market details when price deviation is within threshold
Given user is on the Perps Market Details view for BTC
And the asset price is within 10% of the spot price
When user views the market details page
Then no price deviation warning should be displayed
Scenario: user views market details when price deviation exceeds threshold
Given user is on the Perps Market Details view for BTC
And the perps price deviates more than 10% from the spot price
When user views the market details page
Then a price deviation warning banner should be displayed
And the warning message should indicate that new positions cannot be opened
Scenario: warning updates dynamically as price changes
Given user is on the Perps Market Details view for BTC
And the perps price initially deviates more than 10% from spot price
When the price deviation decreases below 10%
Then the warning banner should disappear
```
## **Screenshots/Recordings**
<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->
### **Before**
No notice shown
### **After**
Notice shown
<img width="1170" height="2532" alt="Simulator Screenshot - iPhone 16e -
2025-11-25 at 11 17 03"
src="https://github.com/user-attachments/assets/02cfb5d5-f57f-4101-8e8c-a852af73b606"
/>
## **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]
> Introduces a price deviation check and warning banner that disables
trade actions when perps price deviates beyond a configured threshold
from spot.
>
> - **UI**
> - Add `PerpsPriceDeviationWarning` component and render it in
`PerpsMarketDetailsView` when `useIsPriceDeviatedAboveThreshold` signals
trading halt.
> - Hide fixed actions footer (Add Funds / Long / Short / Modify /
Close) while trading is halted.
> - Minor cleanup: remove unnecessary fragment around
`TradingViewChart`.
> - **Hooks**
> - New `useIsPriceDeviatedAboveThreshold(symbol)` leveraging
`usePerpsPrices` to detect deviation; exported via `hooks/index`.
> - **Config**
> - Add `VALIDATION_THRESHOLDS.PRICE_DEVIATION` (10%).
> - **i18n**
> - Add `perps.price_deviation_warning.message` copy.
> - **Tests**
> - Add comprehensive unit tests for `useIsPriceDeviatedAboveThreshold`.
> - Update `PerpsMarketDetailsView.test.tsx` with mocks for new hook and
connections; ensure rendering compatibility.
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
81d14af. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->1 parent b966482 commit 6d2a205
11 files changed
Lines changed: 590 additions & 16 deletions
File tree
- app/components/UI/Perps
- Views/PerpsMarketDetailsView
- components/PerpsPriceDeviationWarning
- constants
- hooks
- locales/languages
Lines changed: 35 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
182 | 182 | | |
183 | 183 | | |
184 | 184 | | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
185 | 199 | | |
186 | 200 | | |
187 | 201 | | |
188 | 202 | | |
189 | 203 | | |
190 | 204 | | |
| 205 | + | |
191 | 206 | | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
192 | 211 | | |
193 | 212 | | |
194 | 213 | | |
| |||
266 | 285 | | |
267 | 286 | | |
268 | 287 | | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
269 | 299 | | |
270 | 300 | | |
271 | 301 | | |
272 | 302 | | |
273 | 303 | | |
| 304 | + | |
274 | 305 | | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
275 | 310 | | |
276 | 311 | | |
277 | 312 | | |
| |||
Lines changed: 29 additions & 16 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
68 | 68 | | |
69 | 69 | | |
70 | 70 | | |
| 71 | + | |
71 | 72 | | |
72 | 73 | | |
73 | 74 | | |
| |||
81 | 82 | | |
82 | 83 | | |
83 | 84 | | |
| 85 | + | |
84 | 86 | | |
85 | 87 | | |
86 | 88 | | |
| |||
295 | 297 | | |
296 | 298 | | |
297 | 299 | | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
298 | 306 | | |
299 | 307 | | |
300 | 308 | | |
| |||
894 | 902 | | |
895 | 903 | | |
896 | 904 | | |
897 | | - | |
898 | | - | |
899 | | - | |
900 | | - | |
901 | | - | |
902 | | - | |
903 | | - | |
904 | | - | |
905 | | - | |
906 | | - | |
907 | | - | |
908 | | - | |
909 | | - | |
910 | | - | |
911 | | - | |
| 905 | + | |
| 906 | + | |
| 907 | + | |
| 908 | + | |
| 909 | + | |
| 910 | + | |
| 911 | + | |
| 912 | + | |
| 913 | + | |
| 914 | + | |
| 915 | + | |
| 916 | + | |
| 917 | + | |
912 | 918 | | |
913 | 919 | | |
914 | 920 | | |
| |||
925 | 931 | | |
926 | 932 | | |
927 | 933 | | |
| 934 | + | |
| 935 | + | |
| 936 | + | |
| 937 | + | |
| 938 | + | |
| 939 | + | |
| 940 | + | |
928 | 941 | | |
929 | 942 | | |
930 | 943 | | |
| |||
1035 | 1048 | | |
1036 | 1049 | | |
1037 | 1050 | | |
1038 | | - | |
| 1051 | + | |
1039 | 1052 | | |
1040 | 1053 | | |
1041 | 1054 | | |
| |||
app/components/UI/Perps/components/PerpsPriceDeviationWarning/PerpsPriceDeviationWarning.styles.ts
Lines changed: 25 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
Lines changed: 52 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
Lines changed: 4 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
Lines changed: 2 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
71 | 71 | | |
72 | 72 | | |
73 | 73 | | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
74 | 77 | | |
75 | 78 | | |
76 | 79 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
63 | 63 | | |
64 | 64 | | |
65 | 65 | | |
| 66 | + | |
66 | 67 | | |
67 | 68 | | |
68 | 69 | | |
| |||
0 commit comments