feat: show usd price with no input amount (#7328)

* feat: show usd price with no input amount

* fix: snapshots

* fix: make tokens undefined only - no null

* fix: readability

* fix: syntax improvements
This commit is contained in:
eddie 2023-09-22 10:31:23 -07:00 committed by GitHub
parent 4f8956f79a
commit 7cd72a706d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 579 additions and 452 deletions

@ -190,7 +190,7 @@ export function useToken(tokenAddress?: string | null): Token | null | undefined
return useTokenFromMapOrNetwork(tokens, tokenAddress) return useTokenFromMapOrNetwork(tokens, tokenAddress)
} }
export function useCurrency(currencyId: Maybe<string>, chainId?: ChainId): Currency | null | undefined { export function useCurrency(currencyId: Maybe<string>, chainId?: ChainId): Currency | undefined {
const { chainId: connectedChainId } = useWeb3React() const { chainId: connectedChainId } = useWeb3React()
const tokens = useDefaultActiveTokens(chainId ?? connectedChainId) const tokens = useDefaultActiveTokens(chainId ?? connectedChainId)
return useCurrencyFromMap(tokens, chainId ?? connectedChainId, currencyId) return useCurrencyFromMap(tokens, chainId ?? connectedChainId, currencyId)

@ -80,7 +80,7 @@ type TokenMap = { [address: string]: Token }
* Returns null if token is loading or null was passed. * Returns null if token is loading or null was passed.
* Returns undefined if tokenAddress is invalid or token does not exist. * Returns undefined if tokenAddress is invalid or token does not exist.
*/ */
export function useTokenFromMapOrNetwork(tokens: TokenMap, tokenAddress?: string | null): Token | null | undefined { export function useTokenFromMapOrNetwork(tokens: TokenMap, tokenAddress?: string | null): Token | undefined {
const address = isAddress(tokenAddress) const address = isAddress(tokenAddress)
const token: Token | undefined = address ? tokens[address] : undefined const token: Token | undefined = address ? tokens[address] : undefined
const tokenFromNetwork = useTokenFromActiveNetwork(token ? undefined : address ? address : undefined) const tokenFromNetwork = useTokenFromActiveNetwork(token ? undefined : address ? address : undefined)
@ -106,7 +106,7 @@ export function useCurrencyFromMap(
tokens: TokenMap, tokens: TokenMap,
chainId: ChainId | undefined, chainId: ChainId | undefined,
currencyId?: string | null currencyId?: string | null
): Currency | null | undefined { ): Currency | undefined {
const nativeCurrency = useNativeCurrency(chainId) const nativeCurrency = useNativeCurrency(chainId)
const isNative = Boolean(nativeCurrency && currencyId?.toUpperCase() === 'ETH') const isNative = Boolean(nativeCurrency && currencyId?.toUpperCase() === 'ETH')
const shorthandMatchAddress = useMemo(() => { const shorthandMatchAddress = useMemo(() => {
@ -116,7 +116,7 @@ export function useCurrencyFromMap(
const token = useTokenFromMapOrNetwork(tokens, isNative ? undefined : shorthandMatchAddress ?? currencyId) const token = useTokenFromMapOrNetwork(tokens, isNative ? undefined : shorthandMatchAddress ?? currencyId)
if (currencyId === null || currencyId === undefined || !isSupportedChain(chainId)) return null if (currencyId === null || currencyId === undefined || !isSupportedChain(chainId)) return
// this case so we use our builtin wrapped token instead of wrapped tokens on token lists // this case so we use our builtin wrapped token instead of wrapped tokens on token lists
const wrappedNative = nativeCurrency?.wrapped const wrappedNative = nativeCurrency?.wrapped

File diff suppressed because it is too large Load Diff

@ -323,10 +323,19 @@ export function Swap({
[independentField, parsedAmount, showWrap, trade] [independentField, parsedAmount, showWrap, trade]
) )
const fiatValueInput = useUSDPrice(parsedAmounts[Field.INPUT], currencies[Field.INPUT] ?? undefined) const getSingleUnitAmount = (currency?: Currency) => {
const fiatValueOutput = useUSDPrice(parsedAmounts[Field.OUTPUT], currencies[Field.OUTPUT] ?? undefined) if (!currency) return
const showFiatValueInput = Boolean(parsedAmounts[Field.INPUT]) return CurrencyAmount.fromRawAmount(currency, JSBI.BigInt(10 ** currency.decimals))
const showFiatValueOutput = Boolean(parsedAmounts[Field.OUTPUT]) }
const fiatValueInput = useUSDPrice(
parsedAmounts[Field.INPUT] ?? getSingleUnitAmount(currencies[Field.INPUT]),
currencies[Field.INPUT]
)
const fiatValueOutput = useUSDPrice(
parsedAmounts[Field.OUTPUT] ?? getSingleUnitAmount(currencies[Field.OUTPUT]),
currencies[Field.OUTPUT]
)
const [routeNotFound, routeIsLoading, routeIsSyncing] = useMemo( const [routeNotFound, routeIsLoading, routeIsSyncing] = useMemo(
() => [ () => [
@ -632,7 +641,7 @@ export function Swap({
currency={currencies[Field.INPUT] ?? null} currency={currencies[Field.INPUT] ?? null}
onUserInput={handleTypeInput} onUserInput={handleTypeInput}
onMax={handleMaxInput} onMax={handleMaxInput}
fiatValue={showFiatValueInput ? fiatValueInput : undefined} fiatValue={fiatValueInput}
onCurrencySelect={handleInputSelect} onCurrencySelect={handleInputSelect}
otherCurrency={currencies[Field.OUTPUT]} otherCurrency={currencies[Field.OUTPUT]}
showCommonBases showCommonBases
@ -673,7 +682,7 @@ export function Swap({
label={<Trans>You receive</Trans>} label={<Trans>You receive</Trans>}
showMaxButton={false} showMaxButton={false}
hideBalance={false} hideBalance={false}
fiatValue={showFiatValueOutput ? fiatValueOutput : undefined} fiatValue={fiatValueOutput}
priceImpact={stablecoinPriceImpact} priceImpact={stablecoinPriceImpact}
currency={currencies[Field.OUTPUT] ?? null} currency={currencies[Field.OUTPUT] ?? null}
onCurrencySelect={handleOutputSelect} onCurrencySelect={handleOutputSelect}

@ -78,7 +78,7 @@ const BAD_RECIPIENT_ADDRESSES: { [address: string]: true } = {
} }
export type SwapInfo = { export type SwapInfo = {
currencies: { [field in Field]?: Currency | null } currencies: { [field in Field]?: Currency }
currencyBalances: { [field in Field]?: CurrencyAmount<Currency> } currencyBalances: { [field in Field]?: CurrencyAmount<Currency> }
inputTax: Percent inputTax: Percent
outputTax: Percent outputTax: Percent
@ -148,7 +148,7 @@ export function useDerivedSwapInfo(state: SwapState, chainId: ChainId | undefine
[relevantTokenBalances] [relevantTokenBalances]
) )
const currencies: { [field in Field]?: Currency | null } = useMemo( const currencies: { [field in Field]?: Currency } = useMemo(
() => ({ () => ({
[Field.INPUT]: inputCurrency, [Field.INPUT]: inputCurrency,
[Field.OUTPUT]: outputCurrency, [Field.OUTPUT]: outputCurrency,