2022-01-06 03:47:26 +01:00
|
|
|
import { useSelector } from 'react-redux';
|
|
|
|
import React, { useEffect } from 'react';
|
|
|
|
|
2023-01-27 19:28:03 +01:00
|
|
|
import { EditGasModes, PriorityLevels } from '../../../../shared/constants/gas';
|
2022-01-06 03:47:26 +01:00
|
|
|
import {
|
2023-02-02 21:15:26 +01:00
|
|
|
AlignItems,
|
2022-01-06 03:47:26 +01:00
|
|
|
DISPLAY,
|
|
|
|
FLEX_DIRECTION,
|
2023-02-02 21:15:26 +01:00
|
|
|
TypographyVariant,
|
2022-01-06 03:47:26 +01:00
|
|
|
} from '../../../helpers/constants/design-system';
|
|
|
|
import { getAppIsLoading } from '../../../selectors';
|
|
|
|
import { gasEstimateGreaterThanGasUsedPlusTenPercent } from '../../../helpers/utils/gas';
|
|
|
|
import { useGasFeeContext } from '../../../contexts/gasFee';
|
|
|
|
import { useI18nContext } from '../../../hooks/useI18nContext';
|
|
|
|
import { useTransactionModalContext } from '../../../contexts/transaction-modal';
|
|
|
|
import EditGasFeeButton from '../edit-gas-fee-button';
|
|
|
|
import GasDetailsItem from '../gas-details-item';
|
|
|
|
import Box from '../../ui/box';
|
|
|
|
import Button from '../../ui/button';
|
|
|
|
import InfoTooltip from '../../ui/info-tooltip';
|
|
|
|
import Popover from '../../ui/popover';
|
|
|
|
import Typography from '../../ui/typography';
|
|
|
|
import AppLoadingSpinner from '../app-loading-spinner';
|
|
|
|
|
|
|
|
const CancelSpeedupPopover = () => {
|
|
|
|
const {
|
|
|
|
cancelTransaction,
|
|
|
|
editGasMode,
|
|
|
|
gasFeeEstimates,
|
|
|
|
speedUpTransaction,
|
|
|
|
transaction,
|
|
|
|
updateTransaction,
|
2022-01-06 23:40:31 +01:00
|
|
|
updateTransactionToTenPercentIncreasedGasFee,
|
2022-01-06 03:47:26 +01:00
|
|
|
updateTransactionUsingEstimate,
|
|
|
|
} = useGasFeeContext();
|
|
|
|
const t = useI18nContext();
|
|
|
|
const { closeModal, currentModal } = useTransactionModalContext();
|
|
|
|
const appIsLoading = useSelector(getAppIsLoading);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (
|
|
|
|
transaction.previousGas ||
|
|
|
|
appIsLoading ||
|
|
|
|
currentModal !== 'cancelSpeedUpTransaction'
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// If gas used previously + 10% is less than medium estimated gas
|
2022-01-06 23:40:31 +01:00
|
|
|
// estimate is set to medium, else estimate is set to tenPercentIncreased
|
2022-01-06 03:47:26 +01:00
|
|
|
const gasUsedLessThanMedium =
|
|
|
|
gasFeeEstimates &&
|
|
|
|
gasEstimateGreaterThanGasUsedPlusTenPercent(
|
2022-01-26 19:18:43 +01:00
|
|
|
transaction.txParams,
|
2022-01-06 03:47:26 +01:00
|
|
|
gasFeeEstimates,
|
2023-01-27 19:28:03 +01:00
|
|
|
PriorityLevels.medium,
|
2022-01-06 03:47:26 +01:00
|
|
|
);
|
|
|
|
if (gasUsedLessThanMedium) {
|
2023-01-27 19:28:03 +01:00
|
|
|
updateTransactionUsingEstimate(PriorityLevels.medium);
|
2022-01-06 03:47:26 +01:00
|
|
|
return;
|
|
|
|
}
|
2022-02-01 18:53:03 +01:00
|
|
|
updateTransactionToTenPercentIncreasedGasFee(true);
|
2022-01-06 03:47:26 +01:00
|
|
|
}, [
|
|
|
|
appIsLoading,
|
|
|
|
currentModal,
|
|
|
|
editGasMode,
|
|
|
|
gasFeeEstimates,
|
|
|
|
transaction,
|
|
|
|
updateTransaction,
|
2022-01-06 23:40:31 +01:00
|
|
|
updateTransactionToTenPercentIncreasedGasFee,
|
2022-01-06 03:47:26 +01:00
|
|
|
updateTransactionUsingEstimate,
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (currentModal !== 'cancelSpeedUpTransaction') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const submitTransactionChange = () => {
|
2023-01-27 19:28:03 +01:00
|
|
|
if (editGasMode === EditGasModes.cancel) {
|
2022-01-06 03:47:26 +01:00
|
|
|
cancelTransaction();
|
|
|
|
} else {
|
|
|
|
speedUpTransaction();
|
|
|
|
}
|
2022-03-22 17:05:46 +01:00
|
|
|
closeModal(['cancelSpeedUpTransaction']);
|
2022-01-06 03:47:26 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Popover
|
|
|
|
title={
|
|
|
|
<>
|
2023-01-27 19:28:03 +01:00
|
|
|
{editGasMode === EditGasModes.cancel
|
2022-01-06 03:47:26 +01:00
|
|
|
? `❌${t('cancel')}`
|
|
|
|
: `🚀${t('speedUp')}`}
|
|
|
|
</>
|
|
|
|
}
|
2022-04-05 15:36:58 +02:00
|
|
|
onClose={() => closeModal(['cancelSpeedUpTransaction'])}
|
2022-01-06 03:47:26 +01:00
|
|
|
className="cancel-speedup-popover"
|
|
|
|
>
|
|
|
|
<AppLoadingSpinner className="cancel-speedup-popover__spinner" />
|
|
|
|
<div className="cancel-speedup-popover__wrapper">
|
|
|
|
<Typography
|
2023-02-02 21:15:26 +01:00
|
|
|
boxProps={{ alignItems: AlignItems.center, display: DISPLAY.FLEX }}
|
|
|
|
variant={TypographyVariant.H6}
|
2022-07-20 22:47:51 +02:00
|
|
|
marginTop={0}
|
|
|
|
marginBottom={2}
|
2022-01-06 03:47:26 +01:00
|
|
|
>
|
2022-07-12 15:59:38 +02:00
|
|
|
{t('cancelSpeedUpLabel', [
|
|
|
|
<strong key="cancelSpeedupReplace">{t('replace')}</strong>,
|
|
|
|
])}
|
2022-01-06 03:47:26 +01:00
|
|
|
<InfoTooltip
|
|
|
|
position="top"
|
|
|
|
contentText={
|
|
|
|
<Box>
|
|
|
|
{t('cancelSpeedUpTransactionTooltip', [
|
2023-01-27 19:28:03 +01:00
|
|
|
editGasMode === EditGasModes.cancel
|
2022-05-09 17:37:31 +02:00
|
|
|
? t('cancel')
|
|
|
|
: t('speedUp'),
|
2022-01-06 03:47:26 +01:00
|
|
|
])}
|
|
|
|
<div>
|
|
|
|
<a
|
|
|
|
href="https://community.metamask.io/t/how-to-speed-up-or-cancel-transactions-on-metamask/3296"
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
>
|
|
|
|
{t('learnMoreUpperCase')}
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
</Box>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</Typography>
|
|
|
|
<div className="cancel-speedup-popover__separator" />
|
|
|
|
<Box
|
|
|
|
display={DISPLAY.FLEX}
|
2023-02-02 21:15:26 +01:00
|
|
|
alignItems={AlignItems.center}
|
2022-01-06 03:47:26 +01:00
|
|
|
flexDirection={FLEX_DIRECTION.COLUMN}
|
|
|
|
marginTop={4}
|
|
|
|
>
|
|
|
|
<Box className="cancel-speedup-popover__edit-gas-button">
|
2022-01-31 06:51:43 +01:00
|
|
|
{!appIsLoading && <EditGasFeeButton />}
|
2022-01-06 03:47:26 +01:00
|
|
|
</Box>
|
|
|
|
<Box className="cancel-speedup-popover__gas-details">
|
|
|
|
<GasDetailsItem />
|
|
|
|
</Box>
|
|
|
|
</Box>
|
|
|
|
<Button type="primary" onClick={submitTransactionChange}>
|
2022-07-12 15:59:38 +02:00
|
|
|
{t('submit')}
|
2022-01-06 03:47:26 +01:00
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</Popover>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default CancelSpeedupPopover;
|