2020-05-26 22:49:11 +02:00
|
|
|
import { useDispatch, useSelector } from 'react-redux'
|
|
|
|
import { useCallback } from 'react'
|
|
|
|
import { showModal } from '../store/actions'
|
|
|
|
import { isBalanceSufficient } from '../pages/send/send.utils'
|
2020-11-03 00:41:28 +01:00
|
|
|
import {
|
|
|
|
getHexGasTotal,
|
|
|
|
increaseLastGasPrice,
|
|
|
|
} from '../helpers/utils/confirm-tx.util'
|
2020-05-26 22:49:11 +02:00
|
|
|
import { getConversionRate, getSelectedAccount } from '../selectors'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine whether a transaction can be cancelled and provide a method to
|
|
|
|
* kick off the process of cancellation.
|
|
|
|
*
|
|
|
|
* Provides a reusable hook that, given a transactionGroup, will return
|
|
|
|
* whether or not the account has enough funds to cover the gas cancellation
|
|
|
|
* fee, and a method for beginning the cancellation process
|
|
|
|
* @param {Object} transactionGroup
|
|
|
|
* @return {[boolean, Function]}
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
export function useCancelTransaction(transactionGroup) {
|
2020-05-26 22:49:11 +02:00
|
|
|
const { primaryTransaction, initialTransaction } = transactionGroup
|
2020-11-06 22:18:00 +01:00
|
|
|
const gasPrice = primaryTransaction.txParams?.gasPrice?.startsWith('-')
|
|
|
|
? '0x0'
|
|
|
|
: primaryTransaction.txParams?.gasPrice
|
2020-08-18 22:06:58 +02:00
|
|
|
const { id } = initialTransaction
|
2020-05-26 22:49:11 +02:00
|
|
|
const dispatch = useDispatch()
|
|
|
|
const selectedAccount = useSelector(getSelectedAccount)
|
|
|
|
const conversionRate = useSelector(getConversionRate)
|
2020-11-03 00:41:28 +01:00
|
|
|
const cancelTransaction = useCallback(
|
|
|
|
(event) => {
|
|
|
|
event.stopPropagation()
|
2020-05-27 21:45:20 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
return dispatch(
|
|
|
|
showModal({
|
|
|
|
name: 'CANCEL_TRANSACTION',
|
|
|
|
transactionId: id,
|
|
|
|
originalGasPrice: gasPrice,
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
[dispatch, id, gasPrice],
|
|
|
|
)
|
2020-05-26 22:49:11 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const hasEnoughCancelGas =
|
|
|
|
primaryTransaction.txParams &&
|
|
|
|
isBalanceSufficient({
|
|
|
|
amount: '0x0',
|
|
|
|
gasTotal: getHexGasTotal({
|
|
|
|
gasPrice: increaseLastGasPrice(gasPrice),
|
|
|
|
gasLimit: primaryTransaction.txParams.gas,
|
|
|
|
}),
|
|
|
|
balance: selectedAccount.balance,
|
|
|
|
conversionRate,
|
|
|
|
})
|
2020-05-26 22:49:11 +02:00
|
|
|
|
|
|
|
return [hasEnoughCancelGas, cancelTransaction]
|
|
|
|
}
|