2023-08-14 07:03:11 -07:00
|
|
|
import { PerpOrder } from '@blockworks-foundation/mango-v4'
|
|
|
|
import mangoStore from '@store/mangoStore'
|
|
|
|
import { useQuery } from '@tanstack/react-query'
|
|
|
|
import { fetchFilledOrders } from 'utils/account'
|
|
|
|
import useMangoAccount from './useMangoAccount'
|
2023-08-31 04:40:25 -07:00
|
|
|
import { useMemo } from 'react'
|
2023-08-14 07:03:11 -07:00
|
|
|
|
|
|
|
export default function useFilledOrders() {
|
|
|
|
const { mangoAccount, mangoAccountAddress } = useMangoAccount()
|
|
|
|
const openOrders = mangoStore((s) => s.mangoAccount.openOrders)
|
|
|
|
|
2023-08-31 04:40:25 -07:00
|
|
|
const orderIds = useMemo(() => {
|
|
|
|
if (!mangoAccount || !Object.values(openOrders).flat().length) return []
|
|
|
|
const perpIds = mangoAccount.perpOpenOrders
|
|
|
|
.filter((o) => o.orderMarket !== 65535)
|
|
|
|
.map((p) => p.clientId.toString())
|
|
|
|
const spotIds = Object.values(openOrders)
|
|
|
|
.flat()
|
|
|
|
.filter((o) => !(o instanceof PerpOrder))
|
|
|
|
.map((s) => s.orderId.toString())
|
|
|
|
const ids = spotIds.concat(perpIds)
|
|
|
|
return ids
|
|
|
|
}, [mangoAccount, openOrders])
|
2023-08-14 07:03:11 -07:00
|
|
|
|
|
|
|
const {
|
|
|
|
data: filledOrders,
|
|
|
|
isFetching: fetchingFilledOrders,
|
|
|
|
refetch,
|
|
|
|
} = useQuery(
|
2023-08-31 04:40:25 -07:00
|
|
|
['filled-orders', mangoAccountAddress, orderIds],
|
2023-08-14 07:03:11 -07:00
|
|
|
() => fetchFilledOrders(mangoAccountAddress, orderIds),
|
|
|
|
{
|
|
|
|
cacheTime: 1000 * 60 * 10,
|
|
|
|
staleTime: 30,
|
|
|
|
retry: 3,
|
|
|
|
refetchOnWindowFocus: false,
|
2023-08-31 04:40:25 -07:00
|
|
|
enabled: !!mangoAccountAddress && !!orderIds.length,
|
2023-08-14 07:03:11 -07:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
return {
|
|
|
|
filledOrders,
|
|
|
|
fetchingFilledOrders,
|
|
|
|
refetch,
|
|
|
|
}
|
|
|
|
}
|