Commit b2528792 authored by Moody Salem's avatar Moody Salem

return fake price data for non mainnet and fill it into the currency input panel

parent 65c51ea4
import { Pair } from '@uniswap/v2-sdk' import { Pair } from '@uniswap/v2-sdk'
import { Currency } from '@uniswap/sdk-core' import { Currency, CurrencyAmount } from '@uniswap/sdk-core'
import React, { useState, useCallback } from 'react' import React, { useState, useCallback, useMemo } from 'react'
import styled from 'styled-components' import styled from 'styled-components'
import { darken } from 'polished' import { darken } from 'polished'
import useUSDCPrice from '../../hooks/useUSDCPrice'
import { tryParseAmount } from '../../state/swap/hooks'
import { useCurrencyBalance } from '../../state/wallet/hooks' import { useCurrencyBalance } from '../../state/wallet/hooks'
import CurrencySearchModal from '../SearchModal/CurrencySearchModal' import CurrencySearchModal from '../SearchModal/CurrencySearchModal'
import CurrencyLogo from '../CurrencyLogo' import CurrencyLogo from '../CurrencyLogo'
...@@ -152,6 +154,7 @@ interface CurrencyInputPanelProps { ...@@ -152,6 +154,7 @@ interface CurrencyInputPanelProps {
pair?: Pair | null pair?: Pair | null
hideInput?: boolean hideInput?: boolean
otherCurrency?: Currency | null otherCurrency?: Currency | null
showFiatValue?: boolean
id: string id: string
showCommonBases?: boolean showCommonBases?: boolean
customBalanceText?: string customBalanceText?: string
...@@ -170,6 +173,7 @@ export default function CurrencyInputPanel({ ...@@ -170,6 +173,7 @@ export default function CurrencyInputPanel({
id, id,
showCommonBases, showCommonBases,
customBalanceText, customBalanceText,
showFiatValue = false,
hideBalance = false, hideBalance = false,
pair = null, // used for double token logo pair = null, // used for double token logo
hideInput = false, hideInput = false,
...@@ -187,7 +191,13 @@ export default function CurrencyInputPanel({ ...@@ -187,7 +191,13 @@ export default function CurrencyInputPanel({
setModalOpen(false) setModalOpen(false)
}, [setModalOpen]) }, [setModalOpen])
const disableCurrencySelect = typeof onCurrencySelect !== 'function' const price = useUSDCPrice(showFiatValue ? currency ?? undefined : undefined)
const fiatValueOfTypedAmount: CurrencyAmount | null = useMemo(() => {
if (!price) return null
const amount = tryParseAmount(value, currency ?? undefined)
return amount ? price.quote(amount) : null
}, [currency, price, value])
return ( return (
<InputPanel id={id} hideInput={hideInput} {...rest}> <InputPanel id={id} hideInput={hideInput} {...rest}>
...@@ -206,16 +216,17 @@ export default function CurrencyInputPanel({ ...@@ -206,16 +216,17 @@ export default function CurrencyInputPanel({
<TYPE.body color={theme.text3} fontWeight={500} fontSize={14}> <TYPE.body color={theme.text3} fontWeight={500} fontSize={14}>
{label} {label}
</TYPE.body> </TYPE.body>
{fiatValueOfTypedAmount ? <TYPE.label>${fiatValueOfTypedAmount?.toFixed(2)}</TYPE.label> : null}
</RowBetween> </RowBetween>
</LabelRow> </LabelRow>
)} )}
<InputRow style={hideInput ? { padding: '0', borderRadius: '8px' } : {}} selected={disableCurrencySelect}> <InputRow style={hideInput ? { padding: '0', borderRadius: '8px' } : {}} selected={!onCurrencySelect}>
<CurrencySelect <CurrencySelect
selected={!!currency} selected={!!currency}
hideInput={hideInput} hideInput={hideInput}
className="open-currency-select-button" className="open-currency-select-button"
onClick={() => { onClick={() => {
if (!disableCurrencySelect) { if (onCurrencySelect) {
setModalOpen(true) setModalOpen(true)
} }
}} }}
...@@ -243,19 +254,17 @@ export default function CurrencyInputPanel({ ...@@ -243,19 +254,17 @@ export default function CurrencyInputPanel({
</StyledTokenName> </StyledTokenName>
)} )}
</RowFixed> </RowFixed>
{!disableCurrencySelect && <StyledDropDown selected={!!currency} />} {onCurrencySelect && <StyledDropDown selected={!!currency} />}
</Aligner> </Aligner>
</CurrencySelect> </CurrencySelect>
{!hideInput && ( {!hideInput && (
<> <NumericalInput
<NumericalInput className="token-amount-input"
className="token-amount-input" value={value}
value={value} onUserInput={(val) => {
onUserInput={(val) => { onUserInput(val)
onUserInput(val) }}
}} />
/>
</>
)} )}
</InputRow> </InputRow>
{!hideInput && !hideBalance && ( {!hideInput && !hideBalance && (
...@@ -288,7 +297,7 @@ export default function CurrencyInputPanel({ ...@@ -288,7 +297,7 @@ export default function CurrencyInputPanel({
</LabelRow> </LabelRow>
)} )}
</Container> </Container>
{!disableCurrencySelect && onCurrencySelect && ( {onCurrencySelect && (
<CurrencySearchModal <CurrencySearchModal
isOpen={modalOpen} isOpen={modalOpen}
onDismiss={handleDismissSearch} onDismiss={handleDismissSearch}
......
...@@ -81,7 +81,6 @@ export const Input = React.memo(function InnerInput({ ...@@ -81,7 +81,6 @@ export const Input = React.memo(function InnerInput({
}} }}
// universal input options // universal input options
inputMode="decimal" inputMode="decimal"
title="Token Amount"
autoComplete="off" autoComplete="off"
autoCorrect="off" autoCorrect="off"
// text-specific options // text-specific options
......
import { ChainId, Currency, currencyEquals, Price, WETH9 } from '@uniswap/sdk-core' import { ChainId, Currency, currencyEquals, Price, Token, WETH9 } from '@uniswap/sdk-core'
import { JSBI } from '@uniswap/v2-sdk' import { JSBI } from '@uniswap/v2-sdk'
import { useMemo } from 'react' import { useMemo } from 'react'
import { USDC } from '../constants' import { USDC } from '../constants'
...@@ -13,16 +13,15 @@ import { wrappedCurrency } from '../utils/wrappedCurrency' ...@@ -13,16 +13,15 @@ import { wrappedCurrency } from '../utils/wrappedCurrency'
export default function useUSDCPrice(currency?: Currency): Price | undefined { export default function useUSDCPrice(currency?: Currency): Price | undefined {
const { chainId } = useActiveWeb3React() const { chainId } = useActiveWeb3React()
const wrapped = wrappedCurrency(currency, chainId) const wrapped = wrappedCurrency(currency, chainId)
const weth = WETH9[chainId as ChainId]
const tokenPairs: [Currency | undefined, Currency | undefined][] = useMemo( const tokenPairs: [Currency | undefined, Currency | undefined][] = useMemo(
() => [ () => [
[ [chainId && wrapped && currencyEquals(weth, wrapped) ? undefined : currency, chainId ? weth : undefined],
chainId && wrapped && currencyEquals(WETH9[chainId], wrapped) ? undefined : currency,
chainId ? WETH9[chainId] : undefined,
],
[wrapped?.equals(USDC) ? undefined : wrapped, chainId === ChainId.MAINNET ? USDC : undefined], [wrapped?.equals(USDC) ? undefined : wrapped, chainId === ChainId.MAINNET ? USDC : undefined],
[chainId ? WETH9[chainId] : undefined, chainId === ChainId.MAINNET ? USDC : undefined], [chainId ? weth : undefined, chainId === ChainId.MAINNET ? USDC : undefined],
], ],
[chainId, currency, wrapped] [chainId, currency, weth, wrapped]
) )
const [[ethPairState, ethPair], [usdcPairState, usdcPair], [usdcEthPairState, usdcEthPair]] = useV2Pairs(tokenPairs) const [[ethPairState, ethPair], [usdcPairState, usdcPair], [usdcEthPairState, usdcEthPair]] = useV2Pairs(tokenPairs)
...@@ -30,10 +29,21 @@ export default function useUSDCPrice(currency?: Currency): Price | undefined { ...@@ -30,10 +29,21 @@ export default function useUSDCPrice(currency?: Currency): Price | undefined {
if (!currency || !wrapped || !chainId) { if (!currency || !wrapped || !chainId) {
return undefined return undefined
} }
// return some fake price data for non-mainnet
if (chainId !== ChainId.MAINNET) {
const fakeUSDC = new Token(chainId, '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', 6, 'fUSDC', 'Fake USDC')
return new Price(
currency,
fakeUSDC,
10 ** Math.max(0, currency.decimals - 6),
15 * 10 ** Math.max(6 - currency.decimals, 0)
)
}
// handle weth/eth // handle weth/eth
if (wrapped.equals(WETH9[chainId])) { if (wrapped.equals(weth)) {
if (usdcPair) { if (usdcPair) {
const price = usdcPair.priceOf(WETH9[chainId]) const price = usdcPair.priceOf(weth)
return new Price(currency, USDC, price.denominator, price.numerator) return new Price(currency, USDC, price.denominator, price.numerator)
} else { } else {
return undefined return undefined
...@@ -44,9 +54,9 @@ export default function useUSDCPrice(currency?: Currency): Price | undefined { ...@@ -44,9 +54,9 @@ export default function useUSDCPrice(currency?: Currency): Price | undefined {
return new Price(USDC, USDC, '1', '1') return new Price(USDC, USDC, '1', '1')
} }
const ethPairETHAmount = ethPair?.reserveOf(WETH9[chainId]) const ethPairETHAmount = ethPair?.reserveOf(weth)
const ethPairETHUSDCValue: JSBI = const ethPairETHUSDCValue: JSBI =
ethPairETHAmount && usdcEthPair ? usdcEthPair.priceOf(WETH9[chainId]).quote(ethPairETHAmount).raw : JSBI.BigInt(0) ethPairETHAmount && usdcEthPair ? usdcEthPair.priceOf(weth).quote(ethPairETHAmount).raw : JSBI.BigInt(0)
// all other tokens // all other tokens
// first try the usdc pair // first try the usdc pair
...@@ -55,13 +65,13 @@ export default function useUSDCPrice(currency?: Currency): Price | undefined { ...@@ -55,13 +65,13 @@ export default function useUSDCPrice(currency?: Currency): Price | undefined {
return new Price(currency, USDC, price.denominator, price.numerator) return new Price(currency, USDC, price.denominator, price.numerator)
} }
if (ethPairState === PairState.EXISTS && ethPair && usdcEthPairState === PairState.EXISTS && usdcEthPair) { if (ethPairState === PairState.EXISTS && ethPair && usdcEthPairState === PairState.EXISTS && usdcEthPair) {
if (usdcEthPair.reserveOf(USDC).greaterThan('0') && ethPair.reserveOf(WETH9[chainId]).greaterThan('0')) { if (usdcEthPair.reserveOf(USDC).greaterThan('0') && ethPair.reserveOf(weth).greaterThan('0')) {
const ethUsdcPrice = usdcEthPair.priceOf(USDC) const ethUsdcPrice = usdcEthPair.priceOf(USDC)
const currencyEthPrice = ethPair.priceOf(WETH9[chainId]) const currencyEthPrice = ethPair.priceOf(weth)
const usdcPrice = ethUsdcPrice.multiply(currencyEthPrice).invert() const usdcPrice = ethUsdcPrice.multiply(currencyEthPrice).invert()
return new Price(currency, USDC, usdcPrice.denominator, usdcPrice.numerator) return new Price(currency, USDC, usdcPrice.denominator, usdcPrice.numerator)
} }
} }
return undefined return undefined
}, [chainId, currency, ethPair, ethPairState, usdcEthPair, usdcEthPairState, usdcPair, usdcPairState, wrapped]) }, [chainId, currency, ethPair, ethPairState, usdcEthPair, usdcEthPairState, usdcPair, usdcPairState, weth, wrapped])
} }
...@@ -341,6 +341,7 @@ export default function Swap({ history }: RouteComponentProps) { ...@@ -341,6 +341,7 @@ export default function Swap({ history }: RouteComponentProps) {
currency={currencies[Field.INPUT]} currency={currencies[Field.INPUT]}
onUserInput={handleTypeInput} onUserInput={handleTypeInput}
onMax={handleMaxInput} onMax={handleMaxInput}
showFiatValue
onCurrencySelect={handleInputSelect} onCurrencySelect={handleInputSelect}
otherCurrency={currencies[Field.OUTPUT]} otherCurrency={currencies[Field.OUTPUT]}
showCommonBases={true} showCommonBases={true}
...@@ -363,6 +364,7 @@ export default function Swap({ history }: RouteComponentProps) { ...@@ -363,6 +364,7 @@ export default function Swap({ history }: RouteComponentProps) {
label={independentField === Field.INPUT && !showWrap && trade ? 'To (minimum)' : 'To'} label={independentField === Field.INPUT && !showWrap && trade ? 'To (minimum)' : 'To'}
showMaxButton={false} showMaxButton={false}
hideBalance={true} hideBalance={true}
showFiatValue
currency={currencies[Field.OUTPUT]} currency={currencies[Field.OUTPUT]}
onCurrencySelect={handleOutputSelect} onCurrencySelect={handleOutputSelect}
otherCurrency={currencies[Field.INPUT]} otherCurrency={currencies[Field.INPUT]}
......
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