2020-05-26 22:49:11 +02:00
|
|
|
import { useDispatch } from 'react-redux'
|
|
|
|
import { useCallback } from 'react'
|
2020-05-29 19:46:10 +02:00
|
|
|
import { showSidebar } from '../store/actions'
|
2020-05-26 22:49:11 +02:00
|
|
|
import {
|
|
|
|
fetchBasicGasAndTimeEstimates,
|
|
|
|
fetchGasEstimates,
|
|
|
|
setCustomGasPriceForRetry,
|
|
|
|
setCustomGasLimit,
|
|
|
|
} from '../ducks/gas/gas.duck'
|
|
|
|
import { increaseLastGasPrice } from '../helpers/utils/confirm-tx.util'
|
|
|
|
import { useMetricEvent } from './useMetricEvent'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2020-05-26 22:49:11 +02:00
|
|
|
const { primaryTransaction, initialTransaction } = transactionGroup
|
2020-06-01 19:27:51 +02:00
|
|
|
// Signature requests do not have a txParams, but this hook is called indiscriminately
|
|
|
|
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"',
|
|
|
|
},
|
2020-11-03 00:41:28 +01:00
|
|
|
})
|
2020-05-26 22:49:11 +02:00
|
|
|
const dispatch = useDispatch()
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const retryTransaction = useCallback(
|
|
|
|
async (event) => {
|
|
|
|
event.stopPropagation()
|
2020-05-26 22:49:11 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
trackMetricsEvent()
|
|
|
|
const basicEstimates = await dispatch(fetchBasicGasAndTimeEstimates)
|
|
|
|
await dispatch(fetchGasEstimates(basicEstimates.blockTime))
|
|
|
|
const transaction = initialTransaction
|
|
|
|
const increasedGasPrice = increaseLastGasPrice(gasPrice)
|
|
|
|
dispatch(
|
|
|
|
setCustomGasPriceForRetry(
|
|
|
|
increasedGasPrice || transaction.txParams.gasPrice,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
dispatch(setCustomGasLimit(transaction.txParams.gas))
|
|
|
|
dispatch(
|
|
|
|
showSidebar({
|
|
|
|
transitionName: 'sidebar-left',
|
|
|
|
type: 'customize-gas',
|
|
|
|
props: { transaction },
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
[dispatch, trackMetricsEvent, initialTransaction, gasPrice],
|
|
|
|
)
|
2020-05-26 22:49:11 +02:00
|
|
|
|
|
|
|
return retryTransaction
|
|
|
|
}
|