diff --git a/components/trade/TradeHistory.tsx b/components/trade/TradeHistory.tsx index 64ea105e..aae28b58 100644 --- a/components/trade/TradeHistory.tsx +++ b/components/trade/TradeHistory.tsx @@ -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,