import React, { useState } 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'; export default function ExchangeRateDisplay({ primaryTokenValue, primaryTokenDecimals = 18, primaryTokenSymbol, secondaryTokenValue, secondaryTokenDecimals = 18, secondaryTokenSymbol, arrowColor = 'var(--color-primary-default)', boldSymbols = true, className, }) { const [showPrimaryToSecondary, setShowPrimaryToSecondary] = useState(true); const [rotating, setRotating] = useState(false); const primaryTokenAmount = calcTokenAmount( primaryTokenValue, primaryTokenDecimals, ); const secondaryTokenAmount = calcTokenAmount( secondaryTokenValue, secondaryTokenDecimals, ); const conversionRateFromPrimaryToSecondary = new BigNumber( secondaryTokenAmount, ) .div(primaryTokenAmount) .round(9) .toString(10); const conversionRateFromSecondaryToPrimary = new BigNumber(primaryTokenAmount) .div(secondaryTokenAmount) .round(9) .toString(10); const baseSymbol = showPrimaryToSecondary ? primaryTokenSymbol : secondaryTokenSymbol; const ratiodSymbol = showPrimaryToSecondary ? secondaryTokenSymbol : primaryTokenSymbol; const rate = showPrimaryToSecondary ? conversionRateFromPrimaryToSecondary : conversionRateFromSecondaryToPrimary; let rateToDisplay; let comparisonSymbol = '='; if (new BigNumber(rate, 10).lt('0.00000001', 10)) { rateToDisplay = '0.000000001'; comparisonSymbol = '<'; } else if (new BigNumber(rate, 10).lt('0.000001', 10)) { rateToDisplay = rate; } else { rateToDisplay = formatSwapsValueForDisplay(rate); } return (
1 {baseSymbol} {comparisonSymbol} {rateToDisplay} {ratiodSymbol}
{ setShowPrimaryToSecondary(!showPrimaryToSecondary); setRotating(true); }} onAnimationEnd={() => setRotating(false)} >
); } ExchangeRateDisplay.propTypes = { primaryTokenValue: PropTypes.oneOfType([ PropTypes.string, PropTypes.instanceOf(BigNumber), ]).isRequired, primaryTokenDecimals: PropTypes.oneOfType([ PropTypes.string, PropTypes.number, ]), primaryTokenSymbol: PropTypes.string.isRequired, secondaryTokenValue: PropTypes.oneOfType([ PropTypes.string, PropTypes.instanceOf(BigNumber), ]).isRequired, secondaryTokenDecimals: PropTypes.oneOfType([ PropTypes.string, PropTypes.number, ]), secondaryTokenSymbol: PropTypes.string.isRequired, className: PropTypes.string, arrowColor: PropTypes.string, boldSymbols: PropTypes.bool, };