Skip to content

Commit 214e60d

Browse files
feat: Show < 0.01 when gas cost is really small (#33966)
<!-- 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** Includes the following flows: - Send flow - Native transaction hero component - ERC20 transaction hero component - Transaction simulation balance changes - Selection of gas fee tokens <!-- 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? --> [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/33966?quickstart=1) ## **Related issues** Fixes: MetaMask/MetaMask-planning#5256 ## **Manual testing steps** ``` async function connectMetaMask() { try { const accounts = await window.ethereum.request({ method: 'eth_requestAccounts', }); console.log('Connected account:', accounts[0]); return accounts[0]; } catch (error) { console.error('User rejected the request:', error); throw error; } } async function createTransactionWithHexData() { try { const fromAddress = await connectMetaMask(); const transactionParameters = { to: '0x0C8ADd8d1B0eEFd9E4FD646ab75E3762b38B87dC', from: fromAddress, value: '0xE8D4A51000', }; const txHash = await window.ethereum.request({ method: 'eth_sendTransaction', params: [transactionParameters], }); console.log('Transaction hash:', txHash); return txHash; } catch (error) { console.error('Error creating transaction:', error); } } createTransactionWithHexData(); ``` ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> ### **Before** <!-- [screenshots/recordings] --> ### **After** <!-- [screenshots/recordings] --> <img width="404" alt="Screenshot 2025-06-27 at 17 40 55" src="https://github.com/user-attachments/assets/4e51e4ef-fb9e-4499-9f06-c56953887bd9" /> <img width="512" alt="Screenshot 2025-06-27 at 14 21 29" src="https://github.com/user-attachments/assets/36e7eb1a-fedc-4e62-abdc-89ea6a64796c" /> <img width="2032" alt="Screenshot 2025-06-27 at 13 27 01" src="https://github.com/user-attachments/assets/019cd622-9ba8-43a5-9986-6de1d58d4dea" /> <img width="2032" alt="Screenshot 2025-06-27 at 13 24 55" src="https://github.com/user-attachments/assets/074c0e1d-cfe1-4734-a881-bffd0b9a27ef" /> ## **Pre-merge author checklist** - [ ] 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). - [ ] I've completed the PR template to the best of my ability - [ ] I’ve included tests if applicable - [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [ ] 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. --------- Co-authored-by: Vinicius Stevam <[email protected]> Co-authored-by: Vinicius Stevam <[email protected]>
1 parent 6c80487 commit 214e60d

File tree

5 files changed

+51
-9
lines changed

5 files changed

+51
-9
lines changed

ui/components/app/currency-input/currency-input.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { useEffect, useRef, useState } from 'react';
22
import PropTypes from 'prop-types';
33
import { useSelector } from 'react-redux';
4+
import BigNumber from 'bignumber.js';
45
import { Box } from '../../component-library';
56
import { BlockSize } from '../../../helpers/constants/design-system';
67
import UnitInput from '../../ui/unit-input';
@@ -205,10 +206,19 @@ export default function CurrencyInput({
205206

206207
if (isTokenPrimary) {
207208
// Display fiat; `displayValue` bypasses calculations
208-
displayValue = formatCurrency(
209-
new Numeric(fiatDecimalValue, 10).toString(),
210-
secondaryCurrency,
211-
);
209+
const isNonZeroSmallValue =
210+
fiatDecimalValue &&
211+
new BigNumber(fiatDecimalValue).lt(new BigNumber(0.01)) &&
212+
new BigNumber(fiatDecimalValue).greaterThan(new BigNumber(0));
213+
displayValue = isNonZeroSmallValue
214+
? `< ${formatCurrency(
215+
new Numeric('0.01', 10).toString(),
216+
secondaryCurrency,
217+
)}`
218+
: formatCurrency(
219+
new Numeric(fiatDecimalValue, 10).toString(),
220+
secondaryCurrency,
221+
);
212222
} else {
213223
// Display token
214224
suffix = primarySuffix;

ui/hooks/useEthFiatAmount.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useMemo } from 'react';
22
import { useSelector } from 'react-redux';
3+
import BigNumber from 'bignumber.js';
34
import { getShouldShowFiat } from '../selectors';
45
import { formatCurrency } from '../helpers/utils/confirm-tx.util';
56
import {
@@ -41,6 +42,20 @@ export function useEthFiatAmount(
4142
return undefined;
4243
}
4344

45+
const fiatAmount = new BigNumber(ethAmount.toString()).times(conversionRate);
46+
if (
47+
ethAmount &&
48+
fiatAmount.lt(new BigNumber(0.01)) &&
49+
fiatAmount.greaterThan(new BigNumber(0))
50+
) {
51+
return hideCurrencySymbol
52+
? `< ${formatCurrency(0.01, currentCurrency)}`
53+
: `< ${formatCurrency(
54+
0.01,
55+
currentCurrency,
56+
)} ${currentCurrency.toUpperCase()}`;
57+
}
58+
4459
return hideCurrencySymbol
4560
? formatCurrency(formattedFiat, currentCurrency)
4661
: `${formatCurrency(

ui/pages/confirmations/components/confirm/info/hooks/use-token-values.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,13 @@ export const useTokenValues = (transactionMeta: TransactionMeta) => {
3434
decodedTransferValue &&
3535
exchangeRate.times(decodedTransferValue, 10).toNumber();
3636

37-
const fiatDisplayValue =
38-
fiatValue && fiatFormatter(fiatValue, { shorten: true });
37+
const isNonZeroSmallValue =
38+
fiatValue &&
39+
new BigNumber(String(fiatValue)).greaterThan(new BigNumber(0)) &&
40+
new BigNumber(String(fiatValue)).lt(new BigNumber(0.01));
41+
const fiatDisplayValue = isNonZeroSmallValue
42+
? `< ${fiatFormatter(0.01, { shorten: true })}`
43+
: fiatValue && fiatFormatter(fiatValue, { shorten: true });
3944

4045
const displayTransferValue = formatAmount(
4146
locale,

ui/pages/confirmations/components/confirm/info/shared/native-send-heading/native-send-heading.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,13 @@ const NativeSendHeading = () => {
5656
.times(nativeAssetTransferValue, 10)
5757
.toNumber();
5858
const fiatFormatter = useFiatFormatter();
59-
const fiatDisplayValue =
60-
fiatValue && fiatFormatter(fiatValue, { shorten: true });
59+
const isNonZeroSmallValue =
60+
fiatValue &&
61+
new BigNumber(String(fiatValue)).lt(new BigNumber(0.01)) &&
62+
new BigNumber(String(fiatValue)).greaterThan(new BigNumber(0));
63+
const fiatDisplayValue = isNonZeroSmallValue
64+
? `< ${fiatFormatter(0.01, { shorten: true })}`
65+
: fiatValue && fiatFormatter(fiatValue, { shorten: true });
6166

6267
const networkConfigurationsByChainId = useSelector(
6368
getNetworkConfigurationsByChainId,

ui/pages/confirmations/components/simulation-details/fiat-display.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
22
import { useSelector } from 'react-redux';
3+
import { BigNumber } from 'bignumber.js';
34
import {
45
TextAlign,
56
TextColor,
@@ -53,7 +54,13 @@ export const IndividualFiatDisplay: React.FC<{
5354
return null;
5455
}
5556
const absFiat = Math.abs(fiatAmount);
56-
const fiatDisplayValue = fiatFormatter(absFiat, { shorten });
57+
const isNonZeroSmallValue =
58+
absFiat &&
59+
new BigNumber(String(absFiat)).lt(new BigNumber(0.01)) &&
60+
new BigNumber(String(absFiat)).greaterThan(new BigNumber(0));
61+
const fiatDisplayValue = isNonZeroSmallValue
62+
? `< ${fiatFormatter(0.01, { shorten })}`
63+
: fiatFormatter(absFiat, { shorten });
5764

5865
return shorten ? (
5966
<Tooltip position="bottom" title={fiatDisplayValue} interactive>

0 commit comments

Comments
 (0)