2021-11-11 17:46:45 +01:00
|
|
|
import React, { useContext, useState, useEffect } from 'react';
|
2023-04-27 19:18:32 +02:00
|
|
|
import { useSelector } from 'react-redux';
|
2021-11-11 17:46:45 +01:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { captureException } from '@sentry/browser';
|
|
|
|
import TransactionDetailItem from '../transaction-detail-item/transaction-detail-item.component';
|
2022-03-29 21:21:45 +02:00
|
|
|
import UserPreferencedCurrencyDisplay from '../user-preferenced-currency-display';
|
2021-11-11 17:46:45 +01:00
|
|
|
import fetchEstimatedL1Fee from '../../../helpers/utils/optimism/fetchEstimatedL1Fee';
|
2022-03-29 21:21:45 +02:00
|
|
|
import { SECONDARY } from '../../../helpers/constants/common';
|
2021-11-11 17:46:45 +01:00
|
|
|
import { I18nContext } from '../../../contexts/i18n';
|
2023-01-23 20:30:43 +01:00
|
|
|
import { sumHexes } from '../../../../shared/modules/conversion.utils';
|
|
|
|
import { EtherDenomination } from '../../../../shared/constants/common';
|
|
|
|
import { Numeric } from '../../../../shared/modules/Numeric';
|
2023-04-27 19:18:32 +02:00
|
|
|
import { getUseCurrencyRateCheck } from '../../../selectors';
|
2021-11-11 17:46:45 +01:00
|
|
|
|
2021-11-12 01:15:43 +01:00
|
|
|
export default function MultilayerFeeMessage({
|
|
|
|
transaction,
|
|
|
|
layer2fee,
|
|
|
|
nativeCurrency,
|
2021-11-29 17:53:37 +01:00
|
|
|
plainStyle,
|
2021-11-12 01:15:43 +01:00
|
|
|
}) {
|
2021-11-11 17:46:45 +01:00
|
|
|
const t = useContext(I18nContext);
|
|
|
|
const [fetchedLayer1Total, setLayer1Total] = useState(null);
|
|
|
|
|
2023-04-27 19:18:32 +02:00
|
|
|
const useCurrencyRateCheck = useSelector(getUseCurrencyRateCheck);
|
|
|
|
|
2021-11-11 17:46:45 +01:00
|
|
|
useEffect(() => {
|
2023-04-26 02:13:38 +02:00
|
|
|
if (!transaction?.txParams) {
|
|
|
|
return;
|
|
|
|
}
|
2021-11-11 17:46:45 +01:00
|
|
|
const getEstimatedL1Fee = async () => {
|
|
|
|
try {
|
2023-04-06 13:50:52 +02:00
|
|
|
const result = await fetchEstimatedL1Fee(
|
2023-04-26 02:13:38 +02:00
|
|
|
transaction?.chainId,
|
|
|
|
transaction,
|
2023-04-06 13:50:52 +02:00
|
|
|
);
|
2021-11-11 17:46:45 +01:00
|
|
|
setLayer1Total(result);
|
|
|
|
} catch (e) {
|
|
|
|
captureException(e);
|
|
|
|
setLayer1Total(null);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
getEstimatedL1Fee();
|
|
|
|
}, [transaction]);
|
|
|
|
|
2023-04-26 02:13:38 +02:00
|
|
|
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}`;
|
|
|
|
|
2023-04-27 19:18:32 +02:00
|
|
|
feeTotalInFiat = useCurrencyRateCheck ? (
|
2023-04-26 02:13:38 +02:00
|
|
|
<UserPreferencedCurrencyDisplay
|
|
|
|
type={SECONDARY}
|
|
|
|
value={fetchedLayer1Total}
|
|
|
|
showFiat
|
|
|
|
hideLabel
|
|
|
|
/>
|
2023-04-27 19:18:32 +02:00
|
|
|
) : null;
|
2023-04-26 02:13:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const totalInWeiHex = sumHexes(
|
|
|
|
layer2fee || '0x0',
|
|
|
|
fetchedLayer1Total || '0x0',
|
|
|
|
transaction?.txParams?.value || '0x0',
|
2022-03-29 21:21:45 +02:00
|
|
|
);
|
|
|
|
|
2023-04-26 02:13:38 +02:00
|
|
|
const totalBN = new Numeric(totalInWeiHex, 16, EtherDenomination.WEI);
|
|
|
|
const totalInEth = `${totalBN
|
|
|
|
.toDenomination(EtherDenomination.ETH)
|
|
|
|
.toFixed(12)} ${nativeCurrency}`;
|
|
|
|
|
2023-04-27 19:18:32 +02:00
|
|
|
const totalInFiat = useCurrencyRateCheck ? (
|
2022-03-29 21:21:45 +02:00
|
|
|
<UserPreferencedCurrencyDisplay
|
|
|
|
type={SECONDARY}
|
|
|
|
value={totalInWeiHex}
|
|
|
|
showFiat
|
|
|
|
hideLabel
|
|
|
|
/>
|
2023-04-27 19:18:32 +02:00
|
|
|
) : null;
|
2022-03-29 21:21:45 +02:00
|
|
|
|
2021-11-11 17:46:45 +01:00
|
|
|
return (
|
2022-03-29 21:21:45 +02:00
|
|
|
<div className="multi-layer-fee-message">
|
2021-11-11 17:46:45 +01:00
|
|
|
<TransactionDetailItem
|
2023-05-02 05:42:59 +02:00
|
|
|
key="multi-layer-fee-message-total-item-gas-fee"
|
2023-04-26 02:13:38 +02:00
|
|
|
detailTitle={t('layer1Fees')}
|
2021-11-11 17:46:45 +01:00
|
|
|
detailTotal={layer1Total}
|
2023-04-27 19:18:32 +02:00
|
|
|
detailText={useCurrencyRateCheck && feeTotalInFiat}
|
2021-11-29 17:53:37 +01:00
|
|
|
noBold={plainStyle}
|
|
|
|
flexWidthValues={plainStyle}
|
2021-11-11 17:46:45 +01:00
|
|
|
/>
|
|
|
|
<TransactionDetailItem
|
2023-05-02 05:42:59 +02:00
|
|
|
key="multi-layer-fee-message-total-item-total"
|
2021-11-11 17:46:45 +01:00
|
|
|
detailTitle={t('total')}
|
|
|
|
detailTotal={totalInEth}
|
2022-03-29 21:21:45 +02:00
|
|
|
detailText={totalInFiat}
|
2021-11-11 17:46:45 +01:00
|
|
|
subTitle={t('transactionDetailMultiLayerTotalSubtitle')}
|
2021-11-29 17:53:37 +01:00
|
|
|
noBold={plainStyle}
|
|
|
|
flexWidthValues={plainStyle}
|
2021-11-11 17:46:45 +01:00
|
|
|
/>
|
2022-03-29 21:21:45 +02:00
|
|
|
</div>
|
2021-11-11 17:46:45 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
MultilayerFeeMessage.propTypes = {
|
|
|
|
transaction: PropTypes.object,
|
|
|
|
layer2fee: PropTypes.string,
|
2021-11-12 01:15:43 +01:00
|
|
|
nativeCurrency: PropTypes.string,
|
2021-11-29 17:53:37 +01:00
|
|
|
plainStyle: PropTypes.bool,
|
2021-11-11 17:46:45 +01:00
|
|
|
};
|