You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix(perps): reduce max order amount by 0.5% buffer to avoid insufficient margin rejections (MetaMask#27417)
<!--
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**
When users place a long/short Perps order with the amount slider at 100%
(or tap "Max"), the app was sending the full theoretical maximum
(available balance × leverage). The HyperLiquid API sometimes rejects
these with "Order 0: Insufficient margin to place order" due to fees,
rounding, and exchange-side margin checks.
**Solution:** Introduce a 0.5% margin buffer on the maximum order amount
so that "100%" uses 99.5% of the theoretical max. This is applied in a
single place (`getMaxAllowedAmount`), and the order form uses this
buffered value for the slider max, Max button, and 100% quick button so
all paths stay consistent. The buffer is configurable via
`MAX_ORDER_MARGIN_BUFFER` in perps config for future tuning or smarter
logic (e.g. fee-based).
**Changes:**
- **perpsConfig**: Added `MAX_ORDER_MARGIN_BUFFER = 0.005` (0.5%).
- **getMaxAllowedAmount**: After existing rounding logic, multiply max
by `(1 - MAX_ORDER_MARGIN_BUFFER)` and return `floor(bufferedMax)`.
- **usePerpsOrderForm**: `handleMaxAmount` and
`handlePercentageAmount(1)` now set amount to `maxPossibleAmount` (the
buffered max) instead of computing `balance × leverage` directly.
- **Tests**: Updated expectations for low-balance scenarios (e.g. $2 @
3x → max 5 instead of 6); added test that max is below theoretical after
buffer.
## **Changelog**
CHANGELOG entry: Fixed Perps orders at 100% margin sometimes failing
with "Insufficient margin" by applying a small buffer to the maximum
order amount.
## **Related issues**
Fixes: https://consensyssoftware.atlassian.net/browse/TAT-2502
## **Manual testing steps**
```gherkin
Feature: Perps order placement at maximum margin
Scenario: user places order at 100% (slider or Max) without insufficient margin error
Given user is on Perps order view with available balance and an asset selected
When user sets amount to 100% via the slider or taps the Max / 100% button
Then the amount field shows the buffered max (slightly below theoretical max)
And placing the order does not result in "Insufficient margin" rejection from the exchange
```
## **Screenshots/Recordings**
<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->
### **Before**
N/A (behavioral fix; no UI change)
### **After**
N/A
## **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]
> **Medium Risk**
> Adjusts core order-sizing calculations for max/percentage selections,
which can affect how much users trade and may surface edge cases around
rounding and balance updates. Changes are localized and covered by
updated/additional tests.
>
> **Overview**
> Reduces Perps *maximum order amount* by applying a configurable **0.5%
margin buffer** so “Max”/100% selections are less likely to be rejected
as *Insufficient margin*.
>
> This adds `MAX_ORDER_MARGIN_BUFFER` and applies it in
`getMaxAllowedAmount`, then updates `usePerpsOrderForm` handlers to
clamp percentage-based amounts and set Max to `maxPossibleAmount` (the
buffered max). Tests are updated to reflect new buffered expectations
and add coverage for near-100% clamping and buffered-max behavior.
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
03168c3. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
0 commit comments