1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-22 19:26:13 +02:00
metamask-extension/ui/hooks/gasFeeInput/useTransactionFunctions.js

50 lines
1.3 KiB
JavaScript
Raw Normal View History

import { useCallback } from 'react';
import { useDispatch } from 'react-redux';
import {
decGWEIToHexWEI,
decimalToHex,
} from '../../helpers/utils/conversions.util';
import { updateTransaction as updateTransactionFn } from '../../store/actions';
export const useTransactionFunctions = ({
defaultEstimateToUse,
gasLimit,
gasFeeEstimates,
transaction,
}) => {
const dispatch = useDispatch();
const updateTransaction = useCallback(
(estimateType) => {
const newGasSettings = {
gas: decimalToHex(gasLimit),
gasLimit: decimalToHex(gasLimit),
estimateSuggested: defaultEstimateToUse,
estimateUsed: estimateType,
};
newGasSettings.maxFeePerGas = decGWEIToHexWEI(
gasFeeEstimates[estimateType].suggestedMaxFeePerGas,
);
newGasSettings.maxPriorityFeePerGas = decGWEIToHexWEI(
gasFeeEstimates[estimateType].suggestedMaxPriorityFeePerGas,
);
const updatedTxMeta = {
...transaction,
userFeeLevel: estimateType || 'custom',
txParams: {
...transaction.txParams,
...newGasSettings,
},
};
dispatch(updateTransactionFn(updatedTxMeta));
},
[defaultEstimateToUse, dispatch, gasLimit, gasFeeEstimates, transaction],
);
return { updateTransaction };
};