mango-v4-ui/hooks/useMarkPrice.tsx

33 lines
1.0 KiB
TypeScript
Raw Normal View History

2022-11-25 02:10:23 -08:00
import mangoStore from '@store/mangoStore'
import { useEffect } from 'react'
2023-01-02 11:50:09 -08:00
const set = mangoStore.getState().set
2022-11-25 02:10:23 -08:00
export default function useMarkPrice() {
const markPrice = mangoStore((s) => s.selectedMarket.markPrice)
const orderbook = mangoStore((s) => s.selectedMarket.orderbook)
const fills = mangoStore((s) => s.selectedMarket.fills)
useEffect(() => {
const bb = orderbook?.bids?.length > 0 && Number(orderbook.bids[0][0])
const ba = orderbook?.asks?.length > 0 && Number(orderbook.asks[0][0])
const last = fills && fills.length > 0 && fills[0].price
2022-11-25 02:10:23 -08:00
if (bb && ba && last) {
const priceElements = [bb, ba, last]
.filter((e) => e)
.sort((a, b) => a - b)
const newMarkPrice = priceElements.length
? priceElements[Math.floor(priceElements.length / 2)]
: null
if (newMarkPrice && newMarkPrice !== markPrice) {
set((state) => {
state.selectedMarket.markPrice = newMarkPrice
})
}
2022-11-25 02:10:23 -08:00
}
}, [orderbook, fills, markPrice])
2023-01-02 11:50:09 -08:00
2022-11-25 02:10:23 -08:00
return markPrice
}