Skip to content

fix(perps): block stop loss beyond liquidation boundaries#41922

Open
lexaisnotdead wants to merge 2 commits into
MetaMask:mainfrom
lexaisnotdead:fix-perps-stop-loss-liquidation-41898
Open

fix(perps): block stop loss beyond liquidation boundaries#41922
lexaisnotdead wants to merge 2 commits into
MetaMask:mainfrom
lexaisnotdead:fix-perps-stop-loss-liquidation-41898

Conversation

@lexaisnotdead
Copy link
Copy Markdown

@lexaisnotdead lexaisnotdead commented Apr 19, 2026

Description

This PR fixes issue #41898, where the Perps order entry flow still allowed stop loss values beyond the liquidation boundary.

The original fix only covered the update TP/SL modal for existing positions. This follow-up fixes the actual live submit path used by the Perps order entry page, makes stop-loss validation liquidation-aware in the auto-close section, and disables the real page-level submit button when the stop loss crosses liquidation.

Changelog

CHANGELOG entry: Fixed a bug that allowed Perps stop loss values beyond the liquidation price.

Related issues

Fixes: #41898

Manual testing steps

  1. Open the Perps order entry page for a market such as ETH.
  2. Enter an amount so the liquidation price is calculated.
  3. Enable Auto close.
  4. Enter a stop loss beyond the liquidation boundary.
  5. Verify the UI shows the liquidation validation error.
  6. Verify the Open Long / Open Short submit button is disabled.
  7. Enter a stop loss back within the valid range between the entry/current price and liquidation price.
  8. Verify the validation error clears and the submit button becomes enabled again.

Screenshots/Recordings

Before

Before 1 Before 2

After

After 1 After 2

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

Medium Risk
Changes order-entry and TP/SL editing validation to block stop-loss values that cross liquidation boundaries, which can affect whether users can submit orders. Risk is moderate due to new boundary logic and dependence on liquidation price parsing/availability.

Overview
Stop-loss validation is now liquidation-aware across perps order entry and the update TP/SL modal, preventing SL values that would cross the liquidation boundary.

This introduces getStopLossValidationResult (replacing direct isValidStopLossPrice usage in several UI paths) to distinguish current-price vs liquidation-price validation failures, wires liquidationPrice into AutoCloseSection and the page-level submit-disable logic, and adds a new i18n message (perpsStopLossInvalidLiquidationPrice) plus expanded test coverage for the new boundary and disabled-submit behavior.

Reviewed by Cursor Bugbot for commit 29cb42b. Bugbot is set up for automated code reviews on this repo. Configure here.

The Perps TP/SL editor only validated stop loss values against the
current price, which allowed values to cross the position liquidation
boundary. This change makes stop loss validation liquidation-aware,
keeps the UI error specific to the violated boundary, and adds
regression coverage for the short-position case reported in MetaMask#41898.

Constraint: Keep the fix narrowly scoped to the existing TP/SL validation flow
Constraint: Preserve current take-profit behavior and existing form structure
Rejected: Backend-only validation | issue report requires immediate UI feedback and disabled submit state
Rejected: Broad TP/SL refactor | unnecessary scope increase for a release regression
Confidence: high
Scope-risk: narrow
Reversibility: clean
Directive: Keep stop loss validation aligned with both current-price and liquidation-price boundaries when evolving Perps forms
Tested: node_modules/.bin/jest ui/components/app/perps/utils/tpslValidation.test.ts ui/components/app/perps/update-tpsl/update-tpsl-modal-content.test.tsx --runInBand
Tested: node_modules/.bin/tsx development/lint-changed.mts --fix
Not-tested: Full repository lint/test suite
Related: MetaMask#41898
The Perps order-entry page still accepted stop loss values beyond the
liquidation boundary even though inline validation messaging appeared.
The live page owned its own submit-disable logic and only checked stop
losses against the current or entry price, so this change makes the
page-level validation liquidation-aware and adds regression coverage for
that exact flow.

Constraint: Keep the fix scoped to the Perps TP/SL validation and submit gating paths
Constraint: Do not include unrelated local build or .gitignore changes in this commit
Rejected: Rely on OrderEntry internal submit disabling alone | the live page renders its own external submit button
Rejected: Backend-only validation | the issue requires immediate UI feedback and blocked submission
Confidence: high
Scope-risk: narrow
Reversibility: clean
Directive: Any future TP/SL validation changes must keep the page-level submit-disable logic aligned with the form-level validation helpers
Tested: node_modules/.bin/jest ui/pages/perps/perps-order-entry-page.test.tsx ui/components/app/perps/order-entry/components/auto-close-section/auto-close-section.test.tsx ui/components/app/perps/update-tpsl/update-tpsl-modal-content.test.tsx ui/components/app/perps/utils/tpslValidation.test.ts --runInBand
Tested: node_modules/.bin/tsx development/lint-changed.mts --fix
Not-tested: Full repository lint/test suite
Related: MetaMask#41898
@lexaisnotdead lexaisnotdead requested a review from a team as a code owner April 19, 2026 23:02
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 19, 2026

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 external-contributor INVALID-PR-TEMPLATE PR's body doesn't match template labels Apr 19, 2026
@lexaisnotdead
Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

@metamaskbot metamaskbot removed the INVALID-PR-TEMPLATE PR's body doesn't match template label Apr 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Perps: Stop loss can be set above the liquidation price

2 participants