Skip to content

Commit

Permalink
fix: slippage and price impact ux (#3222)
Browse files Browse the repository at this point in the history
  • Loading branch information
zzmp authored Feb 3, 2022
1 parent 8064dd8 commit 6e22389
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/lib/components/Swap/Settings/MaxSlippageSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ export default function MaxSlippageSelect() {
const numerator = Math.floor(+custom * 100)
if (numerator) {
const percent = new Percent(numerator, 10_000)
if (percent.greaterThan(MAX_VALID_SLIPPAGE)) {
if (!percent.lessThan(MAX_VALID_SLIPPAGE)) {
setWarning(WarningState.INVALID_SLIPPAGE)
setMaxSlippage('auto')
} else if (percent.greaterThan(MIN_HIGH_SLIPPAGE)) {
} else if (!percent.lessThan(MIN_HIGH_SLIPPAGE)) {
setWarning(WarningState.HIGH_SLIPPAGE)
setMaxSlippage(percent)
} else {
Expand Down
17 changes: 11 additions & 6 deletions src/lib/components/Swap/Summary/Details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 { useAtom } from 'jotai'
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 { currencyId } from 'utils/currencyId'
import { computeRealizedPriceImpact } from 'utils/prices'

import Row from '../../Row'

const Value = styled.span<{ color?: Color }>`
color: ${({ color, theme }) => color && theme[color]};
white-space: nowrap;
`

interface DetailProps {
label: string
value: string
Expand All @@ -19,10 +24,10 @@ interface DetailProps {

function Detail({ label, value, color }: DetailProps) {
return (
<ThemedText.Caption color={color}>
<ThemedText.Caption>
<Row gap={2}>
<span>{label}</span>
<span style={{ whiteSpace: 'nowrap' }}>{value}</span>
<Value color={color}>{value}</Value>
</Row>
</ThemedText.Caption>
)
Expand Down Expand Up @@ -50,9 +55,9 @@ export default function Details({ trade, allowedSlippage }: DetailsProps) {
[
t`Price impact`,
`${priceImpact.toFixed(2)}%`,
priceImpact >= ALLOWED_PRICE_IMPACT_HIGH
!priceImpact.lessThan(ALLOWED_PRICE_IMPACT_HIGH)
? 'error'
: priceImpact >= ALLOWED_PRICE_IMPACT_MEDIUM
: !priceImpact.lessThan(ALLOWED_PRICE_IMPACT_MEDIUM)
? 'warning'
: undefined,
],
Expand All @@ -65,7 +70,7 @@ export default function Details({ trade, allowedSlippage }: DetailsProps) {
[
t`Slippage tolerance`,
`${allowedSlippage.toFixed(2)}%`,
allowedSlippage.greaterThan(MIN_HIGH_SLIPPAGE) && 'warning',
!allowedSlippage.lessThan(MIN_HIGH_SLIPPAGE) && 'warning',
],
].filter(isDetail)

Expand Down
6 changes: 3 additions & 3 deletions src/lib/components/Swap/Summary/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ export function SummaryDialog({ trade, allowedSlippage, onConfirm }: SummaryDial
const independentField = useAtomValue(independentFieldAtom)

const warning = useMemo(() => {
if (priceImpact >= ALLOWED_PRICE_IMPACT_HIGH) return 'error'
if (priceImpact >= ALLOWED_PRICE_IMPACT_MEDIUM) return 'warning'
if (allowedSlippage.greaterThan(MIN_HIGH_SLIPPAGE)) return 'warning'
if (!priceImpact.lessThan(ALLOWED_PRICE_IMPACT_HIGH)) return 'error'
if (!priceImpact.lessThan(ALLOWED_PRICE_IMPACT_MEDIUM)) return 'warning'
if (!allowedSlippage.lessThan(MIN_HIGH_SLIPPAGE)) return 'warning'
return
}, [allowedSlippage, priceImpact])

Expand Down

0 comments on commit 6e22389

Please sign in to comment.