2021-11-18 18:54:58 +01:00
|
|
|
import { useCallback } from 'react';
|
|
|
|
import { useDispatch } from 'react-redux';
|
|
|
|
|
2023-02-03 12:27:32 +01:00
|
|
|
import BigNumber from 'bignumber.js';
|
|
|
|
import {
|
|
|
|
EditGasModes,
|
|
|
|
PriorityLevels,
|
|
|
|
CUSTOM_GAS_ESTIMATE,
|
|
|
|
} from '../../../shared/constants/gas';
|
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';
|
2023-01-24 15:44:49 +01:00
|
|
|
import {
|
|
|
|
decGWEIToHexWEI,
|
|
|
|
decimalToHex,
|
|
|
|
} from '../../../shared/modules/conversion.utils';
|
2021-11-18 18:54:58 +01:00
|
|
|
|
2022-12-08 19:37:06 +01:00
|
|
|
/**
|
|
|
|
* @typedef {object} TransactionFunctionsReturnType
|
|
|
|
* @property {() => void} cancelTransaction - cancel the transaction.
|
|
|
|
* @property {() => void} speedUpTransaction - speed up the transaction.
|
|
|
|
* @property {(string, number, number, number, string) => void} updateTransaction - update the transaction.
|
|
|
|
* @property {(boolean) => void} updateTransactionToTenPercentIncreasedGasFee - update the cancel / speed transaction to
|
|
|
|
* gas fee which is equal to current gas fee +10 percent.
|
|
|
|
* @property {(string) => void} updateTransactionUsingDAPPSuggestedValues - update the transaction to DAPP suggested gas value.
|
|
|
|
* @property {(string) => void} updateTransactionUsingEstimate - update the transaction using the estimate passed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param options
|
|
|
|
* @param options.defaultEstimateToUse
|
|
|
|
* @param options.editGasMode
|
|
|
|
* @param options.estimatedBaseFee
|
|
|
|
* @param options.gasFeeEstimates
|
|
|
|
* @param options.gasLimit
|
|
|
|
* @param options.maxPriorityFeePerGas
|
|
|
|
* @param options.transaction
|
|
|
|
* @param options.setRetryTxMeta
|
|
|
|
* @returns {TransactionFunctionsReturnType}
|
|
|
|
*/
|
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 (
|
2023-01-27 19:28:03 +01:00
|
|
|
(editGasMode !== EditGasModes.cancel &&
|
|
|
|
editGasMode !== EditGasModes.speedUp) ||
|
2022-01-06 03:47:26 +01:00
|
|
|
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,
|
2023-01-27 19:28:03 +01:00
|
|
|
userFeeLevel: estimateUsed || PriorityLevels.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
|
|
|
};
|
|
|
|
|
2023-01-27 19:28:03 +01:00
|
|
|
if (editGasMode === EditGasModes.swaps) {
|
2021-12-12 00:26:28 +01:00
|
|
|
dispatch(
|
2023-01-27 19:28:03 +01:00
|
|
|
updateSwapsUserFeeLevel(estimateUsed || PriorityLevels.custom),
|
2021-12-12 00:26:28 +01:00
|
|
|
);
|
|
|
|
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
|
|
|
|
2023-02-03 12:27:32 +01:00
|
|
|
const newMaxPriorityFeePerGas = new BigNumber(
|
|
|
|
maxPriorityFeePerGas,
|
|
|
|
16,
|
|
|
|
).isZero()
|
|
|
|
? decGWEIToHexWEI(
|
|
|
|
gasFeeEstimates[defaultEstimateToUse].suggestedMaxPriorityFeePerGas,
|
|
|
|
)
|
|
|
|
: maxPriorityFeePerGas;
|
|
|
|
|
|
|
|
const estimateUsed =
|
|
|
|
maxPriorityFeePerGas === '0x0'
|
|
|
|
? CUSTOM_GAS_ESTIMATE
|
|
|
|
: PriorityLevels.tenPercentIncreased;
|
|
|
|
|
2022-02-01 18:53:03 +01:00
|
|
|
updateTransaction({
|
|
|
|
estimateSuggested: initTransaction
|
|
|
|
? defaultEstimateToUse
|
2023-01-27 19:28:03 +01:00
|
|
|
: PriorityLevels.tenPercentIncreased,
|
2023-02-03 12:27:32 +01:00
|
|
|
estimateUsed,
|
2022-02-01 18:53:03 +01:00
|
|
|
gasLimit,
|
|
|
|
maxFeePerGas: addTenPercentAndRound(maxFeePerGas),
|
2023-02-03 12:27:32 +01:00
|
|
|
maxPriorityFeePerGas: addTenPercentAndRound(newMaxPriorityFeePerGas),
|
2022-02-01 18:53:03 +01:00
|
|
|
});
|
|
|
|
},
|
2023-02-03 12:27:32 +01:00
|
|
|
[defaultEstimateToUse, gasFeeEstimates, transaction, updateTransaction],
|
2022-02-01 18:53:03 +01:00
|
|
|
);
|
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({
|
2023-01-27 19:28:03 +01:00
|
|
|
estimateUsed: PriorityLevels.dAppSuggested,
|
2022-01-06 03:47:26 +01:00
|
|
|
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
|
|
|
};
|