Commit 0f75c6a5 authored by Jack Short's avatar Jack Short Committed by GitHub

chore: matching swap input number formatting to mobile (#6788)

* initial currency formatting

* updating price formatting on swap

* updating the test
parent 1c2ed1d9
import { Trans } from '@lingui/macro' import { Trans } from '@lingui/macro'
import { TraceEvent } from '@uniswap/analytics' import { TraceEvent } from '@uniswap/analytics'
import { BrowserEvent, InterfaceElementName, SwapEventName } from '@uniswap/analytics-events' import { BrowserEvent, InterfaceElementName, SwapEventName } from '@uniswap/analytics-events'
import { formatCurrencyAmount, NumberType } from '@uniswap/conedison/format'
import { Currency, CurrencyAmount, Percent } from '@uniswap/sdk-core' import { Currency, CurrencyAmount, Percent } from '@uniswap/sdk-core'
import { Pair } from '@uniswap/v2-sdk' import { Pair } from '@uniswap/v2-sdk'
import { useWeb3React } from '@web3-react/core' import { useWeb3React } from '@web3-react/core'
...@@ -13,7 +14,6 @@ import { ReactNode, useCallback, useState } from 'react' ...@@ -13,7 +14,6 @@ import { ReactNode, useCallback, useState } from 'react'
import { Lock } from 'react-feather' import { Lock } from 'react-feather'
import styled, { useTheme } from 'styled-components/macro' import styled, { useTheme } from 'styled-components/macro'
import { flexColumnNoWrap, flexRowNoWrap } from 'theme/styles' import { flexColumnNoWrap, flexRowNoWrap } from 'theme/styles'
import { formatCurrencyAmount } from 'utils/formatCurrencyAmount'
import { ReactComponent as DropDown } from '../../assets/images/dropdown.svg' import { ReactComponent as DropDown } from '../../assets/images/dropdown.svg'
import { useCurrencyBalance } from '../../state/connection/hooks' import { useCurrencyBalance } from '../../state/connection/hooks'
...@@ -323,7 +323,7 @@ export default function SwapCurrencyInputPanel({ ...@@ -323,7 +323,7 @@ export default function SwapCurrencyInputPanel({
renderBalance ? ( renderBalance ? (
renderBalance(selectedCurrencyBalance) renderBalance(selectedCurrencyBalance)
) : ( ) : (
<Trans>Balance: {formatCurrencyAmount(selectedCurrencyBalance, 4)}</Trans> <Trans>Balance: {formatCurrencyAmount(selectedCurrencyBalance, NumberType.TokenNonTx)}</Trans>
) )
) : null} ) : null}
</ThemedText.DeprecatedBody> </ThemedText.DeprecatedBody>
......
import { Trans } from '@lingui/macro' import { Trans } from '@lingui/macro'
import { sendAnalyticsEvent } from '@uniswap/analytics' import { sendAnalyticsEvent } from '@uniswap/analytics'
import { InterfaceElementName, SwapEventName } from '@uniswap/analytics-events' import { InterfaceElementName, SwapEventName } from '@uniswap/analytics-events'
import { formatCurrencyAmount, formatPriceImpact, formatUSDPrice, NumberType } from '@uniswap/conedison/format'
import { Percent, TradeType } from '@uniswap/sdk-core' import { Percent, TradeType } from '@uniswap/sdk-core'
import { useWeb3React } from '@web3-react/core' import { useWeb3React } from '@web3-react/core'
import { LoadingRows } from 'components/Loader/styled' import { LoadingRows } from 'components/Loader/styled'
import { SUPPORTED_GAS_ESTIMATE_CHAIN_IDS } from 'constants/chains' import { SUPPORTED_GAS_ESTIMATE_CHAIN_IDS } from 'constants/chains'
import useNativeCurrency from 'lib/hooks/useNativeCurrency' import useNativeCurrency from 'lib/hooks/useNativeCurrency'
import { InterfaceTrade } from 'state/routing/types' import { InterfaceTrade } from 'state/routing/types'
import formatPriceImpact from 'utils/formatPriceImpact'
import { Separator, ThemedText } from '../../theme' import { Separator, ThemedText } from '../../theme'
import Column from '../Column' import Column from '../Column'
...@@ -61,7 +61,7 @@ export function AdvancedSwapDetails({ trade, allowedSlippage, syncing = false }: ...@@ -61,7 +61,7 @@ export function AdvancedSwapDetails({ trade, allowedSlippage, syncing = false }:
</ThemedText.BodySmall> </ThemedText.BodySmall>
</MouseoverTooltip> </MouseoverTooltip>
<TextWithLoadingPlaceholder syncing={syncing} width={50}> <TextWithLoadingPlaceholder syncing={syncing} width={50}>
<ThemedText.BodySmall>~${trade.gasUseEstimateUSD}</ThemedText.BodySmall> <ThemedText.BodySmall>~{formatUSDPrice(trade.gasUseEstimateUSD)}</ThemedText.BodySmall>
</TextWithLoadingPlaceholder> </TextWithLoadingPlaceholder>
</RowBetween> </RowBetween>
)} )}
...@@ -93,7 +93,9 @@ export function AdvancedSwapDetails({ trade, allowedSlippage, syncing = false }: ...@@ -93,7 +93,9 @@ export function AdvancedSwapDetails({ trade, allowedSlippage, syncing = false }:
<TextWithLoadingPlaceholder syncing={syncing} width={70}> <TextWithLoadingPlaceholder syncing={syncing} width={70}>
<ThemedText.BodySmall> <ThemedText.BodySmall>
{trade.tradeType === TradeType.EXACT_INPUT {trade.tradeType === TradeType.EXACT_INPUT
? `${trade.minimumAmountOut(allowedSlippage).toSignificant(6)} ${trade.outputAmount.currency.symbol}` ? `${formatCurrencyAmount(trade.minimumAmountOut(allowedSlippage), NumberType.SwapTradeAmount)} ${
trade.outputAmount.currency.symbol
}`
: `${trade.maximumAmountIn(allowedSlippage).toSignificant(6)} ${trade.inputAmount.currency.symbol}`} : `${trade.maximumAmountIn(allowedSlippage).toSignificant(6)} ${trade.inputAmount.currency.symbol}`}
</ThemedText.BodySmall> </ThemedText.BodySmall>
</TextWithLoadingPlaceholder> </TextWithLoadingPlaceholder>
...@@ -115,7 +117,9 @@ export function AdvancedSwapDetails({ trade, allowedSlippage, syncing = false }: ...@@ -115,7 +117,9 @@ export function AdvancedSwapDetails({ trade, allowedSlippage, syncing = false }:
</RowFixed> </RowFixed>
<TextWithLoadingPlaceholder syncing={syncing} width={65}> <TextWithLoadingPlaceholder syncing={syncing} width={65}>
<ThemedText.BodySmall> <ThemedText.BodySmall>
{`${trade.outputAmount.toSignificant(6)} ${trade.outputAmount.currency.symbol}`} {`${formatCurrencyAmount(trade.outputAmount, NumberType.SwapTradeAmount)} ${
trade.outputAmount.currency.symbol
}`}
</ThemedText.BodySmall> </ThemedText.BodySmall>
</TextWithLoadingPlaceholder> </TextWithLoadingPlaceholder>
</RowBetween> </RowBetween>
......
import { Trans } from '@lingui/macro' import { Trans } from '@lingui/macro'
import { formatNumber, NumberType } from '@uniswap/conedison/format' import { formatNumber, formatPrice, NumberType } from '@uniswap/conedison/format'
import { Currency, Price } from '@uniswap/sdk-core' import { Currency, Price } from '@uniswap/sdk-core'
import { useUSDPrice } from 'hooks/useUSDPrice' import { useUSDPrice } from 'hooks/useUSDPrice'
import tryParseCurrencyAmount from 'lib/utils/tryParseCurrencyAmount' import tryParseCurrencyAmount from 'lib/utils/tryParseCurrencyAmount'
import { useCallback, useState } from 'react' import { useCallback, useState } from 'react'
import styled from 'styled-components/macro' import styled from 'styled-components/macro'
import { ThemedText } from 'theme' import { ThemedText } from 'theme'
import { formatTransactionAmount, priceToPreciseFloat } from 'utils/formatNumbers'
interface TradePriceProps { interface TradePriceProps {
price: Price<Currency, Currency> price: Price<Currency, Currency>
...@@ -34,14 +33,7 @@ export default function TradePrice({ price }: TradePriceProps) { ...@@ -34,14 +33,7 @@ export default function TradePrice({ price }: TradePriceProps) {
const { baseCurrency, quoteCurrency } = price const { baseCurrency, quoteCurrency } = price
const { data: usdPrice } = useUSDPrice(tryParseCurrencyAmount('1', showInverted ? baseCurrency : quoteCurrency)) const { data: usdPrice } = useUSDPrice(tryParseCurrencyAmount('1', showInverted ? baseCurrency : quoteCurrency))
let formattedPrice: string const formattedPrice = formatPrice(showInverted ? price : price.invert(), NumberType.TokenTx)
try {
formattedPrice = showInverted
? formatTransactionAmount(priceToPreciseFloat(price))
: formatTransactionAmount(priceToPreciseFloat(price.invert()))
} catch (error) {
formattedPrice = '0'
}
const label = showInverted ? `${price.quoteCurrency?.symbol}` : `${price.baseCurrency?.symbol} ` const label = showInverted ? `${price.quoteCurrency?.symbol}` : `${price.baseCurrency?.symbol} `
const labelInverted = showInverted ? `${price.baseCurrency?.symbol} ` : `${price.quoteCurrency?.symbol}` const labelInverted = showInverted ? `${price.baseCurrency?.symbol} ` : `${price.quoteCurrency?.symbol}`
......
...@@ -115,7 +115,7 @@ exports[`AdvancedSwapDetails.tsx matches base snapshot 1`] = ` ...@@ -115,7 +115,7 @@ exports[`AdvancedSwapDetails.tsx matches base snapshot 1`] = `
<div <div
class="c7 css-zhpkf8" class="c7 css-zhpkf8"
> >
105566.37% 105566.373%
</div> </div>
</div> </div>
<div <div
......
...@@ -9,6 +9,7 @@ import { ...@@ -9,6 +9,7 @@ import {
SharedEventName, SharedEventName,
SwapEventName, SwapEventName,
} from '@uniswap/analytics-events' } from '@uniswap/analytics-events'
import { formatCurrencyAmount, NumberType } from '@uniswap/conedison/format'
import { Currency, CurrencyAmount, Percent, Token } from '@uniswap/sdk-core' import { Currency, CurrencyAmount, Percent, Token } from '@uniswap/sdk-core'
import { UNIVERSAL_ROUTER_ADDRESS } from '@uniswap/universal-router-sdk' import { UNIVERSAL_ROUTER_ADDRESS } from '@uniswap/universal-router-sdk'
import { useWeb3React } from '@web3-react/core' import { useWeb3React } from '@web3-react/core'
...@@ -37,7 +38,6 @@ import { Text } from 'rebass' ...@@ -37,7 +38,6 @@ import { Text } from 'rebass'
import { useAppSelector } from 'state/hooks' import { useAppSelector } from 'state/hooks'
import { InterfaceTrade, TradeState } from 'state/routing/types' import { InterfaceTrade, TradeState } from 'state/routing/types'
import styled, { useTheme } from 'styled-components/macro' import styled, { useTheme } from 'styled-components/macro'
import { currencyAmountToPreciseFloat, formatTransactionAmount } from 'utils/formatNumbers'
import { didUserReject } from 'utils/swapErrorToUserReadableMessage' import { didUserReject } from 'utils/swapErrorToUserReadableMessage'
import AddressInputPanel from '../../components/AddressInputPanel' import AddressInputPanel from '../../components/AddressInputPanel'
...@@ -360,7 +360,7 @@ export function Swap({ ...@@ -360,7 +360,7 @@ export function Swap({
[independentField]: typedValue, [independentField]: typedValue,
[dependentField]: showWrap [dependentField]: showWrap
? parsedAmounts[independentField]?.toExact() ?? '' ? parsedAmounts[independentField]?.toExact() ?? ''
: formatTransactionAmount(currencyAmountToPreciseFloat(parsedAmounts[dependentField])), : formatCurrencyAmount(parsedAmounts[dependentField], NumberType.SwapTradeAmount, ''),
}), }),
[dependentField, independentField, parsedAmounts, showWrap, typedValue] [dependentField, independentField, parsedAmounts, showWrap, typedValue]
) )
......
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