1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-01 21:57:06 +01:00
metamask-extension/ui/pages/swaps/exchange-rate-display/exchange-rate-display.js

141 lines
4.1 KiB
JavaScript
Raw Normal View History

2023-06-15 20:17:21 +02:00
import React, { useState, useContext } from 'react';
import PropTypes from 'prop-types';
import BigNumber from 'bignumber.js';
import classnames from 'classnames';
import { formatSwapsValueForDisplay } from '../swaps.util';
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,
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
}) {
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,
);
2020-11-03 00:41:28 +01:00
const secondaryTokenAmount = calcTokenAmount(
secondaryTokenValue,
secondaryTokenDecimals,
);
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)
.toString(10);
2020-11-03 00:41:28 +01:00
const conversionRateFromSecondaryToPrimary = new BigNumber(primaryTokenAmount)
.div(secondaryTokenAmount)
.round(9)
.toString(10);
2020-08-18 15:53:54 +02:00
2020-11-03 00:41:28 +01:00
const baseSymbol = showPrimaryToSecondary
? primaryTokenSymbol
: secondaryTokenSymbol;
2020-11-03 00:41:28 +01:00
const ratiodSymbol = showPrimaryToSecondary
? secondaryTokenSymbol
: primaryTokenSymbol;
2020-10-06 20:28:38 +02:00
2020-11-03 00:41:28 +01:00
const rate = showPrimaryToSecondary
? conversionRateFromPrimaryToSecondary
: 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)) {
rateToDisplay = '0.000000001';
comparisonSymbol = '<';
2020-11-03 00:41:28 +01:00
} else if (new BigNumber(rate, 10).lt('0.000001', 10)) {
rateToDisplay = rate;
2020-10-06 20:28:38 +02:00
} else {
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"
>
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>
);
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,
boldSymbols: PropTypes.bool,
2023-06-15 20:17:21 +02:00
showIconForSwappingTokens: PropTypes.bool,
onQuotesClick: PropTypes.func,
};