2021-05-18 18:36:05 +02:00
|
|
|
import { useDispatch, useSelector } from 'react-redux';
|
|
|
|
|
2021-07-12 21:35:54 +02:00
|
|
|
import { useCallback, useState } from 'react';
|
2021-02-04 19:15:23 +01:00
|
|
|
import { showSidebar } from '../store/actions';
|
2020-05-26 22:49:11 +02:00
|
|
|
import {
|
2020-12-03 00:25:19 +01:00
|
|
|
fetchBasicGasEstimates,
|
2020-05-26 22:49:11 +02:00
|
|
|
setCustomGasPriceForRetry,
|
|
|
|
setCustomGasLimit,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../ducks/gas/gas.duck';
|
2021-05-18 18:36:05 +02:00
|
|
|
import { getIsMainnet } from '../selectors';
|
2021-07-08 20:48:23 +02:00
|
|
|
import { isLegacyTransaction } from '../../shared/modules/transaction.utils';
|
2021-02-04 19:15:23 +01:00
|
|
|
import { useMetricEvent } from './useMetricEvent';
|
2021-07-08 20:48:23 +02:00
|
|
|
import { useIncrementedGasFees } from './useIncrementedGasFees';
|
2021-07-12 21:35:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} RetryTransactionReturnValue
|
|
|
|
* @property {(event: Event) => void} retryTransaction - open edit gas popover
|
|
|
|
* to begin setting retry gas fees
|
|
|
|
* @property {boolean} showRetryEditGasPopover - Whether to show the popover
|
|
|
|
* @property {() => void} closeRetryEditGasPopover - close the popover.
|
|
|
|
*/
|
|
|
|
|
2020-05-26 22:49:11 +02:00
|
|
|
/**
|
|
|
|
* Provides a reusable hook that, given a transactionGroup, will return
|
|
|
|
* a method for beginning the retry process
|
|
|
|
* @param {Object} transactionGroup - the transaction group
|
2021-07-12 21:35:54 +02:00
|
|
|
* @return {RetryTransactionReturnValue}
|
2020-05-26 22:49:11 +02:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
export function useRetryTransaction(transactionGroup) {
|
2021-03-12 20:26:07 +01:00
|
|
|
const { primaryTransaction } = transactionGroup;
|
2021-05-18 18:36:05 +02:00
|
|
|
const isMainnet = useSelector(getIsMainnet);
|
|
|
|
const hideBasic = !(isMainnet || process.env.IN_TEST);
|
2021-07-08 20:48:23 +02:00
|
|
|
const customGasSettings = useIncrementedGasFees(transactionGroup);
|
2020-11-03 00:41:28 +01:00
|
|
|
const trackMetricsEvent = useMetricEvent({
|
2020-05-26 22:49:11 +02:00
|
|
|
eventOpts: {
|
|
|
|
category: 'Navigation',
|
|
|
|
action: 'Activity Log',
|
|
|
|
name: 'Clicked "Speed Up"',
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
const dispatch = useDispatch();
|
2021-07-12 21:35:54 +02:00
|
|
|
const [showRetryEditGasPopover, setShowRetryEditGasPopover] = useState(false);
|
|
|
|
|
|
|
|
const closeRetryEditGasPopover = () => setShowRetryEditGasPopover(false);
|
2020-05-26 22:49:11 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const retryTransaction = useCallback(
|
|
|
|
async (event) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
event.stopPropagation();
|
2020-05-26 22:49:11 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
trackMetricsEvent();
|
2021-07-12 21:35:54 +02:00
|
|
|
if (process.env.SHOW_EIP_1559_UI) {
|
|
|
|
setShowRetryEditGasPopover(true);
|
|
|
|
} else {
|
2021-07-08 20:48:23 +02:00
|
|
|
await dispatch(fetchBasicGasEstimates);
|
2021-07-12 21:35:54 +02:00
|
|
|
if (isLegacyTransaction(primaryTransaction)) {
|
|
|
|
// To support the current process of cancelling or speeding up
|
|
|
|
// a transaction, we have to inform the custom gas state of the new
|
|
|
|
// gasPrice to start at.
|
|
|
|
dispatch(setCustomGasPriceForRetry(customGasSettings.gasPrice));
|
|
|
|
dispatch(setCustomGasLimit(primaryTransaction.txParams.gas));
|
|
|
|
}
|
2021-07-08 20:48:23 +02:00
|
|
|
|
2021-07-12 21:35:54 +02:00
|
|
|
dispatch(
|
|
|
|
showSidebar({
|
|
|
|
transitionName: 'sidebar-left',
|
|
|
|
type: 'customize-gas',
|
|
|
|
props: { transaction: primaryTransaction, hideBasic },
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
2020-11-03 00:41:28 +01:00
|
|
|
},
|
2021-07-08 20:48:23 +02:00
|
|
|
[
|
|
|
|
dispatch,
|
|
|
|
trackMetricsEvent,
|
|
|
|
customGasSettings,
|
|
|
|
primaryTransaction,
|
|
|
|
hideBasic,
|
|
|
|
],
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-05-26 22:49:11 +02:00
|
|
|
|
2021-07-12 21:35:54 +02:00
|
|
|
return {
|
|
|
|
retryTransaction,
|
|
|
|
showRetryEditGasPopover,
|
|
|
|
closeRetryEditGasPopover,
|
|
|
|
};
|
2020-05-26 22:49:11 +02:00
|
|
|
}
|