2021-02-04 19:15:23 +01:00
|
|
|
import { useDispatch, useSelector } from 'react-redux';
|
|
|
|
import { useCallback } from 'react';
|
2021-03-12 20:26:07 +01:00
|
|
|
import { addHexPrefix } from 'ethereumjs-util';
|
|
|
|
import { showModal, showSidebar } from '../store/actions';
|
2021-02-04 19:15:23 +01:00
|
|
|
import { isBalanceSufficient } from '../pages/send/send.utils';
|
2020-11-03 00:41:28 +01:00
|
|
|
import {
|
|
|
|
getHexGasTotal,
|
|
|
|
increaseLastGasPrice,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../helpers/utils/confirm-tx.util';
|
|
|
|
import { getConversionRate, getSelectedAccount } from '../selectors';
|
2021-03-12 20:26:07 +01:00
|
|
|
import {
|
|
|
|
setCustomGasLimit,
|
|
|
|
setCustomGasPriceForRetry,
|
|
|
|
} from '../ducks/gas/gas.duck';
|
|
|
|
import { multiplyCurrencies } from '../helpers/utils/conversion-util';
|
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
|
|
|
|
|
|
|
const transactionGasPrice = primaryTransaction.txParams?.gasPrice;
|
|
|
|
const gasPrice =
|
|
|
|
transactionGasPrice === undefined || transactionGasPrice?.startsWith('-')
|
|
|
|
? '0x0'
|
|
|
|
: primaryTransaction.txParams?.gasPrice;
|
2021-03-12 20:26:07 +01:00
|
|
|
const transaction = primaryTransaction;
|
2021-02-04 19:15:23 +01:00
|
|
|
const dispatch = useDispatch();
|
|
|
|
const selectedAccount = useSelector(getSelectedAccount);
|
|
|
|
const conversionRate = useSelector(getConversionRate);
|
2021-03-12 20:26:07 +01:00
|
|
|
const defaultNewGasPrice = addHexPrefix(
|
|
|
|
multiplyCurrencies(gasPrice, 1.1, {
|
|
|
|
toNumericBase: 'hex',
|
|
|
|
multiplicandBase: 16,
|
|
|
|
multiplierBase: 10,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const cancelTransaction = useCallback(
|
|
|
|
(event) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
event.stopPropagation();
|
2021-03-12 20:26:07 +01:00
|
|
|
dispatch(setCustomGasLimit('0x5208'));
|
|
|
|
dispatch(setCustomGasPriceForRetry(defaultNewGasPrice));
|
|
|
|
const tx = {
|
|
|
|
...transaction,
|
|
|
|
txParams: {
|
|
|
|
...transaction.txParams,
|
|
|
|
gas: '0x5208',
|
|
|
|
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: {
|
|
|
|
transaction: tx,
|
|
|
|
onSubmit: (newGasLimit, newGasPrice) => {
|
|
|
|
const userCustomizedGasTotal = getHexGasTotal({
|
|
|
|
gasPrice: newGasPrice,
|
|
|
|
gasLimit: newGasLimit,
|
|
|
|
});
|
|
|
|
dispatch(
|
|
|
|
showModal({
|
|
|
|
name: 'CANCEL_TRANSACTION',
|
|
|
|
newGasFee: userCustomizedGasTotal,
|
|
|
|
transactionId: transaction.id,
|
|
|
|
defaultNewGasPrice: newGasPrice,
|
|
|
|
gasLimit: newGasLimit,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
},
|
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-03-12 20:26:07 +01:00
|
|
|
[dispatch, transaction, defaultNewGasPrice],
|
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',
|
|
|
|
gasTotal: getHexGasTotal({
|
|
|
|
gasPrice: increaseLastGasPrice(gasPrice),
|
|
|
|
gasLimit: primaryTransaction.txParams.gas,
|
|
|
|
}),
|
|
|
|
balance: selectedAccount.balance,
|
|
|
|
conversionRate,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-05-26 22:49:11 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
return [hasEnoughCancelGas, cancelTransaction];
|
2020-05-26 22:49:11 +02:00
|
|
|
}
|