import React, { useContext, useState, useEffect } from 'react';
import { useSelector } from 'react-redux';
import PropTypes from 'prop-types';
import { captureException } from '@sentry/browser';
import TransactionDetailItem from '../transaction-detail-item/transaction-detail-item.component';
import UserPreferencedCurrencyDisplay from '../user-preferenced-currency-display';
import fetchEstimatedL1Fee from '../../../helpers/utils/optimism/fetchEstimatedL1Fee';
import { SECONDARY } from '../../../helpers/constants/common';
import { I18nContext } from '../../../contexts/i18n';
import { sumHexes } from '../../../../shared/modules/conversion.utils';
import { EtherDenomination } from '../../../../shared/constants/common';
import { Numeric } from '../../../../shared/modules/Numeric';
import { getUseCurrencyRateCheck } from '../../../selectors';
export default function MultilayerFeeMessage({
transaction,
layer2fee,
nativeCurrency,
plainStyle,
}) {
const t = useContext(I18nContext);
const [fetchedLayer1Total, setLayer1Total] = useState(null);
const useCurrencyRateCheck = useSelector(getUseCurrencyRateCheck);
useEffect(() => {
if (!transaction?.txParams) {
return;
}
const getEstimatedL1Fee = async () => {
try {
const result = await fetchEstimatedL1Fee(
transaction?.chainId,
transaction,
);
setLayer1Total(result);
} catch (e) {
captureException(e);
setLayer1Total(null);
}
};
getEstimatedL1Fee();
}, [transaction]);
if (!transaction?.txParams) {
return null;
}
let layer1Total = t('unknown');
let feeTotalInFiat = t('unknown');
if (fetchedLayer1Total !== null) {
const layer1TotalBN = new Numeric(
fetchedLayer1Total,
16,
EtherDenomination.WEI,
);
layer1Total = `${layer1TotalBN
.toDenomination(EtherDenomination.ETH)
.toFixed(12)} ${nativeCurrency}`;
feeTotalInFiat = useCurrencyRateCheck ? (