Skip to content

Commit

Permalink
revert: "fix: remove USD value impact" (#525)
Browse files Browse the repository at this point in the history
* Revert "fix: remove USD value impact (#457)"

This reverts commit d51888a.

* fix: use USD value change in output field

* feat: improvements to slippage

* fix: add testg

* fix: string consistency

* fix: use correct formatting

* fix: rename prop
  • Loading branch information
just-toby authored Mar 8, 2023
1 parent dd65824 commit b8998ef
Show file tree
Hide file tree
Showing 14 changed files with 125 additions and 119 deletions.
6 changes: 3 additions & 3 deletions src/components/Swap/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ interface FieldWrapperProps {
field: Field
maxAmount?: string
approved?: boolean
impact?: PriceImpact
fiatValueChange?: PriceImpact
subheader: string
}

export function FieldWrapper({
field,
maxAmount,
approved,
impact,
fiatValueChange,
className,
subheader,
}: FieldWrapperProps & { className?: string }) {
Expand Down Expand Up @@ -164,7 +164,7 @@ export function FieldWrapper({
<Row>
<USDC isLoading={isRouteLoading}>
{usdc && `${formatCurrencyAmount(usdc, NumberType.FiatTokenQuantity)}`}
<PriceImpactRow impact={impact} />
<PriceImpactRow impact={fiatValueChange} />
</USDC>
{balance && (
<Row gap={0.5}>
Expand Down
5 changes: 2 additions & 3 deletions src/components/Swap/Output.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ const OutputWrapper = styled(FieldWrapper)<{ hasColor?: boolean | null; isWide:
`

export default function Output() {
const { impact } = useSwapInfo()

const { fiatValueChange } = useSwapInfo()
const [currency] = useSwapCurrency(Field.OUTPUT)
const overrideColor = useAtomValue(colorAtom)
const dynamicColor = useCurrencyColor(currency)
Expand All @@ -43,7 +42,7 @@ export default function Output() {
<OutputWrapper
isWide={isWideWidget}
field={Field.OUTPUT}
impact={impact}
fiatValueChange={fiatValueChange}
hasColor={hasColor}
subheader={t`You receive`}
/>
Expand Down
5 changes: 4 additions & 1 deletion src/components/Swap/Settings/MaxSlippageSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Trans } from '@lingui/macro'
import Expando, { IconPrefix } from 'components/Expando'
import Popover from 'components/Popover'
import { useTooltip } from 'components/Tooltip'
import { useSwapInfo } from 'hooks/swap'
import { getSlippageWarning, toPercent } from 'hooks/useSlippage'
import { Expando as ExpandoIcon } from 'icons'
import { AlertTriangle, Check, Icon, LargeIcon, XOctagon } from 'icons'
Expand Down Expand Up @@ -144,6 +145,8 @@ export default function MaxSlippageSelect() {
[setSlippage]
)

const { slippage: allowedSlippage } = useSwapInfo()

const [open, setOpen] = useState(false)
return (
<Column gap={0.75}>
Expand Down Expand Up @@ -190,7 +193,7 @@ export default function MaxSlippageSelect() {
size={Math.max(maxSlippageInput.length, 4)}
value={maxSlippageInput}
onChange={(input) => processInput(input)}
placeholder={'0.10'}
placeholder={allowedSlippage.allowed.toFixed(2)}
ref={input}
data-testid="input-slippage"
maxLength={10}
Expand Down
2 changes: 1 addition & 1 deletion src/components/Swap/Summary/Details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export default function Details({ trade, slippage, gasUseEstimateUSD, inputUSDC,
details.push([t`Price impact`, impact?.percent ? impact?.toString() : '-', impact.warning])
}

const { estimateMessage, descriptor, value } = getEstimateMessage(trade, slippage, impact)
const { estimateMessage, descriptor, value } = getEstimateMessage(trade, slippage)
details.push([descriptor, value])

return { details, estimateMessage }
Expand Down
28 changes: 15 additions & 13 deletions src/components/Swap/Summary/Estimate.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { t, Trans } from '@lingui/macro'
import { formatCurrencyAmount, NumberType } from '@uniswap/conedison/format'
import { PriceImpact } from 'hooks/usePriceImpact'
import { Slippage } from 'hooks/useSlippage'
import { formatSlippage, Slippage } from 'hooks/useSlippage'
import { ReactNode, useMemo } from 'react'
import { InterfaceTrade } from 'state/routing/types'
import styled from 'styled-components/macro'
Expand All @@ -20,22 +19,25 @@ interface EstimateProps {
}

export default function SwapInputOutputEstimate({ trade, slippage }: EstimateProps) {
const { estimateMessage } = useMemo(
() => getEstimateMessage(trade, slippage, undefined /* priceImpact */),
[slippage, trade]
)
const { estimateMessage } = useMemo(() => getEstimateMessage(trade, slippage), [slippage, trade])
return <StyledEstimate color="secondary">{estimateMessage}</StyledEstimate>
}

export function getEstimateMessage(
trade: InterfaceTrade,
slippage: Slippage,
priceImpact: PriceImpact | undefined
trade: InterfaceTrade | undefined,
slippage: Slippage
): {
estimateMessage: string
descriptor: ReactNode
value: string
} {
if (!trade) {
return {
estimateMessage: '',
descriptor: '',
value: '-',
}
}
const { inputAmount, outputAmount } = trade
const inputCurrency = inputAmount.currency
const outputCurrency = outputAmount.currency
Expand All @@ -49,10 +51,10 @@ export function getEstimateMessage(
descriptor: (
<ThemedText.Body2>
<Trans>Minimum output after slippage</Trans>
{priceImpact && (
{slippage && (
<ThemedText.Body2 $inline color={slippage?.warning ?? 'secondary'}>
{' '}
({priceImpact?.toString()})
({formatSlippage(slippage)})
</ThemedText.Body2>
)}
</ThemedText.Body2>
Expand All @@ -68,10 +70,10 @@ export function getEstimateMessage(
descriptor: (
<ThemedText.Body2>
<Trans>Maximum input after slippage</Trans>
{priceImpact && (
{slippage && (
<ThemedText.Body2 $inline color={slippage?.warning ?? 'secondary'}>
{' '}
({priceImpact?.toString()})
({formatSlippage(slippage)})
</ThemedText.Body2>
)}
</ThemedText.Body2>
Expand Down
4 changes: 1 addition & 3 deletions src/components/Swap/TokenInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ const TokenInputRow = styled(Row)`

const ValueInput = styled(DecimalInput)`
color: ${({ theme }) => theme.primary};
height: 1.5rem;
margin: -0.25rem 0;
${loadingTransitionCss};
${loadingTransitionCss}
`

const TokenInputColumn = styled(Column)`
Expand Down
17 changes: 5 additions & 12 deletions src/components/Swap/Toolbar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import { memo, ReactNode, useCallback, useContext, useMemo } from 'react'
import { TradeState } from 'state/routing/types'
import { Field } from 'state/swap'
import styled from 'styled-components/macro'
import { ThemedText } from 'theme'

import Row from '../../Row'
import SwapInputOutputEstimate from '../Summary/Estimate'
import SwapInputOutputEstimate, { getEstimateMessage } from '../Summary/Estimate'
import SwapActionButton from '../SwapActionButton'
import * as Caption from './Caption'
import { Context as ToolbarContext, Provider as ToolbarContextProvider } from './ToolbarContext'
Expand Down Expand Up @@ -111,6 +110,7 @@ function CaptionRow() {

const tradeSummaryRows: SummaryRowProps[] = useMemo(() => {
const currencySymbol = trade?.outputAmount?.currency.symbol ?? ''
const { descriptor, value } = getEstimateMessage(trade, slippage)
const rows: SummaryRowProps[] = [
{
name: t`Network fee`,
Expand All @@ -128,16 +128,9 @@ function CaptionRow() {
: undefined,
},
{
name: (
<ThemedText.Body2 marginRight="0.25rem">
<Trans>Minimum output after slippage </Trans>
<ThemedText.Body2 $inline color={impact?.warning ?? 'secondary'}>
{' '}
({impact?.toString()})
</ThemedText.Body2>
</ThemedText.Body2>
),
value: trade ? `${formatCurrencyAmount(trade?.minimumAmountOut(slippage.allowed))} ${currencySymbol}` : '-',
// min/max output/input after slippage
name: <div style={{ marginRight: '0.25em' }}>{descriptor}</div>,
value,
},
{
name: t`Expected output`,
Expand Down
20 changes: 10 additions & 10 deletions src/components/Swap/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,21 @@ export default function Swap(props: SwapProps) {

return (
<>
<Header title={<Trans>Swap</Trans>}>
<Wallet disabled={props.hideConnectionUI} />
<Settings />
</Header>
<div ref={setWrapper}>
<PopoverBoundaryProvider value={wrapper}>
<SwapInfoProvider>
<SwapInfoProvider>
<Header title={<Trans>Swap</Trans>}>
<Wallet disabled={props.hideConnectionUI} />
<Settings />
</Header>
<div ref={setWrapper}>
<PopoverBoundaryProvider value={wrapper}>
<Input />
<ReverseButton />
<Output />
<Toolbar />
{useBrandedFooter() && <BrandedFooter />}
</SwapInfoProvider>
</PopoverBoundaryProvider>
</div>
</PopoverBoundaryProvider>
</div>
</SwapInfoProvider>
{displayTx && (
<Dialog color="dialog">
<StatusDialog tx={displayTx} onClose={() => setDisplayTxHash()} />
Expand Down
6 changes: 5 additions & 1 deletion src/hooks/swap/useSwapInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useRouterTrade } from 'hooks/routing/useRouterTrade'
import { useCurrencyBalances } from 'hooks/useCurrencyBalance'
import useOnSupportedNetwork from 'hooks/useOnSupportedNetwork'
import usePermit2Allowance, { Allowance, AllowanceState } from 'hooks/usePermit2Allowance'
import { PriceImpact, usePriceImpact } from 'hooks/usePriceImpact'
import { PriceImpact, useFiatValueChange, usePriceImpact } from 'hooks/usePriceImpact'
import useSlippage, { DEFAULT_SLIPPAGE, Slippage } from 'hooks/useSlippage'
import { usePermit2 as usePermit2Enabled } from 'hooks/useSyncFlags'
import useUSDCPrice, { useUSDCValue } from 'hooks/useUSDCPrice'
Expand Down Expand Up @@ -50,6 +50,7 @@ interface SwapInfo {
allowance: Allowance
slippage: Slippage
impact?: PriceImpact
fiatValueChange?: PriceImpact
}

/** Returns the best computed swap (trade/wrap). */
Expand Down Expand Up @@ -105,6 +106,7 @@ function useComputeSwapInfo(): SwapInfo {
// Wait until the trade is valid to avoid displaying incorrect intermediate values.
const slippage = useSlippage(trade)
const impact = usePriceImpact(trade.trade)
const fiatValueChange = useFiatValueChange(trade.trade)

const permit2Enabled = usePermit2Enabled()
const maximumAmountIn = useMemo(() => {
Expand Down Expand Up @@ -137,6 +139,7 @@ function useComputeSwapInfo(): SwapInfo {
allowance,
slippage,
impact,
fiatValueChange,
}
}, [
allowance,
Expand All @@ -148,6 +151,7 @@ function useComputeSwapInfo(): SwapInfo {
currencyIn,
currencyOut,
error,
fiatValueChange,
impact,
slippage,
trade,
Expand Down
71 changes: 0 additions & 71 deletions src/hooks/useAllCurrencyCombinations.ts

This file was deleted.

18 changes: 18 additions & 0 deletions src/hooks/usePriceImpact.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { Percent } from '@uniswap/sdk-core'
import { useMemo } from 'react'
import { InterfaceTrade } from 'state/routing/types'
import { computeFiatValuePriceImpact } from 'utils/computeFiatValuePriceImpact'
import { computeRealizedPriceImpact, getPriceImpactWarning } from 'utils/prices'

import { useUSDCValue } from './useUSDCPrice'

export interface PriceImpact {
percent: Percent
warning?: 'warning' | 'error'
Expand All @@ -22,6 +25,21 @@ export function usePriceImpact(trade?: InterfaceTrade): PriceImpact | undefined
}, [trade])
}

export function useFiatValueChange(trade?: InterfaceTrade) {
const [inputUSDCValue, outputUSDCValue] = [useUSDCValue(trade?.inputAmount), useUSDCValue(trade?.outputAmount)]
return useMemo(() => {
const fiatPriceImpact = computeFiatValuePriceImpact(inputUSDCValue, outputUSDCValue)
if (!fiatPriceImpact) {
return undefined
}
return {
percent: fiatPriceImpact,
warning: getPriceImpactWarning(fiatPriceImpact),
toString: () => toHumanReadablePercent(fiatPriceImpact),
}
}, [inputUSDCValue, outputUSDCValue])
}

export function toHumanReadablePercent(priceImpact: Percent): string {
const sign = priceImpact.lessThan(0) ? '+' : ''
const exactFloat = (Number(priceImpact.numerator) / Number(priceImpact.denominator)) * 100
Expand Down
Loading

1 comment on commit b8998ef

@vercel
Copy link

@vercel vercel bot commented on b8998ef Mar 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

widgets – ./

widgets-uniswap.vercel.app
widgets-git-main-uniswap.vercel.app
widgets-seven-tau.vercel.app

Please sign in to comment.