You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
99 lines
3.1 KiB
99 lines
3.1 KiB
4 years ago
|
import { useDispatch, useSelector } from 'react-redux';
|
||
|
import { useCallback } from 'react';
|
||
4 years ago
|
import { addHexPrefix } from 'ethereumjs-util';
|
||
|
import { showModal, showSidebar } from '../store/actions';
|
||
4 years ago
|
import { isBalanceSufficient } from '../pages/send/send.utils';
|
||
4 years ago
|
import {
|
||
|
getHexGasTotal,
|
||
|
increaseLastGasPrice,
|
||
4 years ago
|
} from '../helpers/utils/confirm-tx.util';
|
||
|
import { getConversionRate, getSelectedAccount } from '../selectors';
|
||
4 years ago
|
import {
|
||
|
setCustomGasLimit,
|
||
|
setCustomGasPriceForRetry,
|
||
|
} from '../ducks/gas/gas.duck';
|
||
|
import { multiplyCurrencies } from '../helpers/utils/conversion-util';
|
||
5 years ago
|
|
||
|
/**
|
||
|
* 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]}
|
||
|
*/
|
||
4 years ago
|
export function useCancelTransaction(transactionGroup) {
|
||
4 years ago
|
const { primaryTransaction } = transactionGroup;
|
||
4 years ago
|
const gasPrice = primaryTransaction.txParams?.gasPrice?.startsWith('-')
|
||
|
? '0x0'
|
||
4 years ago
|
: primaryTransaction.txParams?.gasPrice;
|
||
4 years ago
|
const transaction = primaryTransaction;
|
||
4 years ago
|
const dispatch = useDispatch();
|
||
|
const selectedAccount = useSelector(getSelectedAccount);
|
||
|
const conversionRate = useSelector(getConversionRate);
|
||
4 years ago
|
const defaultNewGasPrice = addHexPrefix(
|
||
|
multiplyCurrencies(gasPrice, 1.1, {
|
||
|
toNumericBase: 'hex',
|
||
|
multiplicandBase: 16,
|
||
|
multiplierBase: 10,
|
||
|
}),
|
||
|
);
|
||
|
|
||
4 years ago
|
const cancelTransaction = useCallback(
|
||
|
(event) => {
|
||
4 years ago
|
event.stopPropagation();
|
||
4 years ago
|
dispatch(setCustomGasLimit('0x5208'));
|
||
|
dispatch(setCustomGasPriceForRetry(defaultNewGasPrice));
|
||
|
const tx = {
|
||
|
...transaction,
|
||
|
txParams: {
|
||
|
...transaction.txParams,
|
||
|
gas: '0x5208',
|
||
|
value: '0x0',
|
||
|
},
|
||
|
};
|
||
4 years ago
|
return dispatch(
|
||
4 years ago
|
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,
|
||
|
}),
|
||
|
);
|
||
|
},
|
||
|
},
|
||
4 years ago
|
}),
|
||
4 years ago
|
);
|
||
4 years ago
|
},
|
||
4 years ago
|
[dispatch, transaction, defaultNewGasPrice],
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
4 years ago
|
const hasEnoughCancelGas =
|
||
|
primaryTransaction.txParams &&
|
||
|
isBalanceSufficient({
|
||
|
amount: '0x0',
|
||
|
gasTotal: getHexGasTotal({
|
||
|
gasPrice: increaseLastGasPrice(gasPrice),
|
||
|
gasLimit: primaryTransaction.txParams.gas,
|
||
|
}),
|
||
|
balance: selectedAccount.balance,
|
||
|
conversionRate,
|
||
4 years ago
|
});
|
||
5 years ago
|
|
||
4 years ago
|
return [hasEnoughCancelGas, cancelTransaction];
|
||
5 years ago
|
}
|