2021-02-04 19:15:23 +01:00
|
|
|
import { useDispatch, useSelector } from 'react-redux';
|
2021-07-12 21:35:54 +02:00
|
|
|
import { useCallback, useState } from 'react';
|
2021-03-12 20:26:07 +01:00
|
|
|
import { showModal, showSidebar } from '../store/actions';
|
2021-02-04 19:15:23 +01:00
|
|
|
import { isBalanceSufficient } from '../pages/send/send.utils';
|
2021-06-08 18:03:59 +02:00
|
|
|
import { getSelectedAccount, getIsMainnet } from '../selectors';
|
|
|
|
import { getConversionRate } from '../ducks/metamask/metamask';
|
|
|
|
|
2021-07-16 18:06:32 +02:00
|
|
|
import { setCustomGasLimit, setCustomGasPrice } from '../ducks/gas/gas.duck';
|
2021-06-08 17:25:48 +02:00
|
|
|
import { GAS_LIMITS } from '../../shared/constants/gas';
|
2021-07-08 20:48:23 +02:00
|
|
|
import { isLegacyTransaction } from '../../shared/modules/transaction.utils';
|
|
|
|
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-08 20:48:23 +02:00
|
|
|
const customGasSettings = useIncrementedGasFees(transactionGroup);
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const dispatch = useDispatch();
|
|
|
|
const selectedAccount = useSelector(getSelectedAccount);
|
|
|
|
const conversionRate = useSelector(getConversionRate);
|
2021-05-18 18:36:05 +02:00
|
|
|
const isMainnet = useSelector(getIsMainnet);
|
|
|
|
const hideBasic = !(isMainnet || process.env.IN_TEST);
|
2021-07-12 21:35:54 +02:00
|
|
|
|
|
|
|
const [showCancelEditGasPopover, setShowCancelEditGasPopover] = useState(
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
|
|
|
|
const closeCancelEditGasPopover = () => setShowCancelEditGasPopover(false);
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const cancelTransaction = useCallback(
|
|
|
|
(event) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
event.stopPropagation();
|
2021-07-12 21:35:54 +02:00
|
|
|
if (process.env.SHOW_EIP_1559_UI) {
|
|
|
|
return setShowCancelEditGasPopover(true);
|
|
|
|
}
|
2021-07-08 20:48:23 +02:00
|
|
|
if (isLegacyTransaction(primaryTransaction)) {
|
|
|
|
// To support the current process of cancelling or speeding up
|
|
|
|
// a transaction, we have to inform the custom gas state of the new
|
|
|
|
// gasPrice/gasLimit to start at.
|
2021-07-16 18:06:32 +02:00
|
|
|
dispatch(setCustomGasPrice(customGasSettings.gasPrice));
|
2021-07-08 20:48:23 +02:00
|
|
|
dispatch(setCustomGasLimit(GAS_LIMITS.SIMPLE));
|
|
|
|
}
|
2021-03-12 20:26:07 +01:00
|
|
|
const tx = {
|
2021-07-08 20:48:23 +02:00
|
|
|
...primaryTransaction,
|
2021-03-12 20:26:07 +01:00
|
|
|
txParams: {
|
2021-07-08 20:48:23 +02:00
|
|
|
...primaryTransaction.txParams,
|
2021-06-08 17:25:48 +02:00
|
|
|
gas: GAS_LIMITS.SIMPLE,
|
2021-03-12 20:26:07 +01:00
|
|
|
value: '0x0',
|
|
|
|
},
|
|
|
|
};
|
2020-11-03 00:41:28 +01:00
|
|
|
return dispatch(
|
2021-03-12 20:26:07 +01:00
|
|
|
showSidebar({
|
|
|
|
transitionName: 'sidebar-left',
|
|
|
|
type: 'customize-gas',
|
|
|
|
props: {
|
2021-05-18 18:36:05 +02:00
|
|
|
hideBasic,
|
2021-03-12 20:26:07 +01:00
|
|
|
transaction: tx,
|
2021-07-08 20:48:23 +02:00
|
|
|
onSubmit: (newGasSettings) => {
|
|
|
|
const userCustomizedGasTotal = getMaximumGasTotalInHexWei(
|
|
|
|
newGasSettings,
|
|
|
|
);
|
2021-03-12 20:26:07 +01:00
|
|
|
dispatch(
|
|
|
|
showModal({
|
|
|
|
name: 'CANCEL_TRANSACTION',
|
|
|
|
newGasFee: userCustomizedGasTotal,
|
2021-07-08 20:48:23 +02:00
|
|
|
transactionId: primaryTransaction.id,
|
|
|
|
customGasSettings: newGasSettings,
|
2021-03-12 20:26:07 +01:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
},
|
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-07-08 20:48:23 +02:00
|
|
|
[dispatch, primaryTransaction, customGasSettings, hideBasic],
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
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-08 20:48:23 +02:00
|
|
|
gasTotal: getMaximumGasTotalInHexWei(customGasSettings),
|
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-12 21:35:54 +02:00
|
|
|
return [
|
|
|
|
hasEnoughCancelGas,
|
|
|
|
{ cancelTransaction, showCancelEditGasPopover, closeCancelEditGasPopover },
|
|
|
|
];
|
2020-05-26 22:49:11 +02:00
|
|
|
}
|