2023-02-09 03:41:15 -08:00
|
|
|
import { I80F48, PerpMarket } from '@blockworks-foundation/mango-v4'
|
|
|
|
import { useMemo } from 'react'
|
|
|
|
import useMangoGroup from './useMangoGroup'
|
|
|
|
import useSelectedMarket from './useSelectedMarket'
|
|
|
|
|
|
|
|
const useStablePrice = () => {
|
|
|
|
const { selectedMarket } = useSelectedMarket()
|
|
|
|
const { group } = useMangoGroup()
|
|
|
|
|
|
|
|
const banks = useMemo(() => {
|
|
|
|
if (!group) return []
|
|
|
|
return Array.from(group.banksMapByMint)
|
2023-02-26 17:04:02 -08:00
|
|
|
.map(([_mintAddress, banks]) => banks)
|
2023-02-09 03:41:15 -08:00
|
|
|
.map((b) => b[0])
|
|
|
|
}, [group])
|
|
|
|
|
|
|
|
const stablePrice = useMemo(() => {
|
|
|
|
if (!group || !selectedMarket || !banks.length) return 0
|
|
|
|
let stablePrice
|
|
|
|
if (selectedMarket instanceof PerpMarket) {
|
2023-02-14 21:09:11 -08:00
|
|
|
stablePrice = selectedMarket.stablePriceModel.stablePrice || 0
|
2023-02-09 03:41:15 -08:00
|
|
|
} else {
|
|
|
|
const baseBank = banks.find(
|
2023-07-21 11:47:53 -07:00
|
|
|
(b) => b.tokenIndex === selectedMarket.baseTokenIndex,
|
2023-02-09 03:41:15 -08:00
|
|
|
)
|
|
|
|
const quoteBank = banks.find(
|
2023-07-21 11:47:53 -07:00
|
|
|
(b) => b.tokenIndex === selectedMarket.quoteTokenIndex,
|
2023-02-09 03:41:15 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
const baseStablePrice = group.toUiPrice(
|
|
|
|
I80F48.fromNumber(baseBank!.stablePriceModel.stablePrice),
|
2023-07-21 11:47:53 -07:00
|
|
|
baseBank!.mintDecimals,
|
2023-02-09 03:41:15 -08:00
|
|
|
)
|
|
|
|
const quoteStablePrice = group.toUiPrice(
|
|
|
|
I80F48.fromNumber(quoteBank!.stablePriceModel.stablePrice),
|
2023-07-21 11:47:53 -07:00
|
|
|
quoteBank!.mintDecimals,
|
2023-02-09 03:41:15 -08:00
|
|
|
)
|
|
|
|
stablePrice = baseStablePrice / quoteStablePrice
|
|
|
|
}
|
|
|
|
return stablePrice
|
2023-02-26 17:04:02 -08:00
|
|
|
}, [banks, group, selectedMarket])
|
2023-02-09 03:41:15 -08:00
|
|
|
|
|
|
|
return stablePrice
|
|
|
|
}
|
|
|
|
|
|
|
|
export default useStablePrice
|