2021-10-22 19:14:28 +02:00
|
|
|
import { useState } from 'react';
|
|
|
|
import { isEqual } from 'lodash';
|
|
|
|
|
2021-11-06 01:59:23 +01:00
|
|
|
import {
|
|
|
|
GAS_ESTIMATE_TYPES,
|
|
|
|
CUSTOM_GAS_ESTIMATE,
|
|
|
|
} from '../../../shared/constants/gas';
|
2021-10-22 19:14:28 +02:00
|
|
|
import { isLegacyTransaction } from '../../helpers/utils/transactions.util';
|
|
|
|
|
2022-09-16 21:05:21 +02:00
|
|
|
import { hexWEIToDecGWEI } from '../../../shared/lib/transactions-controller-utils';
|
2021-10-22 19:14:28 +02:00
|
|
|
import { feeParamsAreCustom } from './utils';
|
|
|
|
|
|
|
|
function getGasPriceEstimate(gasFeeEstimates, gasEstimateType, estimateToUse) {
|
|
|
|
if (gasEstimateType === GAS_ESTIMATE_TYPES.LEGACY) {
|
|
|
|
return gasFeeEstimates?.[estimateToUse] ?? '0';
|
|
|
|
} else if (gasEstimateType === GAS_ESTIMATE_TYPES.ETH_GASPRICE) {
|
|
|
|
return gasFeeEstimates?.gasPrice ?? '0';
|
|
|
|
}
|
|
|
|
return '0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-07-27 15:28:05 +02:00
|
|
|
* @typedef {object} GasPriceInputsReturnType
|
2021-10-22 19:14:28 +02:00
|
|
|
* @property {DecGweiString} [gasPrice] - the gasPrice input value.
|
|
|
|
* @property {(DecGweiString) => void} setGasPrice - state setter method to update the gasPrice.
|
|
|
|
* @property {(boolean) => true} setGasPriceHasBeenManuallySet - state setter method to update gasPriceHasBeenManuallySet
|
|
|
|
* field gasPriceHasBeenManuallySet is used in gasPrice calculations.
|
|
|
|
*/
|
2022-01-07 16:57:33 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param options
|
|
|
|
* @param options.estimateToUse
|
|
|
|
* @param options.gasEstimateType
|
|
|
|
* @param options.gasFeeEstimates
|
|
|
|
* @param options.transaction
|
|
|
|
* @returns {GasPriceInputsReturnType}
|
|
|
|
*/
|
2021-10-22 19:14:28 +02:00
|
|
|
export function useGasPriceInput({
|
|
|
|
estimateToUse,
|
|
|
|
gasEstimateType,
|
|
|
|
gasFeeEstimates,
|
|
|
|
transaction,
|
|
|
|
}) {
|
|
|
|
const [gasPriceHasBeenManuallySet, setGasPriceHasBeenManuallySet] = useState(
|
2021-11-06 01:59:23 +01:00
|
|
|
transaction?.userFeeLevel === CUSTOM_GAS_ESTIMATE,
|
2021-10-22 19:14:28 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
const [gasPrice, setGasPrice] = useState(() => {
|
|
|
|
const { gasPrice: txGasPrice } = transaction?.txParams || {};
|
|
|
|
return txGasPrice && feeParamsAreCustom(transaction)
|
|
|
|
? Number(hexWEIToDecGWEI(txGasPrice))
|
|
|
|
: null;
|
|
|
|
});
|
|
|
|
|
|
|
|
const [initialGasPriceEstimates] = useState(gasFeeEstimates);
|
|
|
|
const gasPriceEstimatesHaveNotChanged = isEqual(
|
|
|
|
initialGasPriceEstimates,
|
|
|
|
gasFeeEstimates,
|
|
|
|
);
|
|
|
|
|
|
|
|
const gasPriceToUse =
|
|
|
|
gasPrice !== null &&
|
|
|
|
(gasPriceHasBeenManuallySet ||
|
|
|
|
gasPriceEstimatesHaveNotChanged ||
|
|
|
|
isLegacyTransaction(transaction?.txParams))
|
|
|
|
? gasPrice
|
|
|
|
: getGasPriceEstimate(gasFeeEstimates, gasEstimateType, estimateToUse);
|
|
|
|
|
|
|
|
return {
|
|
|
|
gasPrice: gasPriceToUse,
|
|
|
|
setGasPrice,
|
|
|
|
setGasPriceHasBeenManuallySet,
|
|
|
|
};
|
|
|
|
}
|