Skip to content

Commit 58d8221

Browse files
fix: cp-7.54.0 quotes on Optimism crashing app (MetaMask#18486)
<!-- 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** <!-- Write a short description of the changes included in this pull request, also include relevant motivation and context. Have in mind the following questions: 1. What is the reason for the change? 2. What is the improvement/solution? --> This PR fixes an issue where the app would crash when `quote.gasFee.effective.amount` was in scientific notation. You could observe this on networks such as Optimism. ## **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: null ## **Related issues** Fixes: MetaMask#18454 ## **Manual testing steps** ```gherkin Feature: Getting quotes on Optimism Scenario: user requests a quote on Optimism Given user has requested a quote When user requests a quote Then they should receive a quote and app should not crash ``` ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> ### **Before** <!-- [screenshots/recordings] --> ### **After** <!-- [screenshots/recordings] --> https://github.com/user-attachments/assets/ca420125-5d07-4b10-9283-bedc5aff585f ## **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.
1 parent 6ad9c10 commit 58d8221

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

app/components/UI/Bridge/hooks/useHasSufficientGas/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { ethers } from 'ethers';
88
import { CaipChainId, Hex } from '@metamask/utils';
99
import { useBridgeQuoteData } from '../useBridgeQuoteData';
1010
import { getNativeSourceToken } from '../useInitialSourceToken';
11+
import { BigNumber } from 'bignumber.js';
1112

1213
interface Props {
1314
quote: ReturnType<typeof useBridgeQuoteData>['activeQuote'];
@@ -40,10 +41,15 @@ export const useHasSufficientGas = ({ quote }: Props): boolean | null => {
4041
decimals: sourceChainNativeAsset?.decimals,
4142
});
4243

44+
// quote.gasFee.effective.amount might be in scientific notation (e.g. 9.200359292e-8), so we need to handle that
45+
const effectiveGasFee = quote?.gasFee.effective
46+
? new BigNumber(quote.gasFee.effective.amount).toFixed()
47+
: null;
48+
4349
const atomicGasFee =
44-
quote?.gasFee.effective && !gasIncluded
50+
effectiveGasFee && !gasIncluded
4551
? ethers.utils.parseUnits(
46-
quote.gasFee.effective.amount,
52+
effectiveGasFee,
4753
sourceChainNativeAsset?.decimals,
4854
)
4955
: null;

app/components/UI/Bridge/hooks/useHasSufficientGas/useHasSufficientGas.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,33 @@ describe('useHasSufficientGas', () => {
9191
expect(result.current).toBe(false);
9292
});
9393

94+
it('should handle scientific notation in effective gas fee', () => {
95+
const mockQuote: ReturnType<typeof useBridgeQuoteData>['activeQuote'] =
96+
{
97+
quote: {
98+
gasIncluded: false,
99+
srcChainId: '0x1',
100+
},
101+
gasFee: {
102+
effective: { amount: '9.200359292e-8' }, // Scientific notation
103+
},
104+
} as unknown as ReturnType<typeof useBridgeQuoteData>['activeQuote'];
105+
106+
// User has 0.001 ETH (more than enough for the tiny gas fee)
107+
mockUseLatestBalance.mockReturnValue({
108+
displayBalance: '0.001',
109+
atomicBalance: BigNumber.from('1000000000000000'), // 0.001 ETH in wei
110+
});
111+
112+
const { result } = renderHookWithProvider(
113+
() => useHasSufficientGas({ quote: mockQuote }),
114+
{ state: {} },
115+
);
116+
117+
// Should return true since 0.001 ETH > 0.00000009200359292 ETH
118+
expect(result.current).toBe(true);
119+
});
120+
94121
it('should return null when gas token balance is not available', () => {
95122
const mockQuote: ReturnType<typeof useBridgeQuoteData>['activeQuote'] =
96123
{

0 commit comments

Comments
 (0)