2021-11-18 18:54:58 +01:00
|
|
|
import { useCallback } from 'react';
|
|
|
|
import { useDispatch } from 'react-redux';
|
2022-09-16 21:05:21 +02:00
|
|
|
import { decimalToHex } from '../../../shared/lib/transactions-controller-utils';
|
2021-11-18 18:54:58 +01:00
|
|
|
|
2021-12-12 00:26:28 +01:00
|
|
|
import { EDIT_GAS_MODES, PRIORITY_LEVELS } from '../../../shared/constants/gas';
|
2022-09-16 21:05:21 +02:00
|
|
|
import { decGWEIToHexWEI } from '../../helpers/utils/conversions.util';
|
2022-04-14 17:50:48 +02:00
|
|
|
import {
|
|
|
|
addTenPercentAndRound,
|
|
|
|
editGasModeIsSpeedUpOrCancel,
|
|
|
|
} from '../../helpers/utils/gas';
|
2021-12-12 00:26:28 +01:00
|
|
|
import {
|
2022-01-06 03:47:26 +01:00
|
|
|
createCancelTransaction,
|
|
|
|
createSpeedUpTransaction,
|
2021-12-12 00:26:28 +01:00
|
|
|
updateCustomSwapsEIP1559GasParams,
|
2022-03-25 18:11:04 +01:00
|
|
|
updatePreviousGasParams,
|
2021-12-12 00:26:28 +01:00
|
|
|
updateSwapsUserFeeLevel,
|
2022-03-25 18:11:04 +01:00
|
|
|
updateTransactionGasFees,
|
2021-12-12 00:26:28 +01:00
|
|
|
} from '../../store/actions';
|
2021-11-18 18:54:58 +01:00
|
|
|
|
|
|
|
export const useTransactionFunctions = ({
|
|
|
|
defaultEstimateToUse,
|
2021-12-12 00:26:28 +01:00
|
|
|
editGasMode,
|
2022-01-06 03:47:26 +01:00
|
|
|
estimatedBaseFee,
|
2021-12-01 01:31:21 +01:00
|
|
|
gasFeeEstimates,
|
2022-01-06 03:47:26 +01:00
|
|
|
gasLimit: gasLimitValue,
|
|
|
|
maxPriorityFeePerGas: maxPriorityFeePerGasValue,
|
2021-11-18 18:54:58 +01:00
|
|
|
transaction,
|
2022-04-14 17:50:48 +02:00
|
|
|
setRetryTxMeta,
|
2021-11-18 18:54:58 +01:00
|
|
|
}) => {
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
2022-01-06 03:47:26 +01:00
|
|
|
const getTxMeta = useCallback(() => {
|
|
|
|
if (
|
|
|
|
(editGasMode !== EDIT_GAS_MODES.CANCEL &&
|
|
|
|
editGasMode !== EDIT_GAS_MODES.SPEED_UP) ||
|
|
|
|
transaction.previousGas
|
|
|
|
) {
|
|
|
|
return {};
|
|
|
|
}
|
2022-07-26 20:10:51 +02:00
|
|
|
const { maxFeePerGas, maxPriorityFeePerGas, gasLimit } =
|
|
|
|
transaction?.txParams ?? {};
|
2022-01-06 03:47:26 +01:00
|
|
|
return {
|
|
|
|
previousGas: {
|
|
|
|
maxFeePerGas,
|
|
|
|
maxPriorityFeePerGas,
|
|
|
|
gasLimit,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}, [editGasMode, transaction?.previousGas, transaction?.txParams]);
|
|
|
|
|
|
|
|
const updateTransaction = useCallback(
|
2022-03-25 18:11:04 +01:00
|
|
|
async ({
|
2022-02-01 18:53:03 +01:00
|
|
|
estimateUsed,
|
|
|
|
gasLimit,
|
|
|
|
maxFeePerGas,
|
|
|
|
maxPriorityFeePerGas,
|
|
|
|
estimateSuggested,
|
|
|
|
}) => {
|
2021-11-18 18:54:58 +01:00
|
|
|
const newGasSettings = {
|
2022-01-06 03:47:26 +01:00
|
|
|
gas: decimalToHex(gasLimit || gasLimitValue),
|
|
|
|
gasLimit: decimalToHex(gasLimit || gasLimitValue),
|
2022-02-01 18:53:03 +01:00
|
|
|
estimateSuggested: estimateSuggested || defaultEstimateToUse,
|
2021-11-18 20:08:29 +01:00
|
|
|
estimateUsed,
|
2021-11-18 18:54:58 +01:00
|
|
|
};
|
2021-11-29 18:40:48 +01:00
|
|
|
if (maxFeePerGas) {
|
|
|
|
newGasSettings.maxFeePerGas = maxFeePerGas;
|
|
|
|
}
|
|
|
|
if (maxPriorityFeePerGas) {
|
2022-01-06 03:47:26 +01:00
|
|
|
newGasSettings.maxPriorityFeePerGas =
|
|
|
|
maxPriorityFeePerGas || decGWEIToHexWEI(maxPriorityFeePerGasValue);
|
2021-11-29 18:40:48 +01:00
|
|
|
}
|
2022-01-06 03:47:26 +01:00
|
|
|
const txMeta = getTxMeta();
|
2021-11-18 18:54:58 +01:00
|
|
|
|
|
|
|
const updatedTxMeta = {
|
|
|
|
...transaction,
|
2021-12-12 00:26:28 +01:00
|
|
|
userFeeLevel: estimateUsed || PRIORITY_LEVELS.CUSTOM,
|
2021-11-18 18:54:58 +01:00
|
|
|
txParams: {
|
|
|
|
...transaction.txParams,
|
|
|
|
...newGasSettings,
|
|
|
|
},
|
2022-01-06 03:47:26 +01:00
|
|
|
...txMeta,
|
2021-11-18 18:54:58 +01:00
|
|
|
};
|
|
|
|
|
2021-12-12 00:26:28 +01:00
|
|
|
if (editGasMode === EDIT_GAS_MODES.SWAPS) {
|
|
|
|
dispatch(
|
|
|
|
updateSwapsUserFeeLevel(estimateUsed || PRIORITY_LEVELS.CUSTOM),
|
|
|
|
);
|
|
|
|
dispatch(updateCustomSwapsEIP1559GasParams(newGasSettings));
|
2022-04-14 17:50:48 +02:00
|
|
|
} else if (editGasModeIsSpeedUpOrCancel(editGasMode)) {
|
|
|
|
setRetryTxMeta(updatedTxMeta);
|
2021-12-12 00:26:28 +01:00
|
|
|
} else {
|
2022-03-25 18:11:04 +01:00
|
|
|
newGasSettings.userEditedGasLimit = updatedTxMeta.userEditedGasLimit;
|
|
|
|
newGasSettings.userFeeLevel = updatedTxMeta.userFeeLevel;
|
|
|
|
|
|
|
|
if (txMeta && txMeta.previousGas) {
|
|
|
|
await dispatch(
|
|
|
|
updatePreviousGasParams(updatedTxMeta.id, txMeta.previousGas),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
await dispatch(
|
|
|
|
updateTransactionGasFees(updatedTxMeta.id, newGasSettings),
|
|
|
|
);
|
2021-12-12 00:26:28 +01:00
|
|
|
}
|
2021-11-18 18:54:58 +01:00
|
|
|
},
|
2021-12-12 00:26:28 +01:00
|
|
|
[
|
|
|
|
defaultEstimateToUse,
|
|
|
|
dispatch,
|
|
|
|
editGasMode,
|
2022-01-06 03:47:26 +01:00
|
|
|
gasLimitValue,
|
|
|
|
getTxMeta,
|
|
|
|
maxPriorityFeePerGasValue,
|
2021-12-12 00:26:28 +01:00
|
|
|
transaction,
|
2022-04-14 17:50:48 +02:00
|
|
|
setRetryTxMeta,
|
2021-12-12 00:26:28 +01:00
|
|
|
],
|
2021-11-18 20:08:29 +01:00
|
|
|
);
|
|
|
|
|
2022-01-06 03:47:26 +01:00
|
|
|
const cancelTransaction = useCallback(() => {
|
|
|
|
dispatch(
|
|
|
|
createCancelTransaction(transaction.id, transaction.txParams, {
|
|
|
|
estimatedBaseFee,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}, [dispatch, estimatedBaseFee, transaction]);
|
|
|
|
|
|
|
|
const speedUpTransaction = useCallback(() => {
|
|
|
|
dispatch(
|
|
|
|
createSpeedUpTransaction(transaction.id, transaction.txParams, {
|
|
|
|
estimatedBaseFee,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}, [dispatch, estimatedBaseFee, transaction]);
|
|
|
|
|
2022-02-01 18:53:03 +01:00
|
|
|
const updateTransactionToTenPercentIncreasedGasFee = useCallback(
|
|
|
|
(initTransaction = false) => {
|
2022-07-31 20:26:40 +02:00
|
|
|
const {
|
|
|
|
gas: gasLimit,
|
|
|
|
maxFeePerGas,
|
|
|
|
maxPriorityFeePerGas,
|
|
|
|
} = transaction.previousGas || transaction.txParams;
|
2022-01-06 03:47:26 +01:00
|
|
|
|
2022-02-01 18:53:03 +01:00
|
|
|
updateTransaction({
|
|
|
|
estimateSuggested: initTransaction
|
|
|
|
? defaultEstimateToUse
|
|
|
|
: PRIORITY_LEVELS.TEN_PERCENT_INCREASED,
|
|
|
|
estimateUsed: PRIORITY_LEVELS.TEN_PERCENT_INCREASED,
|
|
|
|
gasLimit,
|
|
|
|
maxFeePerGas: addTenPercentAndRound(maxFeePerGas),
|
|
|
|
maxPriorityFeePerGas: addTenPercentAndRound(maxPriorityFeePerGas),
|
|
|
|
});
|
|
|
|
},
|
|
|
|
[defaultEstimateToUse, transaction, updateTransaction],
|
|
|
|
);
|
2022-01-06 03:47:26 +01:00
|
|
|
|
|
|
|
const updateTransactionUsingEstimate = useCallback(
|
2021-11-18 20:08:29 +01:00
|
|
|
(gasFeeEstimateToUse) => {
|
2022-01-06 03:47:26 +01:00
|
|
|
if (!gasFeeEstimates[gasFeeEstimateToUse]) {
|
|
|
|
return;
|
2021-11-18 20:08:29 +01:00
|
|
|
}
|
2022-07-31 20:26:40 +02:00
|
|
|
const { suggestedMaxFeePerGas, suggestedMaxPriorityFeePerGas } =
|
|
|
|
gasFeeEstimates[gasFeeEstimateToUse];
|
2022-01-06 03:47:26 +01:00
|
|
|
updateTransaction({
|
|
|
|
estimateUsed: gasFeeEstimateToUse,
|
|
|
|
maxFeePerGas: decGWEIToHexWEI(suggestedMaxFeePerGas),
|
|
|
|
maxPriorityFeePerGas: decGWEIToHexWEI(suggestedMaxPriorityFeePerGas),
|
|
|
|
});
|
2021-11-18 20:08:29 +01:00
|
|
|
},
|
2022-01-06 03:47:26 +01:00
|
|
|
[gasFeeEstimates, updateTransaction],
|
2021-11-18 18:54:58 +01:00
|
|
|
);
|
|
|
|
|
2022-01-06 03:47:26 +01:00
|
|
|
const updateTransactionUsingDAPPSuggestedValues = useCallback(() => {
|
2022-07-26 20:10:51 +02:00
|
|
|
const { maxFeePerGas, maxPriorityFeePerGas } =
|
|
|
|
transaction?.dappSuggestedGasFees ?? {};
|
2022-01-06 03:47:26 +01:00
|
|
|
updateTransaction({
|
|
|
|
estimateUsed: PRIORITY_LEVELS.DAPP_SUGGESTED,
|
|
|
|
maxFeePerGas,
|
|
|
|
maxPriorityFeePerGas,
|
|
|
|
});
|
|
|
|
}, [transaction, updateTransaction]);
|
|
|
|
|
|
|
|
return {
|
|
|
|
cancelTransaction,
|
|
|
|
speedUpTransaction,
|
|
|
|
updateTransaction,
|
2022-01-06 23:40:31 +01:00
|
|
|
updateTransactionToTenPercentIncreasedGasFee,
|
2022-01-06 03:47:26 +01:00
|
|
|
updateTransactionUsingDAPPSuggestedValues,
|
|
|
|
updateTransactionUsingEstimate,
|
|
|
|
};
|
2021-11-18 18:54:58 +01:00
|
|
|
};
|