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

feat: quote token price for nfts (#5897)

* feat: adds input token quote for nfts

* remove eslint

* correct usdc pricing
parent a7c1bd43
import { BigNumber } from '@ethersproject/bignumber' import { BigNumber } from '@ethersproject/bignumber'
import { formatEther } from '@ethersproject/units'
import { parseEther } from '@ethersproject/units' import { parseEther } from '@ethersproject/units'
import { Trans } from '@lingui/macro' import { Trans } from '@lingui/macro'
import { TraceEvent } from '@uniswap/analytics' import { TraceEvent } from '@uniswap/analytics'
import { BrowserEvent, InterfaceElementName, NFTEventName } from '@uniswap/analytics-events' import { BrowserEvent, InterfaceElementName, NFTEventName } from '@uniswap/analytics-events'
import { Currency } from '@uniswap/sdk-core' import { Currency, TradeType } from '@uniswap/sdk-core'
import { useWeb3React } from '@web3-react/core' import { useWeb3React } from '@web3-react/core'
import Column from 'components/Column' import Column from 'components/Column'
import Loader from 'components/Loader' import Loader from 'components/Loader'
...@@ -12,6 +13,9 @@ import Row from 'components/Row' ...@@ -12,6 +13,9 @@ import Row from 'components/Row'
import { SupportedChainId } from 'constants/chains' import { SupportedChainId } from 'constants/chains'
import { PayWithAnyTokenVariant, usePayWithAnyTokenFlag } from 'featureFlags/flags/payWithAnyToken' import { PayWithAnyTokenVariant, usePayWithAnyTokenFlag } from 'featureFlags/flags/payWithAnyToken'
import { useCurrency } from 'hooks/Tokens' import { useCurrency } from 'hooks/Tokens'
import { useBestTrade } from 'hooks/useBestTrade'
import { useStablecoinValue } from 'hooks/useStablecoinPrice'
import tryParseCurrencyAmount from 'lib/utils/tryParseCurrencyAmount'
import { useBag } from 'nft/hooks/useBag' import { useBag } from 'nft/hooks/useBag'
import { useTokenInput } from 'nft/hooks/useTokenInput' import { useTokenInput } from 'nft/hooks/useTokenInput'
import { useWalletBalance } from 'nft/hooks/useWalletBalance' import { useWalletBalance } from 'nft/hooks/useWalletBalance'
...@@ -20,6 +24,7 @@ import { ethNumberStandardFormatter, formatWeiToDecimal } from 'nft/utils' ...@@ -20,6 +24,7 @@ import { ethNumberStandardFormatter, formatWeiToDecimal } from 'nft/utils'
import { PropsWithChildren, useMemo, useReducer } from 'react' import { PropsWithChildren, useMemo, useReducer } from 'react'
import { AlertTriangle, ChevronDown } from 'react-feather' import { AlertTriangle, ChevronDown } from 'react-feather'
import { useToggleWalletModal } from 'state/application/hooks' import { useToggleWalletModal } from 'state/application/hooks'
import { TradeState } from 'state/routing/types'
import styled, { useTheme } from 'styled-components/macro' import styled, { useTheme } from 'styled-components/macro'
import { ThemedText } from 'theme' import { ThemedText } from 'theme'
import { switchChain } from 'utils/switchChain' import { switchChain } from 'utils/switchChain'
...@@ -199,6 +204,19 @@ export const BagFooter = ({ ...@@ -199,6 +204,19 @@ export const BagFooter = ({
const isPending = PENDING_BAG_STATUSES.includes(bagStatus) const isPending = PENDING_BAG_STATUSES.includes(bagStatus)
const activeCurrency = inputCurrency ?? defaultCurrency const activeCurrency = inputCurrency ?? defaultCurrency
const parsedAmount = useMemo(() => {
if (!inputCurrency) return undefined
return tryParseCurrencyAmount(formatEther(totalEthPrice.toString()), defaultCurrency ?? undefined)
}, [defaultCurrency, totalEthPrice, inputCurrency])
const { state: swapState, trade: swapTrade } = useBestTrade(
TradeType.EXACT_OUTPUT,
parsedAmount,
inputCurrency ?? undefined
)
const usdcValue = useStablecoinValue(swapTrade?.inputAmount)
return ( return (
<FooterContainer> <FooterContainer>
<Footer> <Footer>
...@@ -221,12 +239,16 @@ export const BagFooter = ({ ...@@ -221,12 +239,16 @@ export const BagFooter = ({
<Trans>Total</Trans> <Trans>Total</Trans>
</ThemedText.SubHeaderSmall> </ThemedText.SubHeaderSmall>
<ThemedText.HeadlineSmall> <ThemedText.HeadlineSmall>
{formatWeiToDecimal(totalEthPrice.toString())}&nbsp;{activeCurrency?.symbol ?? 'ETH'} {inputCurrency
? swapState !== TradeState.VALID
? '-'
: ethNumberStandardFormatter(swapTrade?.inputAmount.toExact())
: formatWeiToDecimal(totalEthPrice.toString())}
&nbsp;{activeCurrency?.symbol ?? 'ETH'}
</ThemedText.HeadlineSmall> </ThemedText.HeadlineSmall>
<ThemedText.BodySmall color="textSecondary" lineHeight="20px">{`${ethNumberStandardFormatter( <ThemedText.BodySmall color="textSecondary" lineHeight="20px">
totalUsdPrice, {`${ethNumberStandardFormatter(inputCurrency ? usdcValue?.toExact() : totalUsdPrice, true)}`}
true </ThemedText.BodySmall>
)}`}</ThemedText.BodySmall>
</TotalColumn> </TotalColumn>
</CurrencyRow> </CurrencyRow>
)} )}
...@@ -267,7 +289,7 @@ export const BagFooter = ({ ...@@ -267,7 +289,7 @@ export const BagFooter = ({
{showTokenSelector && ( {showTokenSelector && (
<BagTokenSelectorModal <BagTokenSelectorModal
selectedCurrency={activeCurrency ?? undefined} selectedCurrency={activeCurrency ?? undefined}
handleCurrencySelect={(currency: Currency) => { handleCurrencySelect={(currency: Currency | undefined) => {
setInputCurrency(currency) setInputCurrency(currency)
toggleTokenSelector() toggleTokenSelector()
}} }}
......
...@@ -49,7 +49,7 @@ const TokenSelectorContainer = styled(Column)` ...@@ -49,7 +49,7 @@ const TokenSelectorContainer = styled(Column)`
interface BagTokenSelectorModalProps { interface BagTokenSelectorModalProps {
selectedCurrency: Currency | undefined selectedCurrency: Currency | undefined
handleCurrencySelect: (currency: Currency) => void handleCurrencySelect: (currency: Currency | undefined) => void
overlayClick: () => void overlayClick: () => void
} }
......
...@@ -35,7 +35,7 @@ const StyledCheck = styled(Check)` ...@@ -35,7 +35,7 @@ const StyledCheck = styled(Check)`
interface CurrencyRowProps { interface CurrencyRowProps {
currency: Currency currency: Currency
selected: boolean selected: boolean
selectCurrency: (currency: Currency) => void selectCurrency: (currency: Currency | undefined) => void
} }
export const CurrencyRow = ({ currency, selected, selectCurrency }: CurrencyRowProps) => { export const CurrencyRow = ({ currency, selected, selectCurrency }: CurrencyRowProps) => {
...@@ -43,7 +43,7 @@ export const CurrencyRow = ({ currency, selected, selectCurrency }: CurrencyRowP ...@@ -43,7 +43,7 @@ export const CurrencyRow = ({ currency, selected, selectCurrency }: CurrencyRowP
const balance = useCurrencyBalance(account ?? undefined, currency) const balance = useCurrencyBalance(account ?? undefined, currency)
return ( return (
<TokenRow onClick={() => selectCurrency(currency)}> <TokenRow onClick={() => selectCurrency(currency.isNative ? undefined : currency)}>
<TokenInfoRow> <TokenInfoRow>
<CurrencyLogo currency={currency} size="36px" /> <CurrencyLogo currency={currency} size="36px" />
<Column> <Column>
......
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