Skip to content

Commit

Permalink
usd price difference handled in swap
Browse files Browse the repository at this point in the history
  • Loading branch information
vnaysngh-mudrex committed Mar 24, 2024
1 parent 0b6fad0 commit ca86dd3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
20 changes: 14 additions & 6 deletions src/components/CurrencyInputPanel/FiatValue.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,28 @@ import { ThemedText } from 'theme/components'
import { NumberType, useFormatter } from 'utils/formatNumbers'
import { warningSeverity } from 'utils/prices'

const FiatLoadingBubble = styled(LoadingBubble)`
border-radius: 4px;
width: 4rem;
height: 1rem;
const USDPriceContainer = styled.div`
display: flex;
`

export function FiatValue({ fiatValue, priceImpact }: { fiatValue: number; priceImpact?: Percent }) {
const USDPriceDifferenceText = styled.div<{ difference: number }>`
color: ${({ difference }) => (difference > 0 ? 'green' : 'red')};
margin-left: 2px;
`

export function FiatValue({ fiatValue, usdPriceDifference }: { fiatValue: number; usdPriceDifference?: number }) {
const { formatNumber } = useFormatter()

return (
<Row gap="sm">
<ThemedText.LabelSmall color="neutral1">
{fiatValue ? (
formatNumber({ input: fiatValue, type: NumberType.FiatTokenPrice })
<USDPriceContainer>
{formatNumber({ input: fiatValue, type: NumberType.FiatTokenPrice })}{' '}
{usdPriceDifference && (
<USDPriceDifferenceText difference={usdPriceDifference}>({usdPriceDifference}%)</USDPriceDifferenceText>
)}
</USDPriceContainer>
) : (
<MouseoverTooltip text={<Trans>Not enough liquidity to show accurate USD value.</Trans>}>-</MouseoverTooltip>
)}
Expand Down
8 changes: 6 additions & 2 deletions src/components/CurrencyInputPanel/SwapCurrencyInputPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ interface SwapCurrencyInputPanelProps {
pair?: Pair | null
hideInput?: boolean
otherCurrency?: Currency | null
usdPriceDifference?: number | undefined
fiatValue?: number
priceImpact?: Percent
id: string
Expand Down Expand Up @@ -249,6 +250,7 @@ const SwapCurrencyInputPanel = forwardRef<HTMLInputElement, SwapCurrencyInputPan
onCurrencySelect,
currency,
otherCurrency,
usdPriceDifference,
id,
showCommonBases,
showCurrencyAmount,
Expand Down Expand Up @@ -289,7 +291,7 @@ const SwapCurrencyInputPanel = forwardRef<HTMLInputElement, SwapCurrencyInputPan

const chainAllowed = isSupportedChain(chainId)

const { formatted, balance } = useAccountBalance(currency as Currency)
const { formatted } = useAccountBalance(currency as Currency)

// reset tooltip state when currency changes
useEffect(() => setTooltipVisible(false), [currency])
Expand Down Expand Up @@ -398,7 +400,9 @@ const SwapCurrencyInputPanel = forwardRef<HTMLInputElement, SwapCurrencyInputPan
<span />
)}
<LoadingOpacityContainer $loading={loading}>
{fiatValue === 0 ? 'N/A' : fiatValue && <FiatValue fiatValue={fiatValue} priceImpact={priceImpact} />}
{fiatValue === 0
? 'N/A'
: fiatValue && <FiatValue fiatValue={fiatValue} usdPriceDifference={usdPriceDifference} />}
</LoadingOpacityContainer>
</RowBetween>
</FiatRow>
Expand Down
9 changes: 9 additions & 0 deletions src/pages/Swap/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,14 @@ export function Swap({
}
}, [separatedFiatValueofLiquidity])

const usdPriceDifference = useMemo(() => {
if (!token0usdPrice || !token1usdPrice) return undefined
else
return parseFloat(
(((token1usdPrice - token0usdPrice) / ((token0usdPrice + token1usdPrice) / 2)) * 100).toFixed(2)
)
}, [token0usdPrice, token1usdPrice])

const amountToApprove = useMemo(
() => (trade ? trade.maximumAmountIn(allowedSlippage) : undefined),
[trade, allowedSlippage]
Expand Down Expand Up @@ -961,6 +969,7 @@ export function Swap({
currency={currencies[Field.OUTPUT] ?? null}
onCurrencySelect={handleOutputSelect}
otherCurrency={currencies[Field.INPUT]}
usdPriceDifference={usdPriceDifference}
showCommonBases
id={InterfaceSectionName.CURRENCY_OUTPUT_PANEL}
loading={independentField === Field.INPUT && routeIsSyncing}
Expand Down

0 comments on commit ca86dd3

Please sign in to comment.