2021-11-18 18:54:58 +01:00
|
|
|
import classNames from 'classnames';
|
2022-01-06 03:47:26 +01:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React from 'react';
|
2021-11-18 18:54:58 +01:00
|
|
|
|
2021-12-12 00:26:28 +01:00
|
|
|
import {
|
2023-01-27 19:28:03 +01:00
|
|
|
EditGasModes,
|
|
|
|
PriorityLevels,
|
2021-12-12 00:26:28 +01:00
|
|
|
} from '../../../../../shared/constants/gas';
|
2021-11-18 18:54:58 +01:00
|
|
|
import { PRIORITY_LEVEL_ICON_MAP } from '../../../../helpers/constants/gas';
|
|
|
|
import { PRIMARY } from '../../../../helpers/constants/common';
|
|
|
|
import { toHumanReadableTime } from '../../../../helpers/utils/util';
|
|
|
|
import { useGasFeeContext } from '../../../../contexts/gasFee';
|
|
|
|
import { useI18nContext } from '../../../../hooks/useI18nContext';
|
2022-02-01 18:53:03 +01:00
|
|
|
import { useTransactionEventFragment } from '../../../../hooks/useTransactionEventFragment';
|
2021-11-23 18:46:33 +01:00
|
|
|
import { useTransactionModalContext } from '../../../../contexts/transaction-modal';
|
2021-12-03 16:44:23 +01:00
|
|
|
import InfoTooltip from '../../../ui/info-tooltip';
|
2022-01-06 03:47:26 +01:00
|
|
|
import LoadingHeartBeat from '../../../ui/loading-heartbeat';
|
|
|
|
import UserPreferencedCurrencyDisplay from '../../user-preferenced-currency-display';
|
2022-02-01 18:53:03 +01:00
|
|
|
import EditGasToolTip from '../edit-gas-tooltip/edit-gas-tooltip';
|
2022-01-06 03:47:26 +01:00
|
|
|
|
|
|
|
import { useGasItemFeeDetails } from './useGasItemFeeDetails';
|
|
|
|
|
2022-01-06 23:40:31 +01:00
|
|
|
const getTitleAndIcon = (priorityLevel, editGasMode) => {
|
2022-01-06 03:47:26 +01:00
|
|
|
let icon = priorityLevel;
|
2022-01-06 23:40:31 +01:00
|
|
|
let title = priorityLevel;
|
2023-01-27 19:28:03 +01:00
|
|
|
if (priorityLevel === PriorityLevels.dAppSuggested) {
|
2022-01-06 23:40:31 +01:00
|
|
|
title = 'dappSuggestedShortLabel';
|
2023-01-27 19:28:03 +01:00
|
|
|
} else if (priorityLevel === PriorityLevels.tenPercentIncreased) {
|
2022-01-06 03:47:26 +01:00
|
|
|
icon = null;
|
2022-01-06 23:40:31 +01:00
|
|
|
title = 'tenPercentIncreased';
|
2022-01-06 03:47:26 +01:00
|
|
|
} else if (
|
2023-01-27 19:28:03 +01:00
|
|
|
priorityLevel === PriorityLevels.high &&
|
|
|
|
editGasMode === EditGasModes.swaps
|
2022-01-06 03:47:26 +01:00
|
|
|
) {
|
|
|
|
icon = 'swapSuggested';
|
2022-01-06 23:40:31 +01:00
|
|
|
title = 'swapSuggested';
|
2022-01-06 03:47:26 +01:00
|
|
|
}
|
|
|
|
return { title, icon };
|
|
|
|
};
|
2021-11-18 20:08:29 +01:00
|
|
|
|
2021-11-23 18:46:33 +01:00
|
|
|
const EditGasItem = ({ priorityLevel }) => {
|
2021-11-18 18:54:58 +01:00
|
|
|
const {
|
2021-12-12 00:26:28 +01:00
|
|
|
editGasMode,
|
2021-11-18 18:54:58 +01:00
|
|
|
estimateUsed,
|
|
|
|
gasLimit,
|
2022-01-06 23:40:31 +01:00
|
|
|
updateTransactionToTenPercentIncreasedGasFee,
|
2022-01-06 03:47:26 +01:00
|
|
|
updateTransactionUsingDAPPSuggestedValues,
|
|
|
|
updateTransactionUsingEstimate,
|
2021-12-12 00:26:28 +01:00
|
|
|
transaction,
|
2021-11-18 18:54:58 +01:00
|
|
|
} = useGasFeeContext();
|
2022-02-01 18:53:03 +01:00
|
|
|
const { updateTransactionEventFragment } = useTransactionEventFragment();
|
2021-11-18 18:54:58 +01:00
|
|
|
const t = useI18nContext();
|
2021-11-23 18:46:33 +01:00
|
|
|
const { closeModal, openModal } = useTransactionModalContext();
|
2021-12-12 00:26:28 +01:00
|
|
|
const { dappSuggestedGasFees } = transaction;
|
2021-11-23 18:46:33 +01:00
|
|
|
|
2022-01-06 03:47:26 +01:00
|
|
|
const {
|
|
|
|
// for cancel or speedup estimateGreaterThaGasUse is true if previous gas used
|
|
|
|
// was more than estimate for the priorityLevel
|
|
|
|
estimateGreaterThanGasUse,
|
|
|
|
hexMaximumTransactionFee,
|
2021-11-18 20:08:29 +01:00
|
|
|
maxFeePerGas,
|
|
|
|
maxPriorityFeePerGas,
|
2022-01-06 03:47:26 +01:00
|
|
|
minWaitTime,
|
|
|
|
} = useGasItemFeeDetails(priorityLevel);
|
2021-11-18 18:54:58 +01:00
|
|
|
|
2022-01-06 03:47:26 +01:00
|
|
|
if (
|
2023-01-27 19:28:03 +01:00
|
|
|
priorityLevel === PriorityLevels.dAppSuggested &&
|
2022-01-18 18:37:52 +01:00
|
|
|
!dappSuggestedGasFees?.maxFeePerGas &&
|
|
|
|
!dappSuggestedGasFees?.gasPrice
|
2022-01-06 03:47:26 +01:00
|
|
|
) {
|
|
|
|
return null;
|
2021-11-18 20:08:29 +01:00
|
|
|
}
|
|
|
|
|
2021-11-18 18:54:58 +01:00
|
|
|
const onOptionSelect = () => {
|
2023-01-27 19:28:03 +01:00
|
|
|
if (priorityLevel === PriorityLevels.custom) {
|
2022-02-01 18:53:03 +01:00
|
|
|
updateTransactionEventFragment({
|
|
|
|
properties: {
|
|
|
|
gas_edit_attempted: 'advanced',
|
|
|
|
},
|
|
|
|
});
|
2021-11-23 18:46:33 +01:00
|
|
|
openModal('advancedGasFee');
|
|
|
|
} else {
|
2022-02-01 18:53:03 +01:00
|
|
|
updateTransactionEventFragment({
|
|
|
|
properties: {
|
|
|
|
gas_edit_type: 'basic',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-03-22 17:05:46 +01:00
|
|
|
closeModal(['editGasFee']);
|
2022-01-06 03:47:26 +01:00
|
|
|
|
2023-01-27 19:28:03 +01:00
|
|
|
if (priorityLevel === PriorityLevels.tenPercentIncreased) {
|
2022-01-06 23:40:31 +01:00
|
|
|
updateTransactionToTenPercentIncreasedGasFee();
|
2023-01-27 19:28:03 +01:00
|
|
|
} else if (priorityLevel === PriorityLevels.dAppSuggested) {
|
2022-01-06 03:47:26 +01:00
|
|
|
updateTransactionUsingDAPPSuggestedValues();
|
|
|
|
} else {
|
|
|
|
updateTransactionUsingEstimate(priorityLevel);
|
|
|
|
}
|
2021-11-18 20:08:29 +01:00
|
|
|
}
|
2021-11-18 18:54:58 +01:00
|
|
|
};
|
|
|
|
|
2022-01-06 23:40:31 +01:00
|
|
|
const { title, icon } = getTitleAndIcon(priorityLevel, editGasMode);
|
2021-12-12 00:26:28 +01:00
|
|
|
|
2021-11-18 18:54:58 +01:00
|
|
|
return (
|
2021-11-23 19:18:44 +01:00
|
|
|
<button
|
2021-11-18 18:54:58 +01:00
|
|
|
className={classNames('edit-gas-item', {
|
2021-12-03 16:59:48 +01:00
|
|
|
'edit-gas-item--selected': priorityLevel === estimateUsed,
|
2022-01-06 03:47:26 +01:00
|
|
|
'edit-gas-item--disabled': estimateGreaterThanGasUse,
|
2021-11-18 18:54:58 +01:00
|
|
|
})}
|
|
|
|
onClick={onOptionSelect}
|
2021-11-23 19:18:44 +01:00
|
|
|
aria-label={priorityLevel}
|
|
|
|
autoFocus={priorityLevel === estimateUsed}
|
2022-01-06 03:47:26 +01:00
|
|
|
disabled={estimateGreaterThanGasUse}
|
2022-01-19 19:34:43 +01:00
|
|
|
data-testid={`edit-gas-fee-item-${priorityLevel}`}
|
2021-11-18 18:54:58 +01:00
|
|
|
>
|
|
|
|
<span className="edit-gas-item__name">
|
2022-01-06 03:47:26 +01:00
|
|
|
{icon && (
|
|
|
|
<span
|
|
|
|
className={`edit-gas-item__icon edit-gas-item__icon-${priorityLevel}`}
|
|
|
|
>
|
|
|
|
{PRIORITY_LEVEL_ICON_MAP[icon]}
|
|
|
|
</span>
|
|
|
|
)}
|
2022-07-12 15:59:38 +02:00
|
|
|
{t(title)}
|
2021-11-18 18:54:58 +01:00
|
|
|
</span>
|
|
|
|
<span
|
2021-11-18 20:08:29 +01:00
|
|
|
className={`edit-gas-item__time-estimate edit-gas-item__time-estimate-${priorityLevel}`}
|
2021-11-18 18:54:58 +01:00
|
|
|
>
|
2023-01-27 19:28:03 +01:00
|
|
|
{editGasMode !== EditGasModes.swaps &&
|
2021-12-12 00:26:28 +01:00
|
|
|
(minWaitTime ? toHumanReadableTime(t, minWaitTime) : '--')}
|
2021-11-18 18:54:58 +01:00
|
|
|
</span>
|
|
|
|
<span
|
2021-11-18 20:08:29 +01:00
|
|
|
className={`edit-gas-item__fee-estimate edit-gas-item__fee-estimate-${priorityLevel}`}
|
2021-11-18 18:54:58 +01:00
|
|
|
>
|
2021-11-18 20:08:29 +01:00
|
|
|
{hexMaximumTransactionFee ? (
|
2021-12-10 01:50:38 +01:00
|
|
|
<div className="edit-gas-item__maxfee">
|
2022-01-22 01:23:02 +01:00
|
|
|
<LoadingHeartBeat
|
|
|
|
backgroundColor={
|
2022-03-25 16:32:36 +01:00
|
|
|
priorityLevel === estimateUsed
|
|
|
|
? 'var(--color-background-alternative)'
|
|
|
|
: 'var(--color-background-default)'
|
2022-01-22 01:23:02 +01:00
|
|
|
}
|
2022-01-26 19:18:43 +01:00
|
|
|
estimateUsed={priorityLevel}
|
2022-01-22 01:23:02 +01:00
|
|
|
/>
|
2021-12-10 01:50:38 +01:00
|
|
|
<UserPreferencedCurrencyDisplay
|
|
|
|
key="editGasSubTextFeeAmount"
|
|
|
|
type={PRIMARY}
|
|
|
|
value={hexMaximumTransactionFee}
|
|
|
|
/>
|
|
|
|
</div>
|
2021-11-18 20:08:29 +01:00
|
|
|
) : (
|
|
|
|
'--'
|
|
|
|
)}
|
2021-11-18 18:54:58 +01:00
|
|
|
</span>
|
2021-12-03 16:44:23 +01:00
|
|
|
<span className="edit-gas-item__tooltip" data-testid="gas-tooltip">
|
|
|
|
<InfoTooltip
|
|
|
|
contentText={
|
|
|
|
<EditGasToolTip
|
|
|
|
t={t}
|
|
|
|
priorityLevel={priorityLevel}
|
|
|
|
maxFeePerGas={maxFeePerGas}
|
|
|
|
maxPriorityFeePerGas={maxPriorityFeePerGas}
|
2021-12-12 00:26:28 +01:00
|
|
|
editGasMode={editGasMode}
|
|
|
|
gasLimit={gasLimit}
|
|
|
|
transaction={transaction}
|
2022-01-06 03:47:26 +01:00
|
|
|
estimateGreaterThanGasUse={estimateGreaterThanGasUse}
|
2021-12-03 16:44:23 +01:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
position="top"
|
|
|
|
/>
|
2021-11-18 18:54:58 +01:00
|
|
|
</span>
|
2021-11-23 19:18:44 +01:00
|
|
|
</button>
|
2021-11-18 18:54:58 +01:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
EditGasItem.propTypes = {
|
2021-11-18 20:08:29 +01:00
|
|
|
priorityLevel: PropTypes.string,
|
2021-11-18 18:54:58 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export default EditGasItem;
|