2021-09-21 20:51:50 +03:00
|
|
|
import { Currency, CurrencyAmount, Price, Token, TradeType } from '@uniswap/sdk-core'
|
2022-01-12 22:18:11 +03:00
|
|
|
import useActiveWeb3React from 'hooks/useActiveWeb3React'
|
2020-08-17 16:36:38 +03:00
|
|
|
import { useMemo } from 'react'
|
2021-12-16 22:44:03 +03:00
|
|
|
import { tryParseAmount } from 'state/swap/hooks'
|
2021-09-22 02:21:28 +03:00
|
|
|
|
2021-06-02 23:14:34 +03:00
|
|
|
import { SupportedChainId } from '../constants/chains'
|
2021-12-22 22:25:10 +03:00
|
|
|
import { DAI_OPTIMISM, USDC, USDC_ARBITRUM, USDC_POLYGON } from '../constants/tokens'
|
2021-09-21 20:51:50 +03:00
|
|
|
import { useBestV2Trade } from './useBestV2Trade'
|
|
|
|
import { useClientSideV3Trade } from './useClientSideV3Trade'
|
2021-05-18 20:07:37 +03:00
|
|
|
|
2021-06-02 23:14:34 +03:00
|
|
|
// Stablecoin amounts used when calculating spot price for a given currency.
|
2021-05-18 20:07:37 +03:00
|
|
|
// The amount is large enough to filter low liquidity pairs.
|
2021-12-16 22:44:03 +03:00
|
|
|
export const STABLECOIN_AMOUNT_OUT: { [chainId: number]: CurrencyAmount<Token> } = {
|
2021-06-02 23:14:34 +03:00
|
|
|
[SupportedChainId.MAINNET]: CurrencyAmount.fromRawAmount(USDC, 100_000e6),
|
2021-06-24 04:58:03 +03:00
|
|
|
[SupportedChainId.ARBITRUM_ONE]: CurrencyAmount.fromRawAmount(USDC_ARBITRUM, 10_000e6),
|
2021-07-12 19:03:10 +03:00
|
|
|
[SupportedChainId.OPTIMISM]: CurrencyAmount.fromRawAmount(DAI_OPTIMISM, 10_000e18),
|
2021-12-22 22:25:10 +03:00
|
|
|
[SupportedChainId.POLYGON]: CurrencyAmount.fromRawAmount(USDC_POLYGON, 10_000e6),
|
2021-06-02 23:14:34 +03:00
|
|
|
}
|
2020-08-17 16:36:38 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the price in USDC of the input currency
|
|
|
|
* @param currency currency to compute the USDC price of
|
|
|
|
*/
|
2021-05-12 16:52:17 +03:00
|
|
|
export default function useUSDCPrice(currency?: Currency): Price<Currency, Token> | undefined {
|
2021-12-16 22:44:03 +03:00
|
|
|
const chainId = currency?.chainId
|
2021-04-29 03:44:10 +03:00
|
|
|
|
2021-06-02 23:14:34 +03:00
|
|
|
const amountOut = chainId ? STABLECOIN_AMOUNT_OUT[chainId] : undefined
|
|
|
|
const stablecoin = amountOut?.currency
|
|
|
|
|
2021-12-16 22:44:03 +03:00
|
|
|
// TODO(#2808): remove dependency on useBestV2Trade
|
2021-09-21 20:51:50 +03:00
|
|
|
const v2USDCTrade = useBestV2Trade(TradeType.EXACT_OUTPUT, amountOut, currency, {
|
2021-05-19 23:21:27 +03:00
|
|
|
maxHops: 2,
|
|
|
|
})
|
2021-09-21 20:51:50 +03:00
|
|
|
const v3USDCTrade = useClientSideV3Trade(TradeType.EXACT_OUTPUT, amountOut, currency)
|
2020-08-17 16:36:38 +03:00
|
|
|
|
|
|
|
return useMemo(() => {
|
2021-06-02 23:14:34 +03:00
|
|
|
if (!currency || !stablecoin) {
|
2021-06-02 01:33:31 +03:00
|
|
|
return undefined
|
2021-04-29 03:44:10 +03:00
|
|
|
}
|
|
|
|
|
2021-05-22 02:45:39 +03:00
|
|
|
// handle usdc
|
2021-06-02 23:14:34 +03:00
|
|
|
if (currency?.wrapped.equals(stablecoin)) {
|
|
|
|
return new Price(stablecoin, stablecoin, '1', '1')
|
2021-05-22 02:45:39 +03:00
|
|
|
}
|
|
|
|
|
2021-05-18 20:07:37 +03:00
|
|
|
// use v2 price if available, v3 as fallback
|
|
|
|
if (v2USDCTrade) {
|
|
|
|
const { numerator, denominator } = v2USDCTrade.route.midPrice
|
2021-06-02 23:14:34 +03:00
|
|
|
return new Price(currency, stablecoin, denominator, numerator)
|
2021-05-27 23:28:45 +03:00
|
|
|
} else if (v3USDCTrade.trade) {
|
2021-12-16 22:44:03 +03:00
|
|
|
const { numerator, denominator } = v3USDCTrade.trade.routes[0].midPrice
|
2021-06-02 23:14:34 +03:00
|
|
|
return new Price(currency, stablecoin, denominator, numerator)
|
2020-08-17 16:36:38 +03:00
|
|
|
}
|
|
|
|
|
2020-08-27 20:05:09 +03:00
|
|
|
return undefined
|
2021-06-02 23:14:34 +03:00
|
|
|
}, [currency, stablecoin, v2USDCTrade, v3USDCTrade.trade])
|
2020-08-17 16:36:38 +03:00
|
|
|
}
|
2021-05-04 00:29:45 +03:00
|
|
|
|
2021-05-12 16:52:17 +03:00
|
|
|
export function useUSDCValue(currencyAmount: CurrencyAmount<Currency> | undefined | null) {
|
2021-05-04 00:29:45 +03:00
|
|
|
const price = useUSDCPrice(currencyAmount?.currency)
|
|
|
|
|
|
|
|
return useMemo(() => {
|
|
|
|
if (!price || !currencyAmount) return null
|
2021-05-14 01:27:34 +03:00
|
|
|
try {
|
|
|
|
return price.quote(currencyAmount)
|
|
|
|
} catch (error) {
|
|
|
|
return null
|
|
|
|
}
|
2021-05-04 00:29:45 +03:00
|
|
|
}, [currencyAmount, price])
|
|
|
|
}
|
2021-12-16 22:44:03 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param fiatValue string representation of a USD amount
|
|
|
|
* @returns CurrencyAmount where currency is stablecoin on active chain
|
|
|
|
*/
|
|
|
|
export function useStablecoinAmountFromFiatValue(fiatValue: string | null | undefined) {
|
|
|
|
const { chainId } = useActiveWeb3React()
|
|
|
|
const stablecoin = chainId ? STABLECOIN_AMOUNT_OUT[chainId]?.currency : undefined
|
|
|
|
|
|
|
|
if (fiatValue === null || fiatValue === undefined || !chainId || !stablecoin) {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
// trim for decimal precision when parsing
|
|
|
|
const parsedForDecimals = parseFloat(fiatValue).toFixed(stablecoin.decimals).toString()
|
|
|
|
|
|
|
|
try {
|
|
|
|
// parse USD string into CurrencyAmount based on stablecoin decimals
|
|
|
|
return tryParseAmount(parsedForDecimals, stablecoin)
|
|
|
|
} catch (error) {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
}
|