2021-03-24 04:45:58 +03:00
|
|
|
import { ChainId, Currency, currencyEquals, Price, WETH9 } from '@uniswap/sdk-core'
|
|
|
|
import { JSBI } from '@uniswap/v2-sdk'
|
2020-08-17 16:36:38 +03:00
|
|
|
import { useMemo } from 'react'
|
|
|
|
import { USDC } from '../constants'
|
2021-04-24 01:57:48 +03:00
|
|
|
import { PairState, useV2Pairs } from './useV2Pairs'
|
2020-08-17 16:36:38 +03:00
|
|
|
import { useActiveWeb3React } from '../hooks'
|
2021-04-14 17:12:35 +03:00
|
|
|
import { wrappedCurrency } from '../utils/wrappedCurrency'
|
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
|
|
|
|
*/
|
|
|
|
export default function useUSDCPrice(currency?: Currency): Price | undefined {
|
|
|
|
const { chainId } = useActiveWeb3React()
|
|
|
|
const wrapped = wrappedCurrency(currency, chainId)
|
|
|
|
const tokenPairs: [Currency | undefined, Currency | undefined][] = useMemo(
|
|
|
|
() => [
|
|
|
|
[
|
2021-03-24 04:45:58 +03:00
|
|
|
chainId && wrapped && currencyEquals(WETH9[chainId], wrapped) ? undefined : currency,
|
|
|
|
chainId ? WETH9[chainId] : undefined,
|
2020-08-17 16:36:38 +03:00
|
|
|
],
|
|
|
|
[wrapped?.equals(USDC) ? undefined : wrapped, chainId === ChainId.MAINNET ? USDC : undefined],
|
2021-03-24 04:45:58 +03:00
|
|
|
[chainId ? WETH9[chainId] : undefined, chainId === ChainId.MAINNET ? USDC : undefined],
|
2020-08-17 16:36:38 +03:00
|
|
|
],
|
|
|
|
[chainId, currency, wrapped]
|
|
|
|
)
|
2021-04-24 01:57:48 +03:00
|
|
|
const [[ethPairState, ethPair], [usdcPairState, usdcPair], [usdcEthPairState, usdcEthPair]] = useV2Pairs(tokenPairs)
|
2020-08-17 16:36:38 +03:00
|
|
|
|
|
|
|
return useMemo(() => {
|
|
|
|
if (!currency || !wrapped || !chainId) {
|
2020-08-27 20:05:09 +03:00
|
|
|
return undefined
|
2020-08-17 16:36:38 +03:00
|
|
|
}
|
|
|
|
// handle weth/eth
|
2021-03-24 04:45:58 +03:00
|
|
|
if (wrapped.equals(WETH9[chainId])) {
|
2020-08-17 16:36:38 +03:00
|
|
|
if (usdcPair) {
|
2021-03-24 04:45:58 +03:00
|
|
|
const price = usdcPair.priceOf(WETH9[chainId])
|
2020-08-17 16:36:38 +03:00
|
|
|
return new Price(currency, USDC, price.denominator, price.numerator)
|
|
|
|
} else {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// handle usdc
|
|
|
|
if (wrapped.equals(USDC)) {
|
|
|
|
return new Price(USDC, USDC, '1', '1')
|
|
|
|
}
|
|
|
|
|
2021-03-24 04:45:58 +03:00
|
|
|
const ethPairETHAmount = ethPair?.reserveOf(WETH9[chainId])
|
2020-08-17 16:36:38 +03:00
|
|
|
const ethPairETHUSDCValue: JSBI =
|
2021-03-24 04:45:58 +03:00
|
|
|
ethPairETHAmount && usdcEthPair ? usdcEthPair.priceOf(WETH9[chainId]).quote(ethPairETHAmount).raw : JSBI.BigInt(0)
|
2020-08-17 16:36:38 +03:00
|
|
|
|
|
|
|
// all other tokens
|
|
|
|
// first try the usdc pair
|
|
|
|
if (usdcPairState === PairState.EXISTS && usdcPair && usdcPair.reserveOf(USDC).greaterThan(ethPairETHUSDCValue)) {
|
|
|
|
const price = usdcPair.priceOf(wrapped)
|
|
|
|
return new Price(currency, USDC, price.denominator, price.numerator)
|
|
|
|
}
|
|
|
|
if (ethPairState === PairState.EXISTS && ethPair && usdcEthPairState === PairState.EXISTS && usdcEthPair) {
|
2021-03-24 04:45:58 +03:00
|
|
|
if (usdcEthPair.reserveOf(USDC).greaterThan('0') && ethPair.reserveOf(WETH9[chainId]).greaterThan('0')) {
|
2020-08-17 16:36:38 +03:00
|
|
|
const ethUsdcPrice = usdcEthPair.priceOf(USDC)
|
2021-03-24 04:45:58 +03:00
|
|
|
const currencyEthPrice = ethPair.priceOf(WETH9[chainId])
|
2020-08-17 16:36:38 +03:00
|
|
|
const usdcPrice = ethUsdcPrice.multiply(currencyEthPrice).invert()
|
|
|
|
return new Price(currency, USDC, usdcPrice.denominator, usdcPrice.numerator)
|
|
|
|
}
|
|
|
|
}
|
2020-08-27 20:05:09 +03:00
|
|
|
return undefined
|
2020-08-17 16:36:38 +03:00
|
|
|
}, [chainId, currency, ethPair, ethPairState, usdcEthPair, usdcEthPairState, usdcPair, usdcPairState, wrapped])
|
|
|
|
}
|