2021-11-11 17:46:45 +01:00
|
|
|
import React, { useContext, useState, useEffect } from 'react';
|
|
|
|
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';
|
|
|
|
import { sumHexes } from '../../../helpers/utils/transactions.util';
|
|
|
|
import {
|
|
|
|
toBigNumber,
|
|
|
|
toNormalizedDenomination,
|
|
|
|
} from '../../../../shared/modules/conversion.utils';
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
let layer1Total = 'unknown';
|
2022-03-29 21:21:45 +02:00
|
|
|
let layer1TotalBN;
|
2021-11-11 17:46:45 +01:00
|
|
|
|
|
|
|
if (fetchedLayer1Total !== null) {
|
2022-03-29 21:21:45 +02:00
|
|
|
layer1TotalBN = toBigNumber.hex(fetchedLayer1Total);
|
2021-11-11 17:46:45 +01:00
|
|
|
layer1Total = `${toNormalizedDenomination
|
|
|
|
.WEI(layer1TotalBN)
|
2022-03-29 21:21:45 +02:00
|
|
|
.toFixed(12)} ${nativeCurrency}`;
|
2021-11-11 17:46:45 +01:00
|
|
|
}
|
|
|
|
|
2022-03-29 21:21:45 +02:00
|
|
|
const feeTotal = sumHexes(layer2fee || '0x0', fetchedLayer1Total || '0x0');
|
|
|
|
|
2021-11-11 17:46:45 +01:00
|
|
|
const totalInWeiHex = sumHexes(
|
2022-03-29 21:21:45 +02:00
|
|
|
feeTotal || '0x0',
|
2021-11-11 17:46:45 +01:00
|
|
|
transaction.txParams.value || '0x0',
|
|
|
|
);
|
2022-03-29 21:21:45 +02:00
|
|
|
|
2021-11-11 17:46:45 +01:00
|
|
|
const totalBN = toBigNumber.hex(totalInWeiHex);
|
|
|
|
const totalInEth = `${toNormalizedDenomination
|
|
|
|
.WEI(totalBN)
|
2022-03-29 21:21:45 +02:00
|
|
|
.toFixed(12)} ${nativeCurrency}`;
|
2021-11-11 17:46:45 +01:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const getEstimatedL1Fee = async () => {
|
|
|
|
try {
|
|
|
|
const result = await fetchEstimatedL1Fee(global.eth, transaction);
|
|
|
|
setLayer1Total(result);
|
|
|
|
} catch (e) {
|
|
|
|
captureException(e);
|
|
|
|
setLayer1Total(null);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
getEstimatedL1Fee();
|
|
|
|
}, [transaction]);
|
|
|
|
|
2022-03-29 21:21:45 +02:00
|
|
|
const feeTotalInFiat = (
|
|
|
|
<UserPreferencedCurrencyDisplay
|
|
|
|
type={SECONDARY}
|
|
|
|
value={feeTotal}
|
|
|
|
showFiat
|
|
|
|
hideLabel
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
const totalInFiat = (
|
|
|
|
<UserPreferencedCurrencyDisplay
|
|
|
|
type={SECONDARY}
|
|
|
|
value={totalInWeiHex}
|
|
|
|
showFiat
|
|
|
|
hideLabel
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
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
|
|
|
|
key="total-item"
|
2022-03-29 21:21:45 +02:00
|
|
|
detailTitle={t('gasFee')}
|
2021-11-11 17:46:45 +01:00
|
|
|
detailTotal={layer1Total}
|
2022-03-29 21:21:45 +02:00
|
|
|
detailText={feeTotalInFiat}
|
2021-11-29 17:53:37 +01:00
|
|
|
noBold={plainStyle}
|
|
|
|
flexWidthValues={plainStyle}
|
2021-11-11 17:46:45 +01:00
|
|
|
/>
|
|
|
|
<TransactionDetailItem
|
|
|
|
key="total-item"
|
|
|
|
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
|
|
|
};
|