Skip to content

Commit f1b300a

Browse files
committed
add a hook for getting the USDC price of any currency
1 parent 600049b commit f1b300a

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

src/utils/useUSDCPrice.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { ChainId, Currency, currencyEquals, JSBI, Price, WETH } from '@uniswap/sdk'
2+
import { useMemo } from 'react'
3+
import { USDC } from '../constants'
4+
import { PairState, usePairs } from '../data/Reserves'
5+
import { useActiveWeb3React } from '../hooks'
6+
import { wrappedCurrency } from './wrappedCurrency'
7+
8+
/**
9+
* Returns the price in USDC of the input currency
10+
* @param currency currency to compute the USDC price of
11+
*/
12+
export default function useUSDCPrice(currency?: Currency): Price | undefined {
13+
const { chainId } = useActiveWeb3React()
14+
const wrapped = wrappedCurrency(currency, chainId)
15+
const tokenPairs: [Currency | undefined, Currency | undefined][] = useMemo(
16+
() => [
17+
[
18+
chainId && wrapped && currencyEquals(WETH[chainId], wrapped) ? undefined : currency,
19+
chainId ? WETH[chainId] : undefined
20+
],
21+
[wrapped?.equals(USDC) ? undefined : wrapped, chainId === ChainId.MAINNET ? USDC : undefined],
22+
[chainId ? WETH[chainId] : undefined, chainId === ChainId.MAINNET ? USDC : undefined]
23+
],
24+
[chainId, currency, wrapped]
25+
)
26+
const [[ethPairState, ethPair], [usdcPairState, usdcPair], [usdcEthPairState, usdcEthPair]] = usePairs(tokenPairs)
27+
28+
return useMemo(() => {
29+
if (!currency || !wrapped || !chainId) {
30+
return
31+
}
32+
// handle weth/eth
33+
if (wrapped.equals(WETH[chainId])) {
34+
if (usdcPair) {
35+
const price = usdcPair.priceOf(WETH[chainId])
36+
return new Price(currency, USDC, price.denominator, price.numerator)
37+
} else {
38+
return undefined
39+
}
40+
}
41+
// handle usdc
42+
if (wrapped.equals(USDC)) {
43+
return new Price(USDC, USDC, '1', '1')
44+
}
45+
46+
const ethPairETHAmount = ethPair?.reserveOf(WETH[chainId])
47+
const ethPairETHUSDCValue: JSBI =
48+
ethPairETHAmount && usdcEthPair ? usdcEthPair.priceOf(WETH[chainId]).quote(ethPairETHAmount).raw : JSBI.BigInt(0)
49+
50+
// all other tokens
51+
// first try the usdc pair
52+
if (usdcPairState === PairState.EXISTS && usdcPair && usdcPair.reserveOf(USDC).greaterThan(ethPairETHUSDCValue)) {
53+
const price = usdcPair.priceOf(wrapped)
54+
return new Price(currency, USDC, price.denominator, price.numerator)
55+
}
56+
if (ethPairState === PairState.EXISTS && ethPair && usdcEthPairState === PairState.EXISTS && usdcEthPair) {
57+
if (usdcEthPair.reserveOf(USDC).greaterThan('0') && ethPair.reserveOf(WETH[chainId]).greaterThan('0')) {
58+
const ethUsdcPrice = usdcEthPair.priceOf(USDC)
59+
const currencyEthPrice = ethPair.priceOf(WETH[chainId])
60+
const usdcPrice = ethUsdcPrice.multiply(currencyEthPrice).invert()
61+
return new Price(currency, USDC, usdcPrice.denominator, usdcPrice.numerator)
62+
}
63+
}
64+
return
65+
}, [chainId, currency, ethPair, ethPairState, usdcEthPair, usdcEthPairState, usdcPair, usdcPairState, wrapped])
66+
}

0 commit comments

Comments
 (0)