Skip to content

chore(runway): cherry-pick fix(perps): correct amount reset when balance changes and simplify setAmount cp-7.65.0#25776

Merged
joaoloureirop merged 1 commit intorelease/7.65.0from
runway-cherry-pick-7.65.0-1770387276
Feb 6, 2026
Merged

chore(runway): cherry-pick fix(perps): correct amount reset when balance changes and simplify setAmount cp-7.65.0#25776
joaoloureirop merged 1 commit intorelease/7.65.0from
runway-cherry-pick-7.65.0-1770387276

Conversation

@runway-github
Copy link
Contributor

@runway-github runway-github bot 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 UI state change to Perps order form amount clamping with minimal surface area; main risk is behavior differences around edge cases like 0/empty amounts.

Overview
Fixes Perps order form amount clamping when balanceForMax/maxPossibleAmount changes so it only overwrites the entered amount when the current value is greater than or equal to the new max, and avoids clamping when either value is 0.

This refactors the useEffect to early-return for non-exceeding/zero cases and consistently floor the recalculated max before updating orderForm.amount.

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


Co-authored-by: Cursor cursoragent@cursor.com 985f421

…nce changes and simplify setAmount cp-7.65.0 (#25759)

<!--
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**

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**

<!--
If this PR is not End-User-Facing and should not show up in the
CHANGELOG, you can choose to either:
1. Write `CHANGELOG entry: null`
2. Label with `no-changelog`

If this PR is End-User-Facing, please write a short User-Facing
description in the past tense like:
`CHANGELOG entry: Added a new tab for users to see their NFTs`
`CHANGELOG entry: Fixed a bug that was causing some NFTs to flicker`

(This helps the Release Engineer do their job more quickly and
accurately)
-->

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**

```gherkin
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**

- [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]
> **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.
> 
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
9d6f45b. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
@runway-github runway-github bot requested a review from a team as a code owner February 6, 2026 14:14
@github-actions
Copy link
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-bots Bot team (for MetaMask Bot, Runway Bot, etc.) label Feb 6, 2026
@github-actions
Copy link
Contributor

github-actions bot commented Feb 6, 2026

🔍 Smart E2E Test Selection

⏭️ Smart E2E selection skipped - base branch is not main (base: release/7.65.0)

All E2E tests pre-selected.

View GitHub Actions results

@github-actions github-actions bot added the size-S label Feb 6, 2026
Copy link

@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.

maxPossibleAmount === 0 ||
currentAmount < maxPossibleAmount
)
return;
Copy link

Choose a reason for hiding this comment

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

NaN from empty amount bypasses early return guard

Medium Severity

The old code used Number.parseFloat(orderForm.amount || '0') to safely default empty strings to '0'. The new code uses Number.parseFloat(orderForm.amount) without this fallback, producing NaN when orderForm.amount is empty. Since all comparisons with NaN return false (NaN === 0 is false, NaN < maxPossibleAmount is false), none of the early-return conditions will trigger, causing the amount to be unexpectedly reset to maxPossibleAmount. Every other call site in the codebase (e.g., in PerpsOrderView.tsx) consistently uses orderForm.amount || '0' as a guard before parsing.

Fix in Cursor Fix in Web

@sonarqubecloud
Copy link

sonarqubecloud bot commented Feb 6, 2026

@joaoloureirop joaoloureirop merged commit 97a5730 into release/7.65.0 Feb 6, 2026
79 of 81 checks passed
@joaoloureirop joaoloureirop deleted the runway-cherry-pick-7.65.0-1770387276 branch February 6, 2026 15:13
@github-actions github-actions bot locked and limited conversation to collaborators Feb 6, 2026
@metamaskbot metamaskbot added the release-7.65.0 Issue or pull request that will be included in release 7.65.0 label Feb 9, 2026
@metamaskbot
Copy link
Collaborator

No release label on PR. Adding release label release-7.65.0 on PR, as PR was cherry-picked in branch 7.65.0.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

release-7.65.0 Issue or pull request that will be included in release 7.65.0 size-S team-bots Bot team (for MetaMask Bot, Runway Bot, etc.)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants