2021-08-18 07:23:55 -07:00
|
|
|
import { I80F48 } from '@blockworks-foundation/mango-client'
|
2021-06-05 12:38:17 -07:00
|
|
|
import { useCallback, useEffect, useState } from 'react'
|
2021-06-05 11:40:56 -07:00
|
|
|
import useMangoStore from '../stores/useMangoStore'
|
2021-06-05 12:38:17 -07:00
|
|
|
|
2021-08-18 07:23:55 -07:00
|
|
|
export default function useOraclePrice(): I80F48 {
|
2021-07-05 08:03:57 -07:00
|
|
|
const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
|
|
|
|
const mangoCache = useMangoStore((s) => s.selectedMangoGroup.cache)
|
|
|
|
const selectedMarket = useMangoStore((s) => s.selectedMarket.config)
|
2021-06-05 11:40:56 -07:00
|
|
|
const [oraclePrice, setOraclePrice] = useState(null)
|
|
|
|
|
2021-06-05 12:38:17 -07:00
|
|
|
const fetchOraclePrice = useCallback(() => {
|
2021-12-09 09:23:19 -08:00
|
|
|
if (mangoGroup && mangoCache) {
|
2021-06-12 19:03:05 -07:00
|
|
|
setOraclePrice(null)
|
2021-07-05 08:03:57 -07:00
|
|
|
let marketIndex = 0
|
|
|
|
if (selectedMarket.kind === 'spot') {
|
|
|
|
marketIndex = mangoGroup.getSpotMarketIndex(selectedMarket.publicKey)
|
|
|
|
} else {
|
|
|
|
marketIndex = mangoGroup.getPerpMarketIndex(selectedMarket.publicKey)
|
|
|
|
}
|
|
|
|
setOraclePrice(mangoGroup.getPrice(marketIndex, mangoCache))
|
2021-06-05 11:40:56 -07:00
|
|
|
}
|
2021-07-05 08:03:57 -07:00
|
|
|
}, [mangoGroup, selectedMarket, mangoCache])
|
2021-06-05 12:38:17 -07:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
fetchOraclePrice()
|
|
|
|
}, [fetchOraclePrice])
|
|
|
|
|
2021-06-05 11:40:56 -07:00
|
|
|
return oraclePrice
|
|
|
|
}
|