2021-07-31 03:29:21 +02:00
|
|
|
import { 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 { isBalanceSufficient } from '../pages/send/send.utils';
|
2021-07-31 03:29:21 +02:00
|
|
|
import { getSelectedAccount } from '../selectors';
|
2021-06-08 18:03:59 +02:00
|
|
|
import { getConversionRate } from '../ducks/metamask/metamask';
|
|
|
|
|
2021-07-08 20:48:23 +02:00
|
|
|
import { getMaximumGasTotalInHexWei } from '../../shared/modules/gas.utils';
|
|
|
|
import { useIncrementedGasFees } from './useIncrementedGasFees';
|
2020-05-26 22:49:11 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2021-03-12 20:26:07 +01:00
|
|
|
const { primaryTransaction } = transactionGroup;
|
2021-05-07 23:38:36 +02:00
|
|
|
|
2021-07-31 15:01:29 +02:00
|
|
|
const customCancelGasSettings = useIncrementedGasFees(transactionGroup);
|
2021-07-08 20:48:23 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const selectedAccount = useSelector(getSelectedAccount);
|
|
|
|
const conversionRate = useSelector(getConversionRate);
|
2021-07-12 21:35:54 +02:00
|
|
|
|
|
|
|
const [showCancelEditGasPopover, setShowCancelEditGasPopover] = useState(
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
|
|
|
|
const closeCancelEditGasPopover = () => setShowCancelEditGasPopover(false);
|
|
|
|
|
2021-07-31 03:29:21 +02:00
|
|
|
const cancelTransaction = useCallback((event) => {
|
|
|
|
event.stopPropagation();
|
|
|
|
return setShowCancelEditGasPopover(true);
|
|
|
|
}, []);
|
2020-05-26 22:49:11 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const hasEnoughCancelGas =
|
|
|
|
primaryTransaction.txParams &&
|
|
|
|
isBalanceSufficient({
|
|
|
|
amount: '0x0',
|
2021-07-31 15:01:29 +02:00
|
|
|
gasTotal: getMaximumGasTotalInHexWei(customCancelGasSettings),
|
2020-11-03 00:41:28 +01:00
|
|
|
balance: selectedAccount.balance,
|
|
|
|
conversionRate,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-05-26 22:49:11 +02:00
|
|
|
|
2021-07-31 15:01:29 +02:00
|
|
|
return {
|
2021-07-12 21:35:54 +02:00
|
|
|
hasEnoughCancelGas,
|
2021-07-31 15:01:29 +02:00
|
|
|
customCancelGasSettings,
|
|
|
|
cancelTransaction,
|
|
|
|
showCancelEditGasPopover,
|
|
|
|
closeCancelEditGasPopover,
|
|
|
|
};
|
2020-05-26 22:49:11 +02:00
|
|
|
}
|