1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-12 20:57:12 +01:00
metamask-extension/ui/hooks/useCancelTransaction.js

56 lines
1.8 KiB
JavaScript
Raw Normal View History

import { useSelector } from 'react-redux';
import { useCallback, useState } from 'react';
import { isBalanceSufficient } from '../pages/send/send.utils';
import { getSelectedAccount } from '../selectors';
import { getConversionRate } from '../ducks/metamask/metamask';
import { getMaximumGasTotalInHexWei } from '../../shared/modules/gas.utils';
import { useIncrementedGasFees } from './useIncrementedGasFees';
/**
* 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) {
const { primaryTransaction } = transactionGroup;
const customCancelGasSettings = useIncrementedGasFees(transactionGroup);
const selectedAccount = useSelector(getSelectedAccount);
const conversionRate = useSelector(getConversionRate);
const [showCancelEditGasPopover, setShowCancelEditGasPopover] = useState(
false,
);
const closeCancelEditGasPopover = () => setShowCancelEditGasPopover(false);
const cancelTransaction = useCallback((event) => {
event.stopPropagation();
return setShowCancelEditGasPopover(true);
}, []);
2020-11-03 00:41:28 +01:00
const hasEnoughCancelGas =
primaryTransaction.txParams &&
isBalanceSufficient({
amount: '0x0',
gasTotal: getMaximumGasTotalInHexWei(customCancelGasSettings),
2020-11-03 00:41:28 +01:00
balance: selectedAccount.balance,
conversionRate,
});
return {
hasEnoughCancelGas,
customCancelGasSettings,
cancelTransaction,
showCancelEditGasPopover,
closeCancelEditGasPopover,
};
}