2022-12-08 19:37:06 +01:00
|
|
|
import React, { useContext, useRef } from 'react';
|
2021-06-28 16:45:08 +02:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
2022-01-21 16:58:59 +01:00
|
|
|
import BigNumber from 'bignumber.js';
|
2023-01-27 19:28:03 +01:00
|
|
|
import { EditGasModes } from '../../../../shared/constants/gas';
|
2021-07-12 18:16:03 +02:00
|
|
|
|
2021-07-01 16:36:48 +02:00
|
|
|
import Button from '../../ui/button';
|
2021-06-28 16:45:08 +02:00
|
|
|
import Typography from '../../ui/typography/typography';
|
2021-07-31 02:45:18 +02:00
|
|
|
|
2021-06-28 16:45:08 +02:00
|
|
|
import {
|
2022-12-08 19:37:06 +01:00
|
|
|
TEXT_ALIGN,
|
2023-02-02 21:15:26 +01:00
|
|
|
TypographyVariant,
|
2021-06-28 16:45:08 +02:00
|
|
|
FONT_WEIGHT,
|
2023-02-02 21:15:26 +01:00
|
|
|
TextColor,
|
2021-06-28 16:45:08 +02:00
|
|
|
} from '../../../helpers/constants/design-system';
|
2021-07-28 00:31:53 +02:00
|
|
|
import { areDappSuggestedAndTxParamGasFeesTheSame } from '../../../helpers/utils/confirm-tx.util';
|
2021-06-28 16:45:08 +02:00
|
|
|
|
|
|
|
import InfoTooltip from '../../ui/info-tooltip';
|
2021-07-31 00:46:31 +02:00
|
|
|
import ErrorMessage from '../../ui/error-message';
|
2021-06-28 16:45:08 +02:00
|
|
|
import AdvancedGasControls from '../advanced-gas-controls/advanced-gas-controls.component';
|
2021-07-12 18:16:03 +02:00
|
|
|
import ActionableMessage from '../../ui/actionable-message/actionable-message';
|
2021-06-28 16:45:08 +02:00
|
|
|
|
|
|
|
import { I18nContext } from '../../../contexts/i18n';
|
2021-10-28 19:06:16 +02:00
|
|
|
|
2021-06-28 16:45:08 +02:00
|
|
|
export default function EditGasDisplay({
|
2023-01-27 19:28:03 +01:00
|
|
|
mode = EditGasModes.modifyInPlace,
|
2021-08-03 19:51:02 +02:00
|
|
|
estimatedMinimumNative,
|
2022-12-08 19:37:06 +01:00
|
|
|
transaction,
|
2021-07-14 18:45:37 +02:00
|
|
|
gasPrice,
|
|
|
|
setGasPrice,
|
|
|
|
gasLimit,
|
|
|
|
setGasLimit,
|
2022-01-21 16:58:59 +01:00
|
|
|
properGasLimit,
|
2021-07-14 18:45:37 +02:00
|
|
|
dappSuggestedGasFeeAcknowledged,
|
|
|
|
setDappSuggestedGasFeeAcknowledged,
|
2021-07-22 18:32:59 +02:00
|
|
|
onManualChange,
|
2021-07-30 13:35:30 +02:00
|
|
|
minimumGasLimit,
|
2021-07-31 00:46:31 +02:00
|
|
|
balanceError,
|
2022-12-08 19:37:06 +01:00
|
|
|
gasErrors,
|
2021-08-17 22:08:13 +02:00
|
|
|
txParamsHaveBeenCustomized,
|
2021-06-28 16:45:08 +02:00
|
|
|
}) {
|
|
|
|
const t = useContext(I18nContext);
|
2021-09-14 11:02:13 +02:00
|
|
|
const scrollRef = useRef(null);
|
|
|
|
|
2022-07-31 20:26:40 +02:00
|
|
|
const dappSuggestedAndTxParamGasFeesAreTheSame =
|
|
|
|
areDappSuggestedAndTxParamGasFeesTheSame(transaction);
|
2021-07-28 00:31:53 +02:00
|
|
|
|
2021-07-12 18:16:03 +02:00
|
|
|
const requireDappAcknowledgement = Boolean(
|
2021-07-28 00:31:53 +02:00
|
|
|
transaction?.dappSuggestedGasFees &&
|
|
|
|
!dappSuggestedGasFeeAcknowledged &&
|
|
|
|
dappSuggestedAndTxParamGasFeesAreTheSame,
|
2021-07-12 18:16:03 +02:00
|
|
|
);
|
|
|
|
|
2022-01-21 16:58:59 +01:00
|
|
|
let warningMessage;
|
2022-01-25 22:51:42 +01:00
|
|
|
if (
|
|
|
|
gasLimit !== undefined &&
|
|
|
|
properGasLimit !== undefined &&
|
|
|
|
new BigNumber(gasLimit).lessThan(new BigNumber(properGasLimit))
|
|
|
|
) {
|
2022-01-21 16:58:59 +01:00
|
|
|
warningMessage = t('gasLimitRecommended', [properGasLimit]);
|
|
|
|
}
|
|
|
|
|
2022-12-08 19:37:06 +01:00
|
|
|
const showTopError = balanceError && txParamsHaveBeenCustomized;
|
2021-08-01 15:09:53 +02:00
|
|
|
|
2021-07-31 00:46:31 +02:00
|
|
|
let errorKey;
|
|
|
|
if (balanceError) {
|
|
|
|
errorKey = 'insufficientFunds';
|
|
|
|
}
|
2021-07-29 20:13:14 +02:00
|
|
|
|
2021-06-28 16:45:08 +02:00
|
|
|
return (
|
|
|
|
<div className="edit-gas-display">
|
|
|
|
<div className="edit-gas-display__content">
|
2021-08-17 22:08:13 +02:00
|
|
|
{showTopError && (
|
2021-07-31 00:46:31 +02:00
|
|
|
<div className="edit-gas-display__warning">
|
|
|
|
<ErrorMessage errorKey={errorKey} />
|
|
|
|
</div>
|
|
|
|
)}
|
2022-01-21 16:58:59 +01:00
|
|
|
{warningMessage && (
|
|
|
|
<div className="edit-gas-display__warning">
|
|
|
|
<ActionableMessage
|
|
|
|
className="actionable-message--warning"
|
|
|
|
message={warningMessage}
|
2022-03-25 16:32:36 +01:00
|
|
|
iconFillColor="var(--color-warning-default)"
|
2022-01-21 16:58:59 +01:00
|
|
|
useIcon
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
2022-12-08 19:37:06 +01:00
|
|
|
{requireDappAcknowledgement && (
|
2021-07-01 16:36:48 +02:00
|
|
|
<div className="edit-gas-display__dapp-acknowledgement-warning">
|
|
|
|
<ActionableMessage
|
|
|
|
className="actionable-message--warning"
|
2021-07-27 01:43:05 +02:00
|
|
|
message={t('gasDisplayDappWarning', [transaction.origin])}
|
2022-03-25 16:32:36 +01:00
|
|
|
iconFillColor="var(--color-warning-default)"
|
2021-07-01 16:36:48 +02:00
|
|
|
useIcon
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
2023-01-27 19:28:03 +01:00
|
|
|
{mode === EditGasModes.speedUp && (
|
2021-06-28 16:45:08 +02:00
|
|
|
<div className="edit-gas-display__top-tooltip">
|
|
|
|
<Typography
|
2023-02-02 21:15:26 +01:00
|
|
|
color={TextColor.textDefault}
|
|
|
|
variant={TypographyVariant.H8}
|
2021-06-28 16:45:08 +02:00
|
|
|
fontWeight={FONT_WEIGHT.BOLD}
|
|
|
|
>
|
|
|
|
{t('speedUpTooltipText')}{' '}
|
|
|
|
<InfoTooltip
|
|
|
|
position="top"
|
|
|
|
contentText={t('speedUpExplanation')}
|
|
|
|
/>
|
|
|
|
</Typography>
|
|
|
|
</div>
|
|
|
|
)}
|
2022-12-08 19:37:06 +01:00
|
|
|
<Typography
|
2023-02-02 21:15:26 +01:00
|
|
|
color={TextColor.textDefault}
|
|
|
|
variant={TypographyVariant.H1}
|
2022-12-08 19:37:06 +01:00
|
|
|
align={TEXT_ALIGN.CENTER}
|
|
|
|
>
|
|
|
|
{estimatedMinimumNative}
|
|
|
|
</Typography>
|
2021-07-01 16:36:48 +02:00
|
|
|
{requireDappAcknowledgement && (
|
|
|
|
<Button
|
|
|
|
className="edit-gas-display__dapp-acknowledgement-button"
|
|
|
|
onClick={() => setDappSuggestedGasFeeAcknowledged(true)}
|
|
|
|
>
|
|
|
|
{t('gasDisplayAcknowledgeDappButtonText')}
|
|
|
|
</Button>
|
|
|
|
)}
|
2022-12-08 19:37:06 +01:00
|
|
|
{!requireDappAcknowledgement && (
|
|
|
|
<AdvancedGasControls
|
|
|
|
gasLimit={gasLimit}
|
|
|
|
setGasLimit={setGasLimit}
|
|
|
|
gasPrice={gasPrice}
|
|
|
|
setGasPrice={setGasPrice}
|
|
|
|
onManualChange={onManualChange}
|
|
|
|
minimumGasLimit={minimumGasLimit}
|
|
|
|
gasErrors={gasErrors}
|
2021-08-01 15:09:53 +02:00
|
|
|
/>
|
|
|
|
)}
|
2021-06-28 16:45:08 +02:00
|
|
|
</div>
|
2021-09-14 11:02:13 +02:00
|
|
|
<div ref={scrollRef} className="edit-gas-display__scroll-bottom" />
|
2021-06-28 16:45:08 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
EditGasDisplay.propTypes = {
|
2023-01-27 19:28:03 +01:00
|
|
|
mode: PropTypes.oneOf(Object.values(EditGasModes)),
|
2021-08-03 19:51:02 +02:00
|
|
|
estimatedMinimumNative: PropTypes.string,
|
2021-07-14 18:45:37 +02:00
|
|
|
gasPrice: PropTypes.string,
|
|
|
|
setGasPrice: PropTypes.func,
|
|
|
|
gasLimit: PropTypes.number,
|
|
|
|
setGasLimit: PropTypes.func,
|
2022-01-21 16:58:59 +01:00
|
|
|
properGasLimit: PropTypes.number,
|
2021-08-04 15:24:07 +02:00
|
|
|
dappSuggestedGasFeeAcknowledged: PropTypes.bool,
|
2021-07-14 18:45:37 +02:00
|
|
|
setDappSuggestedGasFeeAcknowledged: PropTypes.func,
|
2021-07-16 17:00:03 +02:00
|
|
|
transaction: PropTypes.object,
|
2021-07-22 18:32:59 +02:00
|
|
|
onManualChange: PropTypes.func,
|
2021-08-10 22:45:09 +02:00
|
|
|
minimumGasLimit: PropTypes.string,
|
2021-07-31 00:46:31 +02:00
|
|
|
balanceError: PropTypes.bool,
|
2022-12-08 19:37:06 +01:00
|
|
|
gasErrors: PropTypes.object,
|
2021-08-17 22:08:13 +02:00
|
|
|
txParamsHaveBeenCustomized: PropTypes.bool,
|
2021-06-28 16:45:08 +02:00
|
|
|
};
|