Skip to content

Commit

Permalink
fix: price % format fix (#244)
Browse files Browse the repository at this point in the history
* standardize percentage formatting in widget

* style(lint): lint action with ESLint

* fix

* delete

Co-authored-by: Lint Action <lint-action@uniswap.org>
  • Loading branch information
lynnshaoyu and Lint Action authored Oct 7, 2022
1 parent 897f0e4 commit 2a74b9d
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 33 deletions.
5 changes: 1 addition & 4 deletions src/components/Swap/Output.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { Field } from 'state/swap'
import styled from 'styled-components/macro'
import { DynamicThemeProvider, ThemedText } from 'theme'
import { formatCurrencyAmount } from 'utils/formatCurrencyAmount'
import { formatPercentage } from 'utils/formatPercentage'

import Column from '../Column'
import Row from '../Row'
Expand Down Expand Up @@ -93,9 +92,7 @@ export default function Output({ disabled, focused, children }: PropsWithChildre
<Row>
<USDC gap={0.5} isLoading={isRouteLoading}>
{outputUSDC && `$${formatCurrencyAmount(outputUSDC, 6, 'en', 2)} `}
{impact && (
<ThemedText.Body2 color={impact.warning}>({formatPercentage(impact.percent)})</ThemedText.Body2>
)}
{impact && <ThemedText.Body2 color={impact.warning}>({impact.toString()})</ThemedText.Body2>}
</USDC>
{balance && (
<Balance color={focused ? 'secondary' : 'hint'}>
Expand Down
3 changes: 1 addition & 2 deletions src/components/Swap/Summary/Details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import styled from 'styled-components/macro'
import { Color, ThemedText } from 'theme'
import { currencyId } from 'utils/currencyId'
import { formatCurrencyAmount } from 'utils/formatCurrencyAmount'
import { formatPercentage } from 'utils/formatPercentage'
import { computeRealizedLPFeeAmount } from 'utils/prices'
import { isExactInput } from 'utils/tradeType'

Expand Down Expand Up @@ -66,7 +65,7 @@ export default function Details({ trade, slippage, impact }: DetailsProps) {
}

if (impact) {
rows.push([t`Price impact`, formatPercentage(impact.percent), impact.warning])
rows.push([t`Price impact`, impact.toString(), impact.warning])
}

if (lpFeeAmount) {
Expand Down
3 changes: 1 addition & 2 deletions src/components/Swap/Summary/Summary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { PropsWithChildren } from 'react'
import styled from 'styled-components/macro'
import { ThemedText } from 'theme'
import { formatCurrencyAmount } from 'utils/formatCurrencyAmount'
import { formatPercentage } from 'utils/formatPercentage'

import Column from '../../Column'
import Row from '../../Row'
Expand Down Expand Up @@ -59,7 +58,7 @@ export default function Summary({ input, output, inputUSDC, outputUSDC, impact,
<TokenValue input={input} usdc={inputUSDC} open={open} />
{open ? <ArrowRight /> : <ArrowDown />}
<TokenValue input={output} usdc={outputUSDC} open={open}>
{impact && <ThemedText.Caption color={impact.warning}>({formatPercentage(impact.percent)})</ThemedText.Caption>}
{impact && <ThemedText.Caption color={impact.warning}>({impact.toString()})</ThemedText.Caption>}
</TokenValue>
</>
)
Expand Down
4 changes: 1 addition & 3 deletions src/components/Swap/Toolbar/Caption.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { ReactNode, useCallback } from 'react'
import { InterfaceTrade } from 'state/routing/types'
import styled from 'styled-components/macro'
import { ThemedText } from 'theme'
import { formatPercentage } from 'utils/formatPercentage'

import Price from '../Price'
import RoutingDiagram from '../RoutingDiagram'
Expand Down Expand Up @@ -113,8 +112,7 @@ export function Trade({
{impact?.warning && (
<>
<ThemedText.Caption>
The output amount is estimated at {formatPercentage(impact.percent)} less than the input amount due to
impact
The output amount is estimated at {impact.toString()} less than the input amount due to impact
</ThemedText.Caption>
<Rule />
</>
Expand Down
6 changes: 5 additions & 1 deletion src/hooks/usePriceImpact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export function usePriceImpact(

export function toHumanReadablePercent(priceImpact: Percent): string {
const sign = priceImpact.lessThan(0) ? '+' : ''
const number = parseFloat(priceImpact.multiply(-1)?.toSignificant(3))
const exactFloat = (Number(priceImpact.numerator) / Number(priceImpact.denominator)) * 100
if (exactFloat < 0.005) {
return '0.00%'
}
const number = parseFloat(priceImpact.multiply(-1)?.toFixed(2))
return `${sign}${number}%`
}
12 changes: 0 additions & 12 deletions src/utils/formatPercentage.test.ts

This file was deleted.

9 changes: 0 additions & 9 deletions src/utils/formatPercentage.ts

This file was deleted.

1 comment on commit 2a74b9d

@vercel
Copy link

@vercel vercel bot commented on 2a74b9d Oct 7, 2022

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-git-main-uniswap.vercel.app
widgets-uniswap.vercel.app
widgets-seven-tau.vercel.app

Please sign in to comment.