|
| 1 | +import BigNumber from 'bignumber.js'; |
| 2 | +import React from 'react'; |
| 3 | +import { formatCurrencyValue } from 'src/shared/units/formatCurrencyValue'; |
| 4 | +import { formatPercent } from 'src/shared/units/formatPercent/formatPercent'; |
| 5 | +import { UIText } from 'src/ui/ui-kit/UIText'; |
| 6 | +import { isNumeric } from 'src/shared/isNumeric'; |
| 7 | +import { useCurrency } from 'src/modules/currency/useCurrency'; |
| 8 | +import { exceedsPriceImpactThreshold } from 'src/ui/pages/SwapForm/shared/price-impact'; |
| 9 | +import type { Asset } from 'defi-sdk'; |
| 10 | + |
| 11 | +export function FiatInputValue({ |
| 12 | + name, |
| 13 | + primaryInput, |
| 14 | + spendInput, |
| 15 | + spendAsset, |
| 16 | + receiveInput, |
| 17 | + receiveAsset, |
| 18 | +}: { |
| 19 | + name: 'spendInput' | 'receiveInput'; |
| 20 | + primaryInput: 'spend' | 'receive'; |
| 21 | + spendInput?: string; |
| 22 | + spendAsset: Asset | null; |
| 23 | + receiveInput?: string; |
| 24 | + receiveAsset: Asset | null; |
| 25 | +}) { |
| 26 | + const { currency } = useCurrency(); |
| 27 | + |
| 28 | + const asset = name === 'receiveInput' ? receiveAsset : spendAsset; |
| 29 | + const oppositeAsset = asset === receiveAsset ? spendAsset : receiveAsset; |
| 30 | + const inputValue = name === 'receiveInput' ? receiveInput : spendInput; |
| 31 | + const oppositeInputValue = |
| 32 | + name === 'receiveInput' ? spendInput : receiveInput; |
| 33 | + const isPrimaryInput = |
| 34 | + name === 'receiveInput' |
| 35 | + ? primaryInput === 'receive' |
| 36 | + : primaryInput === 'spend'; |
| 37 | + |
| 38 | + if (inputValue == null || !isNumeric(inputValue)) { |
| 39 | + return null; |
| 40 | + } |
| 41 | + |
| 42 | + const fiatValue = new BigNumber(inputValue || 0).times( |
| 43 | + asset?.price?.value || 0 |
| 44 | + ); |
| 45 | + |
| 46 | + let diff: BigNumber | null = null; |
| 47 | + if (name === 'receiveInput') { |
| 48 | + const oppositeFiatValue = new BigNumber(oppositeInputValue || 0).times( |
| 49 | + oppositeAsset?.price?.value || 0 |
| 50 | + ); |
| 51 | + diff = oppositeFiatValue.isGreaterThan(0) |
| 52 | + ? fiatValue.minus(oppositeFiatValue).div(oppositeFiatValue) |
| 53 | + : null; |
| 54 | + } |
| 55 | + |
| 56 | + const formattedDiff = diff |
| 57 | + ? `${diff.isLessThan(0) ? '' : '+'}${formatPercent(diff.times(100), 'en')}%` |
| 58 | + : null; |
| 59 | + |
| 60 | + const isPriceImpactWarning = diff |
| 61 | + ? exceedsPriceImpactThreshold({ relativeChange: diff }) |
| 62 | + : false; |
| 63 | + |
| 64 | + return ( |
| 65 | + <UIText |
| 66 | + kind="small/regular" |
| 67 | + color={ |
| 68 | + isPriceImpactWarning ? 'var(--negative-500)' : 'var(--neutral-600)' |
| 69 | + } |
| 70 | + style={isPriceImpactWarning ? { cursor: 'help' } : undefined} |
| 71 | + title={ |
| 72 | + isPriceImpactWarning |
| 73 | + ? 'The exchange rate is lower than the market rate. Lack of liquidity affects the exchange rate. Try a lower amount.' |
| 74 | + : undefined |
| 75 | + } |
| 76 | + > |
| 77 | + {isPrimaryInput ? null : '≈'} |
| 78 | + {formatCurrencyValue(fiatValue, 'en', currency)}{' '} |
| 79 | + {formattedDiff ? `(${formattedDiff})` : null} |
| 80 | + </UIText> |
| 81 | + ); |
| 82 | +} |
0 commit comments