2021-05-18 18:36:05 +02:00
|
|
|
import { useDispatch, useSelector } from 'react-redux';
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
import { useCallback } from 'react';
|
|
|
|
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';
|
|
|
|
import { increaseLastGasPrice } from '../helpers/utils/confirm-tx.util';
|
2021-05-18 18:36:05 +02:00
|
|
|
import { getIsMainnet } from '../selectors';
|
2021-02-04 19:15:23 +01:00
|
|
|
import { useMetricEvent } from './useMetricEvent';
|
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
|
|
|
|
* @return {Function}
|
|
|
|
*/
|
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);
|
2020-06-01 19:27:51 +02:00
|
|
|
// Signature requests do not have a txParams, but this hook is called indiscriminately
|
2021-02-04 19:15:23 +01:00
|
|
|
const gasPrice = primaryTransaction.txParams?.gasPrice;
|
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();
|
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();
|
|
|
|
await dispatch(fetchBasicGasEstimates);
|
2021-03-12 20:26:07 +01:00
|
|
|
const transaction = primaryTransaction;
|
2021-02-04 19:15:23 +01:00
|
|
|
const increasedGasPrice = increaseLastGasPrice(gasPrice);
|
2020-11-24 16:38:04 +01:00
|
|
|
await dispatch(
|
2020-11-03 00:41:28 +01:00
|
|
|
setCustomGasPriceForRetry(
|
|
|
|
increasedGasPrice || transaction.txParams.gasPrice,
|
|
|
|
),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
dispatch(setCustomGasLimit(transaction.txParams.gas));
|
2020-11-03 00:41:28 +01:00
|
|
|
dispatch(
|
|
|
|
showSidebar({
|
|
|
|
transitionName: 'sidebar-left',
|
|
|
|
type: 'customize-gas',
|
2021-05-18 18:36:05 +02:00
|
|
|
props: { transaction, hideBasic },
|
2020-11-03 00:41:28 +01:00
|
|
|
}),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-11-03 00:41:28 +01:00
|
|
|
},
|
2021-05-18 18:36:05 +02:00
|
|
|
[dispatch, trackMetricsEvent, gasPrice, primaryTransaction, hideBasic],
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-05-26 22:49:11 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
return retryTransaction;
|
2020-05-26 22:49:11 +02:00
|
|
|
}
|