2021-06-28 16:45:08 +02:00
|
|
|
import React, { useCallback, useContext, useState } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { useDispatch, useSelector } from 'react-redux';
|
2021-10-22 19:14:28 +02:00
|
|
|
import { useGasFeeInputs } from '../../../hooks/gasFeeInput/useGasFeeInputs';
|
2021-08-06 01:59:58 +02:00
|
|
|
import { getGasLoadingAnimationIsShowing } from '../../../ducks/app/app';
|
2021-08-17 22:08:13 +02:00
|
|
|
import { txParamsAreDappSuggested } from '../../../../shared/modules/transaction.utils';
|
2021-11-06 01:59:23 +01:00
|
|
|
import {
|
|
|
|
EDIT_GAS_MODES,
|
|
|
|
GAS_LIMITS,
|
|
|
|
GAS_RECOMMENDATIONS,
|
|
|
|
CUSTOM_GAS_ESTIMATE,
|
|
|
|
} from '../../../../shared/constants/gas';
|
2021-07-14 18:45:37 +02:00
|
|
|
|
|
|
|
import {
|
|
|
|
decGWEIToHexWEI,
|
|
|
|
decimalToHex,
|
2021-07-30 13:35:30 +02:00
|
|
|
hexToDecimal,
|
2021-07-14 18:45:37 +02:00
|
|
|
} from '../../../helpers/utils/conversions.util';
|
|
|
|
|
2021-06-28 16:45:08 +02:00
|
|
|
import Popover from '../../ui/popover';
|
|
|
|
import Button from '../../ui/button';
|
|
|
|
import EditGasDisplay from '../edit-gas-display';
|
|
|
|
import EditGasDisplayEducation from '../edit-gas-display-education';
|
|
|
|
|
|
|
|
import { I18nContext } from '../../../contexts/i18n';
|
2021-07-12 21:35:54 +02:00
|
|
|
import {
|
|
|
|
createCancelTransaction,
|
|
|
|
createSpeedUpTransaction,
|
|
|
|
hideModal,
|
|
|
|
updateTransaction,
|
2021-07-30 13:35:30 +02:00
|
|
|
updateCustomSwapsEIP1559GasParams,
|
2021-08-06 21:31:30 +02:00
|
|
|
updateSwapsUserFeeLevel,
|
2021-07-12 21:35:54 +02:00
|
|
|
} from '../../../store/actions';
|
2021-07-26 17:24:44 +02:00
|
|
|
import LoadingHeartBeat from '../../ui/loading-heartbeat';
|
2021-08-03 00:52:18 +02:00
|
|
|
import { checkNetworkAndAccountSupports1559 } from '../../../selectors';
|
2021-09-15 17:59:51 +02:00
|
|
|
import { useIncrementedGasFees } from '../../../hooks/useIncrementedGasFees';
|
2021-10-06 20:29:57 +02:00
|
|
|
import { isLegacyTransaction } from '../../../helpers/utils/transactions.util';
|
2021-07-12 21:35:54 +02:00
|
|
|
|
2021-06-28 16:45:08 +02:00
|
|
|
export default function EditGasPopover({
|
2021-07-14 18:45:37 +02:00
|
|
|
popoverTitle = '',
|
|
|
|
confirmButtonText = '',
|
|
|
|
editGasDisplayProps = {},
|
2021-11-06 01:59:23 +01:00
|
|
|
defaultEstimateToUse = GAS_RECOMMENDATIONS.MEDIUM,
|
2021-07-12 21:35:54 +02:00
|
|
|
transaction,
|
|
|
|
mode,
|
2021-07-08 22:23:00 +02:00
|
|
|
onClose,
|
2021-07-31 15:02:14 +02:00
|
|
|
minimumGasLimit = GAS_LIMITS.SIMPLE,
|
2021-06-28 16:45:08 +02:00
|
|
|
}) {
|
|
|
|
const t = useContext(I18nContext);
|
|
|
|
const dispatch = useDispatch();
|
2021-10-06 20:29:57 +02:00
|
|
|
const supportsEIP1559 =
|
|
|
|
useSelector(checkNetworkAndAccountSupports1559) &&
|
|
|
|
!isLegacyTransaction(transaction?.txParams);
|
2021-08-06 01:59:58 +02:00
|
|
|
const gasLoadingAnimationIsShowing = useSelector(
|
|
|
|
getGasLoadingAnimationIsShowing,
|
|
|
|
);
|
2021-07-26 17:24:44 +02:00
|
|
|
|
2021-07-15 19:22:55 +02:00
|
|
|
const showEducationButton =
|
2021-08-06 00:10:04 +02:00
|
|
|
(mode === EDIT_GAS_MODES.MODIFY_IN_PLACE ||
|
|
|
|
mode === EDIT_GAS_MODES.SWAPS) &&
|
2021-10-06 20:29:57 +02:00
|
|
|
supportsEIP1559;
|
2021-06-28 16:45:08 +02:00
|
|
|
const [showEducationContent, setShowEducationContent] = useState(false);
|
|
|
|
|
2021-07-14 18:45:37 +02:00
|
|
|
const [warning] = useState(null);
|
|
|
|
|
|
|
|
const [
|
|
|
|
dappSuggestedGasFeeAcknowledged,
|
|
|
|
setDappSuggestedGasFeeAcknowledged,
|
|
|
|
] = useState(false);
|
|
|
|
|
2021-07-30 13:35:30 +02:00
|
|
|
const minimumGasLimitDec = hexToDecimal(minimumGasLimit);
|
2021-09-15 17:59:51 +02:00
|
|
|
const updatedCustomGasSettings = useIncrementedGasFees(transaction);
|
|
|
|
|
|
|
|
let updatedTransaction = transaction;
|
|
|
|
if (mode === EDIT_GAS_MODES.SPEED_UP || mode === EDIT_GAS_MODES.CANCEL) {
|
|
|
|
updatedTransaction = {
|
|
|
|
...transaction,
|
2021-11-06 01:59:23 +01:00
|
|
|
userFeeLevel: CUSTOM_GAS_ESTIMATE,
|
2021-09-15 17:59:51 +02:00
|
|
|
txParams: {
|
|
|
|
...transaction.txParams,
|
|
|
|
...updatedCustomGasSettings,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2021-07-30 13:35:30 +02:00
|
|
|
|
2021-07-14 18:45:37 +02:00
|
|
|
const {
|
|
|
|
maxPriorityFeePerGas,
|
|
|
|
setMaxPriorityFeePerGas,
|
|
|
|
maxPriorityFeePerGasFiat,
|
|
|
|
maxFeePerGas,
|
|
|
|
setMaxFeePerGas,
|
|
|
|
maxFeePerGasFiat,
|
|
|
|
estimatedMaximumNative,
|
2021-08-03 19:51:02 +02:00
|
|
|
estimatedMinimumNative,
|
2021-07-14 18:45:37 +02:00
|
|
|
isGasEstimatesLoading,
|
|
|
|
gasEstimateType,
|
|
|
|
gasPrice,
|
|
|
|
setGasPrice,
|
|
|
|
gasLimit,
|
|
|
|
setGasLimit,
|
|
|
|
estimateToUse,
|
|
|
|
setEstimateToUse,
|
|
|
|
estimatedMinimumFiat,
|
|
|
|
estimatedMaximumFiat,
|
2021-07-20 21:34:32 +02:00
|
|
|
hasGasErrors,
|
|
|
|
gasErrors,
|
2021-08-06 23:37:18 +02:00
|
|
|
gasWarnings,
|
2021-07-22 18:32:59 +02:00
|
|
|
onManualChange,
|
2021-07-31 00:46:31 +02:00
|
|
|
balanceError,
|
2021-08-02 23:49:07 +02:00
|
|
|
estimatesUnavailableWarning,
|
2021-08-07 00:18:53 +02:00
|
|
|
estimatedBaseFee,
|
2021-09-15 17:59:51 +02:00
|
|
|
} = useGasFeeInputs(
|
|
|
|
defaultEstimateToUse,
|
|
|
|
updatedTransaction,
|
|
|
|
minimumGasLimit,
|
|
|
|
mode,
|
|
|
|
);
|
2021-07-14 18:45:37 +02:00
|
|
|
|
2021-08-17 22:08:13 +02:00
|
|
|
const txParamsHaveBeenCustomized =
|
2021-11-06 01:59:23 +01:00
|
|
|
estimateToUse === CUSTOM_GAS_ESTIMATE ||
|
|
|
|
txParamsAreDappSuggested(updatedTransaction);
|
2021-08-17 22:08:13 +02:00
|
|
|
|
2021-06-28 16:45:08 +02:00
|
|
|
/**
|
|
|
|
* Temporary placeholder, this should be managed by the parent component but
|
2021-09-09 17:14:07 +02:00
|
|
|
* we will be extracting this component from the hard to maintain modal
|
|
|
|
* component. For now this is just to be able to appropriately close
|
2021-06-28 16:45:08 +02:00
|
|
|
* the modal in testing
|
|
|
|
*/
|
|
|
|
const closePopover = useCallback(() => {
|
2021-07-08 22:23:00 +02:00
|
|
|
if (onClose) {
|
|
|
|
onClose();
|
2021-06-28 16:45:08 +02:00
|
|
|
} else {
|
|
|
|
dispatch(hideModal());
|
|
|
|
}
|
2021-09-09 17:14:07 +02:00
|
|
|
}, [onClose, dispatch]);
|
2021-06-28 16:45:08 +02:00
|
|
|
|
2021-07-12 21:35:54 +02:00
|
|
|
const onSubmit = useCallback(() => {
|
2021-09-15 17:59:51 +02:00
|
|
|
if (!updatedTransaction || !mode) {
|
2021-07-12 21:35:54 +02:00
|
|
|
closePopover();
|
|
|
|
}
|
2021-07-14 18:45:37 +02:00
|
|
|
|
2021-10-22 22:42:20 +02:00
|
|
|
const newGasSettings = {
|
|
|
|
gas: decimalToHex(gasLimit),
|
|
|
|
gasLimit: decimalToHex(gasLimit),
|
|
|
|
estimateSuggested: defaultEstimateToUse,
|
|
|
|
estimateUsed: estimateToUse,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (supportsEIP1559) {
|
|
|
|
newGasSettings.maxFeePerGas = decGWEIToHexWEI(maxFeePerGas ?? gasPrice);
|
|
|
|
newGasSettings.maxPriorityFeePerGas = decGWEIToHexWEI(
|
|
|
|
maxPriorityFeePerGas ?? maxFeePerGas ?? gasPrice,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
newGasSettings.gasPrice = decGWEIToHexWEI(gasPrice);
|
|
|
|
}
|
2021-07-14 18:45:37 +02:00
|
|
|
|
2021-09-15 17:59:51 +02:00
|
|
|
const cleanTransactionParams = { ...updatedTransaction.txParams };
|
2021-08-17 22:08:13 +02:00
|
|
|
|
2021-10-06 20:29:57 +02:00
|
|
|
if (supportsEIP1559) {
|
2021-08-17 22:08:13 +02:00
|
|
|
delete cleanTransactionParams.gasPrice;
|
|
|
|
}
|
|
|
|
|
2021-08-06 21:31:30 +02:00
|
|
|
const updatedTxMeta = {
|
2021-09-15 17:59:51 +02:00
|
|
|
...updatedTransaction,
|
2021-11-06 01:59:23 +01:00
|
|
|
userFeeLevel: estimateToUse || CUSTOM_GAS_ESTIMATE,
|
2021-08-06 21:31:30 +02:00
|
|
|
txParams: {
|
2021-08-17 22:08:13 +02:00
|
|
|
...cleanTransactionParams,
|
2021-08-06 21:31:30 +02:00
|
|
|
...newGasSettings,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-07-12 21:35:54 +02:00
|
|
|
switch (mode) {
|
2021-07-14 18:45:37 +02:00
|
|
|
case EDIT_GAS_MODES.CANCEL:
|
2021-08-07 00:18:53 +02:00
|
|
|
dispatch(
|
2021-09-15 17:59:51 +02:00
|
|
|
createCancelTransaction(updatedTransaction.id, newGasSettings, {
|
2021-08-07 00:18:53 +02:00
|
|
|
estimatedBaseFee,
|
|
|
|
}),
|
|
|
|
);
|
2021-07-12 21:35:54 +02:00
|
|
|
break;
|
2021-07-14 18:45:37 +02:00
|
|
|
case EDIT_GAS_MODES.SPEED_UP:
|
2021-08-07 00:18:53 +02:00
|
|
|
dispatch(
|
2021-09-15 17:59:51 +02:00
|
|
|
createSpeedUpTransaction(updatedTransaction.id, newGasSettings, {
|
2021-08-07 00:18:53 +02:00
|
|
|
estimatedBaseFee,
|
|
|
|
}),
|
|
|
|
);
|
2021-07-12 21:35:54 +02:00
|
|
|
break;
|
2021-07-14 18:45:37 +02:00
|
|
|
case EDIT_GAS_MODES.MODIFY_IN_PLACE:
|
2021-08-06 21:31:30 +02:00
|
|
|
dispatch(updateTransaction(updatedTxMeta));
|
2021-07-12 21:35:54 +02:00
|
|
|
break;
|
2021-07-30 13:35:30 +02:00
|
|
|
case EDIT_GAS_MODES.SWAPS:
|
|
|
|
// This popover component should only be used for the "FEE_MARKET" type in Swaps.
|
2021-10-06 20:29:57 +02:00
|
|
|
if (supportsEIP1559) {
|
2021-11-06 01:59:23 +01:00
|
|
|
dispatch(
|
|
|
|
updateSwapsUserFeeLevel(estimateToUse || CUSTOM_GAS_ESTIMATE),
|
|
|
|
);
|
2021-07-30 13:35:30 +02:00
|
|
|
dispatch(updateCustomSwapsEIP1559GasParams(newGasSettings));
|
|
|
|
}
|
|
|
|
break;
|
2021-07-12 21:35:54 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
closePopover();
|
2021-07-14 18:45:37 +02:00
|
|
|
}, [
|
2021-09-15 17:59:51 +02:00
|
|
|
updatedTransaction,
|
2021-07-14 18:45:37 +02:00
|
|
|
mode,
|
|
|
|
dispatch,
|
|
|
|
closePopover,
|
|
|
|
gasLimit,
|
|
|
|
gasPrice,
|
|
|
|
maxFeePerGas,
|
|
|
|
maxPriorityFeePerGas,
|
2021-10-06 20:29:57 +02:00
|
|
|
supportsEIP1559,
|
2021-08-06 21:31:30 +02:00
|
|
|
estimateToUse,
|
2021-08-07 00:18:53 +02:00
|
|
|
estimatedBaseFee,
|
2021-10-22 22:42:20 +02:00
|
|
|
defaultEstimateToUse,
|
2021-07-14 18:45:37 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
let title = t('editGasTitle');
|
|
|
|
if (popoverTitle) {
|
|
|
|
title = popoverTitle;
|
|
|
|
} else if (showEducationContent) {
|
|
|
|
title = t('editGasEducationModalTitle');
|
|
|
|
} else if (mode === EDIT_GAS_MODES.SPEED_UP) {
|
|
|
|
title = t('speedUpPopoverTitle');
|
|
|
|
} else if (mode === EDIT_GAS_MODES.CANCEL) {
|
|
|
|
title = t('cancelPopoverTitle');
|
|
|
|
}
|
2021-07-12 21:35:54 +02:00
|
|
|
|
2021-06-28 16:45:08 +02:00
|
|
|
const footerButtonText = confirmButtonText || t('save');
|
|
|
|
return (
|
|
|
|
<Popover
|
|
|
|
title={title}
|
|
|
|
onClose={closePopover}
|
2021-07-31 00:46:31 +02:00
|
|
|
className="edit-gas-popover__wrapper"
|
2021-06-28 16:45:08 +02:00
|
|
|
onBack={
|
|
|
|
showEducationContent ? () => setShowEducationContent(false) : undefined
|
|
|
|
}
|
|
|
|
footer={
|
2021-07-16 18:19:09 +02:00
|
|
|
showEducationContent ? null : (
|
|
|
|
<>
|
|
|
|
<Button
|
|
|
|
type="primary"
|
|
|
|
onClick={onSubmit}
|
2021-08-06 01:59:58 +02:00
|
|
|
disabled={
|
|
|
|
hasGasErrors ||
|
|
|
|
balanceError ||
|
2021-08-17 00:45:55 +02:00
|
|
|
((isGasEstimatesLoading || gasLoadingAnimationIsShowing) &&
|
2021-08-17 22:08:13 +02:00
|
|
|
!txParamsHaveBeenCustomized)
|
2021-08-06 01:59:58 +02:00
|
|
|
}
|
2021-07-16 18:19:09 +02:00
|
|
|
>
|
|
|
|
{footerButtonText}
|
|
|
|
</Button>
|
|
|
|
</>
|
|
|
|
)
|
2021-06-28 16:45:08 +02:00
|
|
|
}
|
|
|
|
>
|
2021-07-26 17:24:44 +02:00
|
|
|
<div style={{ padding: '0 20px 20px 20px', position: 'relative' }}>
|
2021-06-28 16:45:08 +02:00
|
|
|
{showEducationContent ? (
|
|
|
|
<EditGasDisplayEducation />
|
|
|
|
) : (
|
2021-07-26 17:24:44 +02:00
|
|
|
<>
|
2021-08-06 01:59:58 +02:00
|
|
|
{process.env.IN_TEST === 'true' ? null : <LoadingHeartBeat />}
|
2021-07-26 17:24:44 +02:00
|
|
|
<EditGasDisplay
|
|
|
|
showEducationButton={showEducationButton}
|
|
|
|
warning={warning}
|
|
|
|
dappSuggestedGasFeeAcknowledged={dappSuggestedGasFeeAcknowledged}
|
|
|
|
setDappSuggestedGasFeeAcknowledged={
|
|
|
|
setDappSuggestedGasFeeAcknowledged
|
|
|
|
}
|
|
|
|
maxPriorityFeePerGas={maxPriorityFeePerGas}
|
|
|
|
setMaxPriorityFeePerGas={setMaxPriorityFeePerGas}
|
|
|
|
maxPriorityFeePerGasFiat={maxPriorityFeePerGasFiat}
|
|
|
|
maxFeePerGas={maxFeePerGas}
|
|
|
|
setMaxFeePerGas={setMaxFeePerGas}
|
|
|
|
maxFeePerGasFiat={maxFeePerGasFiat}
|
|
|
|
estimatedMaximumNative={estimatedMaximumNative}
|
2021-08-03 19:51:02 +02:00
|
|
|
estimatedMinimumNative={estimatedMinimumNative}
|
2021-07-26 17:24:44 +02:00
|
|
|
isGasEstimatesLoading={isGasEstimatesLoading}
|
|
|
|
gasEstimateType={gasEstimateType}
|
|
|
|
gasPrice={gasPrice}
|
|
|
|
setGasPrice={setGasPrice}
|
|
|
|
gasLimit={gasLimit}
|
|
|
|
setGasLimit={setGasLimit}
|
|
|
|
estimateToUse={estimateToUse}
|
|
|
|
setEstimateToUse={setEstimateToUse}
|
|
|
|
estimatedMinimumFiat={estimatedMinimumFiat}
|
|
|
|
estimatedMaximumFiat={estimatedMaximumFiat}
|
|
|
|
onEducationClick={() => setShowEducationContent(true)}
|
|
|
|
mode={mode}
|
2021-09-15 17:59:51 +02:00
|
|
|
transaction={updatedTransaction}
|
2021-07-26 17:24:44 +02:00
|
|
|
gasErrors={gasErrors}
|
2021-08-06 23:37:18 +02:00
|
|
|
gasWarnings={gasWarnings}
|
2021-07-26 17:24:44 +02:00
|
|
|
onManualChange={onManualChange}
|
2021-07-30 13:35:30 +02:00
|
|
|
minimumGasLimit={minimumGasLimitDec}
|
2021-07-31 00:46:31 +02:00
|
|
|
balanceError={balanceError}
|
2021-08-02 23:49:07 +02:00
|
|
|
estimatesUnavailableWarning={estimatesUnavailableWarning}
|
2021-08-04 23:34:42 +02:00
|
|
|
hasGasErrors={hasGasErrors}
|
2021-08-17 22:08:13 +02:00
|
|
|
txParamsHaveBeenCustomized={txParamsHaveBeenCustomized}
|
2021-07-26 17:24:44 +02:00
|
|
|
{...editGasDisplayProps}
|
|
|
|
/>
|
|
|
|
</>
|
2021-06-28 16:45:08 +02:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</Popover>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
EditGasPopover.propTypes = {
|
|
|
|
popoverTitle: PropTypes.string,
|
|
|
|
editGasDisplayProps: PropTypes.object,
|
|
|
|
confirmButtonText: PropTypes.string,
|
2021-07-08 22:23:00 +02:00
|
|
|
onClose: PropTypes.func,
|
2021-07-12 21:35:54 +02:00
|
|
|
transaction: PropTypes.object,
|
2021-07-14 18:45:37 +02:00
|
|
|
mode: PropTypes.oneOf(Object.values(EDIT_GAS_MODES)),
|
|
|
|
defaultEstimateToUse: PropTypes.string,
|
2021-07-30 13:35:30 +02:00
|
|
|
minimumGasLimit: PropTypes.string,
|
2021-06-28 16:45:08 +02:00
|
|
|
};
|