2021-05-20 19:21:40 +03:00
|
|
|
import { Currency, CurrencyAmount, Price, Token } from '@uniswap/sdk-core'
|
2020-08-17 16:36:38 +03:00
|
|
|
import { useMemo } from 'react'
|
2021-05-18 18:31:22 +03:00
|
|
|
import { USDC } from '../constants/tokens'
|
2021-05-18 20:07:37 +03:00
|
|
|
import { useV2TradeExactOut } from './useV2Trade'
|
|
|
|
import { useBestV3TradeExactOut, V3TradeState } from './useBestV3Trade'
|
2021-05-18 18:31:22 +03:00
|
|
|
import { useActiveWeb3React } from './web3'
|
2021-05-18 20:07:37 +03:00
|
|
|
|
|
|
|
// USDC amount used when calculating spot price for a given currency.
|
|
|
|
// The amount is large enough to filter low liquidity pairs.
|
2021-05-19 23:21:27 +03:00
|
|
|
const usdcCurrencyAmount = CurrencyAmount.fromRawAmount(USDC, 100_000e6)
|
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 {
|
2020-08-17 16:36:38 +03:00
|
|
|
const { chainId } = useActiveWeb3React()
|
2021-04-29 03:44:10 +03:00
|
|
|
|
2021-05-20 19:21:40 +03:00
|
|
|
const v2USDCTrade = useV2TradeExactOut(currency, chainId === 1 ? usdcCurrencyAmount : undefined, {
|
2021-05-19 23:21:27 +03:00
|
|
|
maxHops: 2,
|
|
|
|
})
|
2021-05-20 19:21:40 +03:00
|
|
|
const v3USDCTrade = useBestV3TradeExactOut(currency, chainId === 1 ? usdcCurrencyAmount : undefined)
|
2020-08-17 16:36:38 +03:00
|
|
|
|
|
|
|
return useMemo(() => {
|
2021-05-18 20:07:37 +03:00
|
|
|
if (!currency || !chainId) {
|
2020-08-27 20:05:09 +03:00
|
|
|
return undefined
|
2020-08-17 16:36:38 +03:00
|
|
|
}
|
2021-05-18 20:07:37 +03:00
|
|
|
|
2021-04-29 03:44:10 +03:00
|
|
|
// return some fake price data for non-mainnet
|
2021-05-20 19:21:40 +03:00
|
|
|
if (chainId !== 1) {
|
2021-04-29 03:44:10 +03:00
|
|
|
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)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-05-18 20:07:37 +03:00
|
|
|
// use v2 price if available, v3 as fallback
|
|
|
|
if (v2USDCTrade) {
|
|
|
|
const { numerator, denominator } = v2USDCTrade.route.midPrice
|
|
|
|
return new Price(currency, USDC, denominator, numerator)
|
|
|
|
} else if (v3USDCTrade.state === V3TradeState.VALID && v3USDCTrade.trade) {
|
|
|
|
const { numerator, denominator } = v3USDCTrade.trade.route.midPrice
|
|
|
|
return new Price(currency, USDC, denominator, numerator)
|
2020-08-17 16:36:38 +03:00
|
|
|
}
|
|
|
|
|
2020-08-27 20:05:09 +03:00
|
|
|
return undefined
|
2021-05-18 20:07:37 +03:00
|
|
|
}, [chainId, currency, v2USDCTrade, v3USDCTrade])
|
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])
|
|
|
|
}
|