1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-26 20:39:08 +01:00
metamask-extension/ui/hooks/gasFeeInput/useTransactionFunctions.js
Dan J Miller b7b041c43b
Fix speed-up/cancel: don't update existing transaction data (#14355)
* Fix speed-up/cancel: don't update existing transaction data

* Move retryTxMeta state management to useGasFeeInputs.js

* Handle initial retryTxMeta set if no transaction is passed to useGasFeeInputs

* Ensure previousGas is use on retry transaction if it is available in useGasFeeInputs

* Remove update transaction mock and correctly test gas fee increase scenarios now that updateTransaction used in cancel-speedup is defined on the front end
2022-04-14 13:20:48 -02:30

196 lines
5.4 KiB
JavaScript

import { useCallback } from 'react';
import { useDispatch } from 'react-redux';
import { EDIT_GAS_MODES, PRIORITY_LEVELS } from '../../../shared/constants/gas';
import {
decimalToHex,
decGWEIToHexWEI,
} from '../../helpers/utils/conversions.util';
import {
addTenPercentAndRound,
editGasModeIsSpeedUpOrCancel,
} from '../../helpers/utils/gas';
import {
createCancelTransaction,
createSpeedUpTransaction,
updateCustomSwapsEIP1559GasParams,
updatePreviousGasParams,
updateSwapsUserFeeLevel,
updateTransactionGasFees,
} from '../../store/actions';
export const useTransactionFunctions = ({
defaultEstimateToUse,
editGasMode,
estimatedBaseFee,
gasFeeEstimates,
gasLimit: gasLimitValue,
maxPriorityFeePerGas: maxPriorityFeePerGasValue,
transaction,
setRetryTxMeta,
}) => {
const dispatch = useDispatch();
const getTxMeta = useCallback(() => {
if (
(editGasMode !== EDIT_GAS_MODES.CANCEL &&
editGasMode !== EDIT_GAS_MODES.SPEED_UP) ||
transaction.previousGas
) {
return {};
}
const {
maxFeePerGas,
maxPriorityFeePerGas,
gasLimit,
} = transaction?.txParams;
return {
previousGas: {
maxFeePerGas,
maxPriorityFeePerGas,
gasLimit,
},
};
}, [editGasMode, transaction?.previousGas, transaction?.txParams]);
const updateTransaction = useCallback(
async ({
estimateUsed,
gasLimit,
maxFeePerGas,
maxPriorityFeePerGas,
estimateSuggested,
}) => {
const newGasSettings = {
gas: decimalToHex(gasLimit || gasLimitValue),
gasLimit: decimalToHex(gasLimit || gasLimitValue),
estimateSuggested: estimateSuggested || defaultEstimateToUse,
estimateUsed,
};
if (maxFeePerGas) {
newGasSettings.maxFeePerGas = maxFeePerGas;
}
if (maxPriorityFeePerGas) {
newGasSettings.maxPriorityFeePerGas =
maxPriorityFeePerGas || decGWEIToHexWEI(maxPriorityFeePerGasValue);
}
const txMeta = getTxMeta();
const updatedTxMeta = {
...transaction,
userFeeLevel: estimateUsed || PRIORITY_LEVELS.CUSTOM,
txParams: {
...transaction.txParams,
...newGasSettings,
},
...txMeta,
};
if (editGasMode === EDIT_GAS_MODES.SWAPS) {
dispatch(
updateSwapsUserFeeLevel(estimateUsed || PRIORITY_LEVELS.CUSTOM),
);
dispatch(updateCustomSwapsEIP1559GasParams(newGasSettings));
} else if (editGasModeIsSpeedUpOrCancel(editGasMode)) {
setRetryTxMeta(updatedTxMeta);
} else {
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),
);
}
},
[
defaultEstimateToUse,
dispatch,
editGasMode,
gasLimitValue,
getTxMeta,
maxPriorityFeePerGasValue,
transaction,
setRetryTxMeta,
],
);
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]);
const updateTransactionToTenPercentIncreasedGasFee = useCallback(
(initTransaction = false) => {
const { gas: gasLimit, maxFeePerGas, maxPriorityFeePerGas } =
transaction.previousGas || transaction.txParams;
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],
);
const updateTransactionUsingEstimate = useCallback(
(gasFeeEstimateToUse) => {
if (!gasFeeEstimates[gasFeeEstimateToUse]) {
return;
}
const {
suggestedMaxFeePerGas,
suggestedMaxPriorityFeePerGas,
} = gasFeeEstimates[gasFeeEstimateToUse];
updateTransaction({
estimateUsed: gasFeeEstimateToUse,
maxFeePerGas: decGWEIToHexWEI(suggestedMaxFeePerGas),
maxPriorityFeePerGas: decGWEIToHexWEI(suggestedMaxPriorityFeePerGas),
});
},
[gasFeeEstimates, updateTransaction],
);
const updateTransactionUsingDAPPSuggestedValues = useCallback(() => {
const {
maxFeePerGas,
maxPriorityFeePerGas,
} = transaction?.dappSuggestedGasFees;
updateTransaction({
estimateUsed: PRIORITY_LEVELS.DAPP_SUGGESTED,
maxFeePerGas,
maxPriorityFeePerGas,
});
}, [transaction, updateTransaction]);
return {
cancelTransaction,
speedUpTransaction,
updateTransaction,
updateTransactionToTenPercentIncreasedGasFee,
updateTransactionUsingDAPPSuggestedValues,
updateTransactionUsingEstimate,
};
};