1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Show warning in confirm transaction screen for low/high gas values. (#12545)

This commit is contained in:
Jyoti Puri 2021-11-12 08:52:54 +05:30 committed by GitHub
parent 9bc07f5e6b
commit 4779bd9fe5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 175 additions and 19 deletions

View File

@ -1349,6 +1349,9 @@
"lockTimeTooGreat": { "lockTimeTooGreat": {
"message": "Lock time is too great" "message": "Lock time is too great"
}, },
"lowPriorityMessage": {
"message": "Future transactions will queue after this one. This price was last seen was some time ago."
},
"mainnet": { "mainnet": {
"message": "Ethereum Mainnet" "message": "Ethereum Mainnet"
}, },

View File

@ -24,6 +24,7 @@ import InfoTooltip from '../../ui/info-tooltip/info-tooltip';
import { getGasFeeTimeEstimate } from '../../../store/actions'; import { getGasFeeTimeEstimate } from '../../../store/actions';
import { GAS_FORM_ERRORS } from '../../../helpers/constants/gas'; import { GAS_FORM_ERRORS } from '../../../helpers/constants/gas';
import { useGasFeeContext } from '../../../contexts/gasFee';
// Once we reach this second threshold, we switch to minutes as a unit // Once we reach this second threshold, we switch to minutes as a unit
const SECOND_CUTOFF = 90; const SECOND_CUTOFF = 90;
@ -49,6 +50,7 @@ export default function GasTiming({
const [customEstimatedTime, setCustomEstimatedTime] = useState(null); const [customEstimatedTime, setCustomEstimatedTime] = useState(null);
const t = useContext(I18nContext); const t = useContext(I18nContext);
const { estimateToUse } = useGasFeeContext();
// If the user has chosen a value lower than the low gas fee estimate, // If the user has chosen a value lower than the low gas fee estimate,
// We'll need to use the useEffect hook below to make a call to calculate // We'll need to use the useEffect hook below to make a call to calculate
@ -94,12 +96,17 @@ export default function GasTiming({
previousIsUnknownLow, previousIsUnknownLow,
]); ]);
const unknownProcessingTimeText = ( let unknownProcessingTimeText;
if (EIP_1559_V2) {
unknownProcessingTimeText = t('editGasTooLow');
} else {
unknownProcessingTimeText = (
<> <>
{t('editGasTooLow')}{' '} {t('editGasTooLow')}{' '}
<InfoTooltip position="top" contentText={t('editGasTooLowTooltip')} /> <InfoTooltip position="top" contentText={t('editGasTooLowTooltip')} />
</> </>
); );
}
if ( if (
gasWarnings?.maxPriorityFee === GAS_FORM_ERRORS.MAX_PRIORITY_FEE_TOO_LOW || gasWarnings?.maxPriorityFee === GAS_FORM_ERRORS.MAX_PRIORITY_FEE_TOO_LOW ||
@ -148,8 +155,9 @@ export default function GasTiming({
]); ]);
} }
} else { } else {
if (!EIP_1559_V2 || estimateToUse === 'low') {
attitude = 'negative'; attitude = 'negative';
}
// If the user has chosen a value less than our low estimate, // If the user has chosen a value less than our low estimate,
// calculate a potential wait time // calculate a potential wait time
if (isUnknownLow) { if (isUnknownLow) {
@ -191,7 +199,8 @@ export default function GasTiming({
<Typography <Typography
variant={TYPOGRAPHY.H7} variant={TYPOGRAPHY.H7}
className={classNames('gas-timing', { className={classNames('gas-timing', {
[`gas-timing--${attitude}`]: attitude, [`gas-timing--${attitude}`]: attitude && !EIP_1559_V2,
[`gas-timing--${attitude}-V2`]: attitude && EIP_1559_V2,
})} })}
> >
{text} {text}

View File

@ -14,6 +14,11 @@
font-weight: bold; font-weight: bold;
} }
&--negative-V2 {
color: $secondary-1;
font-weight: bold;
}
.info-tooltip { .info-tooltip {
display: inline-block; display: inline-block;
margin-inline-start: 4px; margin-inline-start: 4px;

View File

@ -4,9 +4,9 @@ import { useSelector } from 'react-redux';
import { getAdvancedInlineGasShown } from '../../selectors'; import { getAdvancedInlineGasShown } from '../../selectors';
import { hexToDecimal } from '../../helpers/utils/conversions.util'; import { hexToDecimal } from '../../helpers/utils/conversions.util';
import { import {
EDIT_GAS_MODES,
GAS_RECOMMENDATIONS,
CUSTOM_GAS_ESTIMATE, CUSTOM_GAS_ESTIMATE,
GAS_RECOMMENDATIONS,
EDIT_GAS_MODES,
} from '../../../shared/constants/gas'; } from '../../../shared/constants/gas';
import { GAS_FORM_ERRORS } from '../../helpers/constants/gas'; import { GAS_FORM_ERRORS } from '../../helpers/constants/gas';

View File

@ -54,6 +54,7 @@ import Typography from '../../components/ui/typography/typography';
import { MIN_GAS_LIMIT_DEC } from '../send/send.constants'; import { MIN_GAS_LIMIT_DEC } from '../send/send.constants';
import GasDetailsItem from './gas-details-item'; import GasDetailsItem from './gas-details-item';
import LowPriorityMessage from './low-priority-message';
// eslint-disable-next-line prefer-destructuring // eslint-disable-next-line prefer-destructuring
const EIP_1559_V2 = process.env.EIP_1559_V2; const EIP_1559_V2 = process.env.EIP_1559_V2;
@ -414,6 +415,7 @@ export default class ConfirmTransactionBase extends Component {
return ( return (
<div className="confirm-page-container-content__details"> <div className="confirm-page-container-content__details">
{EIP_1559_V2 && <LowPriorityMessage />}
<TransactionDetail <TransactionDetail
onEdit={() => this.handleEditGas()} onEdit={() => this.handleEditGas()}
rows={[ rows={[

View File

@ -1,5 +1,6 @@
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import classNames from 'classnames';
import { COLORS } from '../../../helpers/constants/design-system'; import { COLORS } from '../../../helpers/constants/design-system';
import { PRIMARY, SECONDARY } from '../../../helpers/constants/common'; import { PRIMARY, SECONDARY } from '../../../helpers/constants/common';
@ -14,11 +15,12 @@ import InfoTooltip from '../../../components/ui/info-tooltip/info-tooltip';
import LoadingHeartBeat from '../../../components/ui/loading-heartbeat'; import LoadingHeartBeat from '../../../components/ui/loading-heartbeat';
import TransactionDetailItem from '../../../components/app/transaction-detail-item/transaction-detail-item.component'; import TransactionDetailItem from '../../../components/app/transaction-detail-item/transaction-detail-item.component';
import UserPreferencedCurrencyDisplay from '../../../components/app/user-preferenced-currency-display'; import UserPreferencedCurrencyDisplay from '../../../components/app/user-preferenced-currency-display';
import { useGasFeeContext } from '../../../contexts/gasFee';
const HeartBeat = () => const HeartBeat = () =>
process.env.IN_TEST === 'true' ? null : <LoadingHeartBeat />; process.env.IN_TEST === 'true' ? null : <LoadingHeartBeat />;
const GasDetailItem = ({ const GasDetailsItem = ({
hexMaximumTransactionFee, hexMaximumTransactionFee,
hexMinimumTransactionFee, hexMinimumTransactionFee,
isMainnet, isMainnet,
@ -29,6 +31,8 @@ const GasDetailItem = ({
useNativeCurrencyAsPrimaryCurrency, useNativeCurrencyAsPrimaryCurrency,
}) => { }) => {
const t = useI18nContext(); const t = useI18nContext();
const { estimateToUse } = useGasFeeContext();
return ( return (
<TransactionDetailItem <TransactionDetailItem
key="gas-item" key="gas-item"
@ -62,7 +66,7 @@ const GasDetailItem = ({
</Typography> </Typography>
</> </>
} }
position="top" position="bottom"
/> />
</Box> </Box>
} }
@ -88,9 +92,18 @@ const GasDetailItem = ({
</div> </div>
} }
subText={t('editGasSubTextFee', [ subText={t('editGasSubTextFee', [
<Box key="editGasSubTextFeeLabel" display="inline-flex"> <Box
<Box marginRight={1} className="gas-details-item__gasfee-label"> key="editGasSubTextFeeLabel"
display="inline-flex"
className={classNames('gas-details-item__gasfee-label', {
'gas-details-item__gas-fee-warning': estimateToUse === 'high',
})}
>
<Box marginRight={1}>
<b>
{estimateToUse === 'high' && '⚠ '}
<I18nValue messageKey="editGasSubTextFeeLabel" /> <I18nValue messageKey="editGasSubTextFeeLabel" />
</b>
</Box> </Box>
<div <div
key="editGasSubTextFeeValue" key="editGasSubTextFeeValue"
@ -122,7 +135,7 @@ const GasDetailItem = ({
); );
}; };
GasDetailItem.propTypes = { GasDetailsItem.propTypes = {
hexMaximumTransactionFee: PropTypes.string, hexMaximumTransactionFee: PropTypes.string,
hexMinimumTransactionFee: PropTypes.string, hexMinimumTransactionFee: PropTypes.string,
isMainnet: PropTypes.bool, isMainnet: PropTypes.bool,
@ -133,4 +146,4 @@ GasDetailItem.propTypes = {
useNativeCurrencyAsPrimaryCurrency: PropTypes.bool, useNativeCurrencyAsPrimaryCurrency: PropTypes.bool,
}; };
export default GasDetailItem; export default GasDetailsItem;

View File

@ -7,6 +7,10 @@
line-height: inherit; line-height: inherit;
} }
&__gas-fee-warning {
color: $secondary-1;
}
&__gasfee-label { &__gasfee-label {
font-weight: bold; font-weight: bold;
} }

View File

@ -2,11 +2,20 @@ import React from 'react';
import { screen } from '@testing-library/react'; import { screen } from '@testing-library/react';
import { ETH } from '../../../helpers/constants/common'; import { ETH } from '../../../helpers/constants/common';
import { GasFeeContextProvider } from '../../../contexts/gasFee';
import { renderWithProvider } from '../../../../test/jest'; import { renderWithProvider } from '../../../../test/jest';
import configureStore from '../../../store/store'; import configureStore from '../../../store/store';
import GasDetailsItem from './gas-details-item'; import GasDetailsItem from './gas-details-item';
jest.mock('../../../store/actions', () => ({
disconnectGasFeeEstimatePoller: jest.fn(),
getGasFeeEstimatesAndStartPolling: jest
.fn()
.mockImplementation(() => Promise.resolve()),
addPollingTokenToAppState: jest.fn(),
}));
const render = (props) => { const render = (props) => {
const store = configureStore({ const store = configureStore({
metamask: { metamask: {
@ -15,10 +24,23 @@ const render = (props) => {
useNativeCurrencyAsPrimaryCurrency: true, useNativeCurrencyAsPrimaryCurrency: true,
}, },
provider: {}, provider: {},
cachedBalances: {},
accounts: {
'0xAddress': {
address: '0xAddress',
balance: '0x176e5b6f173ebe66',
},
},
selectedAddress: '0xAddress',
}, },
}); });
return renderWithProvider(<GasDetailsItem txData={{}} {...props} />, store); return renderWithProvider(
<GasFeeContextProvider {...props}>
<GasDetailsItem txData={{}} {...props} />
</GasFeeContextProvider>,
store,
);
}; };
describe('GasDetailsItem', () => { describe('GasDetailsItem', () => {
@ -29,4 +51,14 @@ describe('GasDetailsItem', () => {
expect(screen.queryByText('Max fee:')).toBeInTheDocument(); expect(screen.queryByText('Max fee:')).toBeInTheDocument();
expect(screen.queryByText('ETH')).toBeInTheDocument(); expect(screen.queryByText('ETH')).toBeInTheDocument();
}); });
it('should show warning icon if estimates are high', () => {
render({ defaultEstimateToUse: 'high' });
expect(screen.queryByText('⚠ Max fee:')).toBeInTheDocument();
});
it('should not show warning icon if estimates are not high', () => {
render({ defaultEstimateToUse: 'low' });
expect(screen.queryByText('Max fee:')).toBeInTheDocument();
});
}); });

View File

@ -0,0 +1 @@
export { default } from './low-priority-message';

View File

@ -0,0 +1,24 @@
import React from 'react';
import ActionableMessage from '../../../components/ui/actionable-message/actionable-message';
import { useGasFeeContext } from '../../../contexts/gasFee';
import { useI18nContext } from '../../../hooks/useI18nContext';
const LowPriorityMessage = () => {
const { estimateToUse } = useGasFeeContext();
const t = useI18nContext();
if (estimateToUse !== 'low') return null;
return (
<div className="low-priority-message">
<ActionableMessage
className="actionable-message--warning"
message={t('lowPriorityMessage')}
useIcon
iconFillColor="#f8c000"
/>
</div>
);
};
export default LowPriorityMessage;

View File

@ -0,0 +1,3 @@
.low-priority-message {
margin-top: 20px;
}

View File

@ -0,0 +1,59 @@
import React from 'react';
import { renderWithProvider } from '../../../../test/lib/render-helpers';
import { ETH } from '../../../helpers/constants/common';
import { GasFeeContextProvider } from '../../../contexts/gasFee';
import configureStore from '../../../store/store';
import LowPriorityMessage from './low-priority-message';
jest.mock('../../../store/actions', () => ({
disconnectGasFeeEstimatePoller: jest.fn(),
getGasFeeEstimatesAndStartPolling: jest
.fn()
.mockImplementation(() => Promise.resolve()),
addPollingTokenToAppState: jest.fn(),
}));
const render = (props) => {
const store = configureStore({
metamask: {
nativeCurrency: ETH,
preferences: {
useNativeCurrencyAsPrimaryCurrency: true,
},
provider: {},
cachedBalances: {},
accounts: {
'0xAddress': {
address: '0xAddress',
balance: '0x176e5b6f173ebe66',
},
},
selectedAddress: '0xAddress',
},
});
return renderWithProvider(
<GasFeeContextProvider {...props}>
<LowPriorityMessage />
</GasFeeContextProvider>,
store,
);
};
describe('LowPriorityMessage', () => {
it('should returning warning message for low gas estimate', () => {
render({ transaction: { userFeeLevel: 'low' } });
expect(
document.getElementsByClassName('actionable-message--warning'),
).toHaveLength(1);
});
it('should return null for gas estimate other than low', () => {
render({ transaction: { userFeeLevel: 'high' } });
expect(
document.getElementsByClassName('actionable-message--warning'),
).toHaveLength(0);
});
});

View File

@ -6,6 +6,7 @@
@import 'confirm-decrypt-message/confirm-decrypt-message'; @import 'confirm-decrypt-message/confirm-decrypt-message';
@import 'confirm-encryption-public-key/confirm-encryption-public-key'; @import 'confirm-encryption-public-key/confirm-encryption-public-key';
@import 'confirm-transaction-base/gas-details-item/gas-details-item'; @import 'confirm-transaction-base/gas-details-item/gas-details-item';
@import 'confirm-transaction-base/low-priority-message/low-priority-message';
@import 'confirmation/confirmation'; @import 'confirmation/confirmation';
@import 'connected-sites/index'; @import 'connected-sites/index';
@import 'connected-accounts/index'; @import 'connected-accounts/index';