2023-06-15 20:17:21 +02:00
|
|
|
import React, { useState, useContext } from 'react';
|
2021-02-04 19:15:23 +01:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import BigNumber from 'bignumber.js';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
import { formatSwapsValueForDisplay } from '../swaps.util';
|
2022-09-16 21:05:21 +02:00
|
|
|
import { calcTokenAmount } from '../../../../shared/lib/transactions-controller-utils';
|
2023-06-15 20:17:21 +02:00
|
|
|
import Box from '../../../components/ui/box';
|
|
|
|
import {
|
|
|
|
JustifyContent,
|
|
|
|
DISPLAY,
|
|
|
|
AlignItems,
|
|
|
|
IconColor,
|
|
|
|
TextColor,
|
|
|
|
} from '../../../helpers/constants/design-system';
|
|
|
|
import { Icon, IconName } from '../../../components/component-library';
|
|
|
|
import { I18nContext } from '../../../contexts/i18n';
|
2020-08-18 15:53:54 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export default function ExchangeRateDisplay({
|
2020-08-18 15:53:54 +02:00
|
|
|
primaryTokenValue,
|
|
|
|
primaryTokenDecimals = 18,
|
|
|
|
primaryTokenSymbol,
|
|
|
|
secondaryTokenValue,
|
|
|
|
secondaryTokenDecimals = 18,
|
|
|
|
secondaryTokenSymbol,
|
2020-12-08 17:47:53 +01:00
|
|
|
boldSymbols = true,
|
2023-06-15 20:17:21 +02:00
|
|
|
showIconForSwappingTokens = true,
|
2020-08-18 15:53:54 +02:00
|
|
|
className,
|
2023-06-15 20:17:21 +02:00
|
|
|
onQuotesClick,
|
2020-08-18 15:53:54 +02:00
|
|
|
}) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const [showPrimaryToSecondary, setShowPrimaryToSecondary] = useState(true);
|
2023-06-15 20:17:21 +02:00
|
|
|
const t = useContext(I18nContext);
|
2020-08-18 15:53:54 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const primaryTokenAmount = calcTokenAmount(
|
|
|
|
primaryTokenValue,
|
|
|
|
primaryTokenDecimals,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-11-03 00:41:28 +01:00
|
|
|
const secondaryTokenAmount = calcTokenAmount(
|
|
|
|
secondaryTokenValue,
|
|
|
|
secondaryTokenDecimals,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-08-18 15:53:54 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const conversionRateFromPrimaryToSecondary = new BigNumber(
|
|
|
|
secondaryTokenAmount,
|
|
|
|
)
|
|
|
|
.div(primaryTokenAmount)
|
|
|
|
.round(9)
|
2021-02-04 19:15:23 +01:00
|
|
|
.toString(10);
|
2020-11-03 00:41:28 +01:00
|
|
|
const conversionRateFromSecondaryToPrimary = new BigNumber(primaryTokenAmount)
|
|
|
|
.div(secondaryTokenAmount)
|
|
|
|
.round(9)
|
2021-02-04 19:15:23 +01:00
|
|
|
.toString(10);
|
2020-08-18 15:53:54 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const baseSymbol = showPrimaryToSecondary
|
|
|
|
? primaryTokenSymbol
|
2021-02-04 19:15:23 +01:00
|
|
|
: secondaryTokenSymbol;
|
2020-11-03 00:41:28 +01:00
|
|
|
const ratiodSymbol = showPrimaryToSecondary
|
|
|
|
? secondaryTokenSymbol
|
2021-02-04 19:15:23 +01:00
|
|
|
: primaryTokenSymbol;
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const rate = showPrimaryToSecondary
|
|
|
|
? conversionRateFromPrimaryToSecondary
|
2021-02-04 19:15:23 +01:00
|
|
|
: conversionRateFromSecondaryToPrimary;
|
|
|
|
let rateToDisplay;
|
|
|
|
let comparisonSymbol = '=';
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
if (new BigNumber(rate, 10).lt('0.00000001', 10)) {
|
2021-02-04 19:15:23 +01:00
|
|
|
rateToDisplay = '0.000000001';
|
|
|
|
comparisonSymbol = '<';
|
2020-11-03 00:41:28 +01:00
|
|
|
} else if (new BigNumber(rate, 10).lt('0.000001', 10)) {
|
2021-02-04 19:15:23 +01:00
|
|
|
rateToDisplay = rate;
|
2020-10-06 20:28:38 +02:00
|
|
|
} else {
|
2021-02-04 19:15:23 +01:00
|
|
|
rateToDisplay = formatSwapsValueForDisplay(rate);
|
2020-10-06 20:28:38 +02:00
|
|
|
}
|
2020-08-18 15:53:54 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={classnames('exchange-rate-display', className)}>
|
2023-06-15 20:17:21 +02:00
|
|
|
<Box
|
|
|
|
display={DISPLAY.FLEX}
|
|
|
|
justifyContent={JustifyContent.center}
|
|
|
|
alignItems={AlignItems.center}
|
|
|
|
onClick={onQuotesClick}
|
|
|
|
color={TextColor.primaryDefault}
|
|
|
|
className="exchange-rate-display__quote-rate"
|
|
|
|
data-testid="exchange-rate-display-quote-rate"
|
2020-12-08 17:47:53 +01:00
|
|
|
>
|
2023-06-15 20:17:21 +02:00
|
|
|
<span>1</span>
|
|
|
|
<span
|
|
|
|
className={classnames({ 'exchange-rate-display__bold': boldSymbols })}
|
|
|
|
data-testid="exchange-rate-display-base-symbol"
|
|
|
|
>
|
|
|
|
{baseSymbol}
|
|
|
|
</span>
|
|
|
|
<span>{comparisonSymbol}</span>
|
|
|
|
<span>{rateToDisplay}</span>
|
|
|
|
<span
|
|
|
|
className={classnames({ 'exchange-rate-display__bold': boldSymbols })}
|
2020-11-03 00:41:28 +01:00
|
|
|
>
|
2023-06-15 20:17:21 +02:00
|
|
|
{ratiodSymbol}
|
|
|
|
</span>
|
|
|
|
</Box>
|
|
|
|
{showIconForSwappingTokens && (
|
|
|
|
<Icon
|
|
|
|
name={IconName.SwapHorizontal}
|
|
|
|
onClick={() => {
|
|
|
|
setShowPrimaryToSecondary(!showPrimaryToSecondary);
|
|
|
|
}}
|
|
|
|
color={IconColor.iconAlternative}
|
|
|
|
style={{ cursor: 'pointer' }}
|
|
|
|
title={t('switch')}
|
|
|
|
data-testid="exchange-rate-display-switch"
|
|
|
|
/>
|
|
|
|
)}
|
2020-08-18 15:53:54 +02:00
|
|
|
</div>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-08-18 15:53:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ExchangeRateDisplay.propTypes = {
|
2020-10-06 20:28:38 +02:00
|
|
|
primaryTokenValue: PropTypes.oneOfType([
|
|
|
|
PropTypes.string,
|
|
|
|
PropTypes.instanceOf(BigNumber),
|
|
|
|
]).isRequired,
|
2020-11-03 00:41:28 +01:00
|
|
|
primaryTokenDecimals: PropTypes.oneOfType([
|
|
|
|
PropTypes.string,
|
|
|
|
PropTypes.number,
|
|
|
|
]),
|
2020-08-18 15:53:54 +02:00
|
|
|
primaryTokenSymbol: PropTypes.string.isRequired,
|
2020-10-06 20:28:38 +02:00
|
|
|
secondaryTokenValue: PropTypes.oneOfType([
|
|
|
|
PropTypes.string,
|
|
|
|
PropTypes.instanceOf(BigNumber),
|
|
|
|
]).isRequired,
|
2020-11-03 00:41:28 +01:00
|
|
|
secondaryTokenDecimals: PropTypes.oneOfType([
|
|
|
|
PropTypes.string,
|
|
|
|
PropTypes.number,
|
|
|
|
]),
|
2020-08-18 15:53:54 +02:00
|
|
|
secondaryTokenSymbol: PropTypes.string.isRequired,
|
|
|
|
className: PropTypes.string,
|
2020-12-08 17:47:53 +01:00
|
|
|
boldSymbols: PropTypes.bool,
|
2023-06-15 20:17:21 +02:00
|
|
|
showIconForSwappingTokens: PropTypes.bool,
|
|
|
|
onQuotesClick: PropTypes.func,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|