Skip to content

Commit 990941d

Browse files
release(runway): cherry-pick fix(confirmations): compare Money Account pay-token balances as strings (#47981)
- fix(confirmations): compare Money Account pay-token balances as strings (#47932) ## **Description** `useAutomaticMoneyAccountPayToken` decides whether to default Pay with to the Money Account by comparing its balance (a `bignumber.js` value) with the highest token fiat balance and the minimum required balance. Those two values are plain JS numbers, and `bignumber.js` 4 throws `gt() number type has more than 15 significant digits` when it is given a number with more than 15 significant digits. Fiat balances are computed in floating point and often have more, so opening a Pay confirmation (for example a Perps deposit) with such a token balance crashed the confirmation. This passes both values to `BigNumber` as strings, which `bignumber.js` accepts at any precision, and adds a test with a 16-digit fiat balance (it fails without the fix). ## **Changelog** CHANGELOG entry: Fixed a crash when opening a MetaMask Pay confirmation with a token balance that has many decimal places ## **Related issues** Follow-up to #47836. ## **Manual testing steps** 1. Use an account with a Money Account balance and a token whose fiat balance has more than 15 significant digits (most non-round balances). 2. Open Perps → Add funds. 3. Before: the confirmation crashes with "gt() number type has more than 15 significant digits". After: it opens, and Pay with defaults as before. ## **Screenshots/Recordings** ### **Before** The confirmation shows "MetaMask encountered an error" with `BigNumber Error: gt() number type has more than 15 significant digits`. ### **After** The Perps deposit confirmation opens and quotes. ## **Pre-merge author checklist** - [x] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Extension Coding Standards](https://github.com/MetaMask/metamask-extension/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-extension/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 fix in pay-token auto-selection logic with a regression test; no auth, payment execution, or broad API changes. > > **Overview** > Fixes a **MetaMask Pay confirmation crash** when token fiat balances exceed JavaScript’s ~15 significant-digit limit passed into `bignumber.js` `gt()` / `gte()`. > > In **`useAutomaticMoneyAccountPayToken`**, the highest token fiat balance and minimum required balance are now coerced with **`String(...)`** before **`BigNumber`** comparison (Money Account withdrawable balance was already a BigNumber). Selection behavior is unchanged; only the comparison path is safe for high-precision fiat values. > > Adds a hook test with a **16-digit** token fiat balance to ensure **`shouldSelect`** still works without throwing. > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 65878f0. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY --> [deb08d6](deb08d6) Co-authored-by: Pedro Figueiredo <pedro.figueiredo@consensys.net>
1 parent 76f35ff commit 990941d

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

‎ui/pages/confirmations/hooks/pay/useAutomaticMoneyAccountPayToken.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,22 @@ describe('useAutomaticMoneyAccountPayToken', () => {
178178
expect(applyMoneyAccountOverrideMock).toHaveBeenCalled();
179179
});
180180

181+
it('compares fiat balances with more than 15 significant digits', () => {
182+
mockMoneyAccountPay({ balance: '1.1' });
183+
184+
const { result } = runHook({
185+
availableTokens: [
186+
{
187+
address: TOKEN_ADDRESS,
188+
chainId: '0x1',
189+
fiat: { balance: 0.9727848652231649 },
190+
},
191+
],
192+
});
193+
194+
expect(result.current.shouldSelect).toBe(true);
195+
});
196+
181197
it('selects Money Account when its balance beats a preferred token', () => {
182198
mockMoneyAccountPay({ balance: '76' });
183199
selectPreferredPayTokensMock.mockReturnValue([

‎ui/pages/confirmations/hooks/pay/useAutomaticMoneyAccountPayToken.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,11 @@ export function useAutomaticMoneyAccountPayToken({
9696
);
9797
const withdrawableBalance = toFiatBalance(withdrawableFiatRaw);
9898
const highestBalanceToken = getHighestBalanceToken(availableTokens);
99-
const highestTokenBalance = highestBalanceToken?.fiat?.balance ?? 0;
99+
// bignumber.js 4 throws on JS numbers with more than 15 significant digits,
100+
// which fiat balances often have, so compare through strings.
101+
const highestTokenBalance = new BigNumber(
102+
String(highestBalanceToken?.fiat?.balance ?? 0),
103+
);
100104
const isHighestBalanceTokenPreferred = Boolean(
101105
highestBalanceToken &&
102106
preferredTokens.some((token) => isSamePayToken(highestBalanceToken, token)),
@@ -107,7 +111,9 @@ export function useAutomaticMoneyAccountPayToken({
107111
withdrawableBalance?.greaterThan(0) &&
108112
(withdrawableBalance.greaterThan(highestTokenBalance) ||
109113
(!isHighestBalanceTokenPreferred &&
110-
withdrawableBalance.greaterThanOrEqualTo(minimumRequiredTokenBalance))),
114+
withdrawableBalance.greaterThanOrEqualTo(
115+
new BigNumber(String(minimumRequiredTokenBalance)),
116+
))),
111117
);
112118

113119
useEffect(() => {

0 commit comments

Comments
 (0)