Commit 6e223897 authored by Zach Pomerantz's avatar Zach Pomerantz Committed by GitHub

fix: slippage and price impact ux (#3222)

parent 8064dd8e
...@@ -112,10 +112,10 @@ export default function MaxSlippageSelect() { ...@@ -112,10 +112,10 @@ export default function MaxSlippageSelect() {
const numerator = Math.floor(+custom * 100) const numerator = Math.floor(+custom * 100)
if (numerator) { if (numerator) {
const percent = new Percent(numerator, 10_000) const percent = new Percent(numerator, 10_000)
if (percent.greaterThan(MAX_VALID_SLIPPAGE)) { if (!percent.lessThan(MAX_VALID_SLIPPAGE)) {
setWarning(WarningState.INVALID_SLIPPAGE) setWarning(WarningState.INVALID_SLIPPAGE)
setMaxSlippage('auto') setMaxSlippage('auto')
} else if (percent.greaterThan(MIN_HIGH_SLIPPAGE)) { } else if (!percent.lessThan(MIN_HIGH_SLIPPAGE)) {
setWarning(WarningState.HIGH_SLIPPAGE) setWarning(WarningState.HIGH_SLIPPAGE)
setMaxSlippage(percent) setMaxSlippage(percent)
} else { } else {
......
...@@ -4,13 +4,18 @@ import { Currency, Percent, TradeType } from '@uniswap/sdk-core' ...@@ -4,13 +4,18 @@ import { Currency, Percent, TradeType } from '@uniswap/sdk-core'
import { ALLOWED_PRICE_IMPACT_HIGH, ALLOWED_PRICE_IMPACT_MEDIUM } from 'constants/misc' import { ALLOWED_PRICE_IMPACT_HIGH, ALLOWED_PRICE_IMPACT_MEDIUM } from 'constants/misc'
import { useAtom } from 'jotai' import { useAtom } from 'jotai'
import { integratorFeeAtom, MIN_HIGH_SLIPPAGE } from 'lib/state/settings' import { integratorFeeAtom, MIN_HIGH_SLIPPAGE } from 'lib/state/settings'
import { Color, ThemedText } from 'lib/theme' import styled, { Color, ThemedText } from 'lib/theme'
import { useMemo } from 'react' import { useMemo } from 'react'
import { currencyId } from 'utils/currencyId' import { currencyId } from 'utils/currencyId'
import { computeRealizedPriceImpact } from 'utils/prices' import { computeRealizedPriceImpact } from 'utils/prices'
import Row from '../../Row' import Row from '../../Row'
const Value = styled.span<{ color?: Color }>`
color: ${({ color, theme }) => color && theme[color]};
white-space: nowrap;
`
interface DetailProps { interface DetailProps {
label: string label: string
value: string value: string
...@@ -19,10 +24,10 @@ interface DetailProps { ...@@ -19,10 +24,10 @@ interface DetailProps {
function Detail({ label, value, color }: DetailProps) { function Detail({ label, value, color }: DetailProps) {
return ( return (
<ThemedText.Caption color={color}> <ThemedText.Caption>
<Row gap={2}> <Row gap={2}>
<span>{label}</span> <span>{label}</span>
<span style={{ whiteSpace: 'nowrap' }}>{value}</span> <Value color={color}>{value}</Value>
</Row> </Row>
</ThemedText.Caption> </ThemedText.Caption>
) )
...@@ -50,9 +55,9 @@ export default function Details({ trade, allowedSlippage }: DetailsProps) { ...@@ -50,9 +55,9 @@ export default function Details({ trade, allowedSlippage }: DetailsProps) {
[ [
t`Price impact`, t`Price impact`,
`${priceImpact.toFixed(2)}%`, `${priceImpact.toFixed(2)}%`,
priceImpact >= ALLOWED_PRICE_IMPACT_HIGH !priceImpact.lessThan(ALLOWED_PRICE_IMPACT_HIGH)
? 'error' ? 'error'
: priceImpact >= ALLOWED_PRICE_IMPACT_MEDIUM : !priceImpact.lessThan(ALLOWED_PRICE_IMPACT_MEDIUM)
? 'warning' ? 'warning'
: undefined, : undefined,
], ],
...@@ -65,7 +70,7 @@ export default function Details({ trade, allowedSlippage }: DetailsProps) { ...@@ -65,7 +70,7 @@ export default function Details({ trade, allowedSlippage }: DetailsProps) {
[ [
t`Slippage tolerance`, t`Slippage tolerance`,
`${allowedSlippage.toFixed(2)}%`, `${allowedSlippage.toFixed(2)}%`,
allowedSlippage.greaterThan(MIN_HIGH_SLIPPAGE) && 'warning', !allowedSlippage.lessThan(MIN_HIGH_SLIPPAGE) && 'warning',
], ],
].filter(isDetail) ].filter(isDetail)
......
...@@ -93,9 +93,9 @@ export function SummaryDialog({ trade, allowedSlippage, onConfirm }: SummaryDial ...@@ -93,9 +93,9 @@ export function SummaryDialog({ trade, allowedSlippage, onConfirm }: SummaryDial
const independentField = useAtomValue(independentFieldAtom) const independentField = useAtomValue(independentFieldAtom)
const warning = useMemo(() => { const warning = useMemo(() => {
if (priceImpact >= ALLOWED_PRICE_IMPACT_HIGH) return 'error' if (!priceImpact.lessThan(ALLOWED_PRICE_IMPACT_HIGH)) return 'error'
if (priceImpact >= ALLOWED_PRICE_IMPACT_MEDIUM) return 'warning' if (!priceImpact.lessThan(ALLOWED_PRICE_IMPACT_MEDIUM)) return 'warning'
if (allowedSlippage.greaterThan(MIN_HIGH_SLIPPAGE)) return 'warning' if (!allowedSlippage.lessThan(MIN_HIGH_SLIPPAGE)) return 'warning'
return return
}, [allowedSlippage, priceImpact]) }, [allowedSlippage, priceImpact])
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment