2022-01-06 03:47:26 +01:00
|
|
|
import { useSelector } from 'react-redux';
|
|
|
|
import React, { useEffect } from 'react';
|
|
|
|
|
|
|
|
import {
|
|
|
|
EDIT_GAS_MODES,
|
|
|
|
PRIORITY_LEVELS,
|
|
|
|
} from '../../../../shared/constants/gas';
|
|
|
|
import {
|
|
|
|
ALIGN_ITEMS,
|
|
|
|
DISPLAY,
|
|
|
|
FLEX_DIRECTION,
|
|
|
|
TYPOGRAPHY,
|
|
|
|
} 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,
|
|
|
|
PRIORITY_LEVELS.MEDIUM,
|
|
|
|
);
|
|
|
|
if (gasUsedLessThanMedium) {
|
|
|
|
updateTransactionUsingEstimate(PRIORITY_LEVELS.MEDIUM);
|
|
|
|
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 = () => {
|
|
|
|
if (editGasMode === EDIT_GAS_MODES.CANCEL) {
|
|
|
|
cancelTransaction();
|
|
|
|
} else {
|
|
|
|
speedUpTransaction();
|
|
|
|
}
|
2022-03-22 17:05:46 +01:00
|
|
|
closeModal(['cancelSpeedUpTransaction']);
|
2022-01-06 03:47:26 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Popover
|
|
|
|
title={
|
|
|
|
<>
|
|
|
|
{editGasMode === EDIT_GAS_MODES.CANCEL
|
|
|
|
? `❌${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
|
|
|
|
boxProps={{ alignItems: ALIGN_ITEMS.CENTER, display: DISPLAY.FLEX }}
|
|
|
|
variant={TYPOGRAPHY.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', [
|
2022-05-09 17:37:31 +02:00
|
|
|
editGasMode === EDIT_GAS_MODES.CANCEL
|
|
|
|
? 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}
|
|
|
|
alignItems={ALIGN_ITEMS.CENTER}
|
|
|
|
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;
|