Skip to content

fix(perps): correct amount reset when balance changes and simplify setAmount cp-7.65.0#25759

Merged
michalconsensys merged 2 commits intomainfrom
fix/perps/initial-amount-10
Feb 6, 2026
Merged

fix(perps): correct amount reset when balance changes and simplify setAmount cp-7.65.0#25759
michalconsensys merged 2 commits intomainfrom
fix/perps/initial-amount-10

Conversation

@michalconsensys
Copy link
Copy Markdown
Contributor

@michalconsensys michalconsensys commented Feb 6, 2026

Description

  1. What is the reason for the change?
    When the user changed the payment token or when the effective balance dropped, the Perps order form was resetting the amount to max whenever the current amount was greater than the new max. That logic could also run when the user had intentionally set the amount to 0 or left it empty (e.g. initial value 10), and setAmount was forcing empty values to '0', which made it harder to preserve the intended initial amount.

  2. What is the improvement/solution?

    • In the useEffect that reacts to balanceForMax / maxPossibleAmount / orderForm.amount: only reset the amount when it actually exceeds the new max. If currentAmount === 0, maxPossibleAmount === 0, or currentAmount < maxPossibleAmount, we return early and do not overwrite the form amount.
    • In setAmount, pass through the amount string as-is and remove the || '0' fallback so the form can keep an empty or user-chosen initial value (e.g. 10) without being forced to '0'.

Changelog

CHANGELOG entry: Fixed Perps order form so the amount is only reset when it exceeds the new max after changing payment token or balance, and no longer overwrites an initial or empty amount.

Related issues

Fixes:

Manual testing steps

Feature: Perps order form amount when balance or payment token changes

  Scenario: user has set amount to 10 and then changes payment token
    Given user is on the Perps order view with amount set to 10 (or another value below the new max)

    When user changes the payment token (or balance updates so max possible amount changes)
    Then the amount remains 10 and is not reset to max or to 0

Screenshots/Recordings

Before

No visible change

After

No visible change

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

Low Risk
Small, localized change to UI state synchronization logic; primary risk is subtle regressions in amount clamping behavior when balances update.

Overview
Adjusts the Perps order form auto-clamp logic so orderForm.amount is only reset when it actually exceeds the newly computed maxPossibleAmount after balance/payment-token changes.

This refactors the useEffect to parse the current amount directly, early-return for zero/under-max cases, and then set the amount to Math.floor(maxPossibleAmount) only when clamping is required.

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

…tAmount

Co-authored-by: Cursor <cursoragent@cursor.com>
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Feb 6, 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 the team-perps Perps team label Feb 6, 2026
@michalconsensys michalconsensys marked this pull request as ready for review February 6, 2026 09:43
@michalconsensys michalconsensys requested a review from a team as a code owner February 6, 2026 09:43
@github-actions github-actions bot added the size-S label Feb 6, 2026
Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Feb 6, 2026

🔍 Smart E2E Test Selection

  • Selected E2E tags: SmokePerps, SmokeWalletPlatform, SmokeConfirmations
  • Selected Performance tags: @PerformancePreps
  • Risk Level: medium
  • AI Confidence: 85%
click to see 🤖 AI reasoning details

E2E Test Selection:
The change modifies the usePerpsOrderForm hook, specifically the useEffect that handles resetting the order amount when the payment token changes or effective balance drops. The modification changes the logic from always checking if current amount exceeds max (with a fallback to '0') to an early-return pattern that skips the update when: currentAmount is 0, maxPossibleAmount is 0, or currentAmount is already less than maxPossibleAmount. This is a targeted bug fix/improvement to prevent unnecessary amount resets.

The hook is used by PerpsOrderContext which powers the PerpsOrderView for perpetuals trading. Per the tag descriptions:

  • SmokePerps: Directly tests perpetuals trading functionality including Add Funds flow, balance verification, and balance updates
  • SmokeWalletPlatform: Required because Perps is a section inside the Trending tab, and changes to Perps views affect this tag
  • SmokeConfirmations: Required per SmokePerps description since Add Funds deposits are on-chain transactions

The change is isolated to the Perps feature and doesn't affect other wallet functionality. Risk is medium because while the change is localized, it affects order form state management which is critical for trading operations.

Performance Test Selection:
The change modifies state management logic in the usePerpsOrderForm hook, specifically the useEffect that handles amount updates. While this is a minor logic change (adding early returns), it affects the Perps order form state updates which could impact rendering performance. The @PerformancePreps tag covers perps market loading, position management, add funds flow, and order execution - all of which use this hook. Running performance tests will help ensure the state management changes don't introduce any performance regressions in the order form.

View GitHub Actions results

@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud bot commented Feb 6, 2026

@michalconsensys michalconsensys added this pull request to the merge queue Feb 6, 2026
Merged via the queue into main with commit 985f421 Feb 6, 2026
76 of 77 checks passed
@michalconsensys michalconsensys deleted the fix/perps/initial-amount-10 branch February 6, 2026 14:12
@github-actions github-actions bot locked and limited conversation to collaborators Feb 6, 2026
@metamaskbot metamaskbot added the release-7.66.0 Issue or pull request that will be included in release 7.66.0 label Feb 6, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

release-7.66.0 Issue or pull request that will be included in release 7.66.0 size-S team-perps Perps team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants