fix bug in sorting trade history from event queue

This commit is contained in:
tjs 2023-04-14 11:56:36 -04:00
parent 442af25a09
commit 2a49dc1c5c
1 changed files with 37 additions and 17 deletions

View File

@ -174,6 +174,28 @@ const formatTradeHistory = (
})
}
const filterNewFills = (
eventQueueFills: (SerumEvent | PerpFillEvent)[],
apiTradeHistory: (SpotTradeHistory | PerpTradeHistory)[]
): (SerumEvent | PerpFillEvent)[] => {
return eventQueueFills.filter((fill) => {
return !apiTradeHistory.find((trade) => {
if ('order_id' in trade && isSerumFillEvent(fill)) {
return trade.order_id === fill.orderId.toString()
} else if ('seq_num' in trade && isPerpFillEvent(fill)) {
const fillTimestamp = new Date(
fill.timestamp.toNumber() * 1000
).getTime()
const lastApiTradeTimestamp = new Date(
apiTradeHistory[apiTradeHistory.length - 1].block_datetime
).getTime()
if (fillTimestamp < lastApiTradeTimestamp) return true
return trade.seq_num === fill.seqNum.toNumber()
}
})
})
}
const TradeHistory = () => {
const { t } = useTranslation(['common', 'trade'])
const group = mangoStore.getState().group
@ -225,23 +247,21 @@ const TradeHistory = () => {
const combinedTradeHistory = useMemo(() => {
const group = mangoStore.getState().group
if (!group || !selectedMarket) return []
let newFills: (SerumEvent | PerpFillEvent)[] = []
const combinedTradeHistoryPages = tradeHistoryFromApi?.pages.flat() ?? []
if (eventQueueFillsForOwner?.length) {
newFills = eventQueueFillsForOwner.filter((fill) => {
return !combinedTradeHistoryPages.find((t) => {
if ('order_id' in t && isSerumFillEvent(fill)) {
return t.order_id === fill.orderId.toString()
} else if ('seq_num' in t && isPerpFillEvent(fill)) {
return t.seq_num === fill.seqNum.toNumber()
}
})
})
}
return formatTradeHistory(group, selectedMarket, mangoAccountAddress, [
...newFills,
...combinedTradeHistoryPages,
])
const apiTradeHistory = tradeHistoryFromApi?.pages.flat() ?? []
const newFills: (SerumEvent | PerpFillEvent)[] =
eventQueueFillsForOwner?.length
? filterNewFills(eventQueueFillsForOwner, apiTradeHistory)
: []
const combinedHistory = [...newFills, ...apiTradeHistory]
return formatTradeHistory(
group,
selectedMarket,
mangoAccountAddress,
combinedHistory
)
}, [
eventQueueFillsForOwner,
mangoAccountAddress,