diff --git a/apps/cowswap-frontend/src/modules/twap/hooks/useEmulatedPartOrders.ts b/apps/cowswap-frontend/src/modules/twap/hooks/useEmulatedPartOrders.ts index 6293084099..ed6154674f 100644 --- a/apps/cowswap-frontend/src/modules/twap/hooks/useEmulatedPartOrders.ts +++ b/apps/cowswap-frontend/src/modules/twap/hooks/useEmulatedPartOrders.ts @@ -46,7 +46,9 @@ function emulatePartOrders( const enrichedOrder = emulatePartAsOrder(item, parent) const order = mapPartOrderToStoreOrder(item, enrichedOrder, isVirtualPart, parent, tokensByAddress) - acc.push(order) + if (order) { + acc.push(order) + } return acc }, []) diff --git a/apps/cowswap-frontend/src/modules/twap/hooks/useEmulatedTwapOrders.ts b/apps/cowswap-frontend/src/modules/twap/hooks/useEmulatedTwapOrders.ts index 210fc69db8..cef086df37 100644 --- a/apps/cowswap-frontend/src/modules/twap/hooks/useEmulatedTwapOrders.ts +++ b/apps/cowswap-frontend/src/modules/twap/hooks/useEmulatedTwapOrders.ts @@ -32,7 +32,10 @@ export function useEmulatedTwapOrders(tokensByAddress: TokensByAddress | undefin return acc } - acc.push(mapTwapOrderToStoreOrder(order, tokensByAddress)) + const orderDetails = mapTwapOrderToStoreOrder(order, tokensByAddress) + if (orderDetails) { + acc.push(orderDetails) + } return acc }, []) }, [allTwapOrders, accountLowerCase, chainId, tokensByAddress, refresher]) diff --git a/apps/cowswap-frontend/src/modules/twap/updaters/CreatedInOrderBookOrdersUpdater.tsx b/apps/cowswap-frontend/src/modules/twap/updaters/CreatedInOrderBookOrdersUpdater.tsx index 2317c59b22..05d92cc944 100644 --- a/apps/cowswap-frontend/src/modules/twap/updaters/CreatedInOrderBookOrdersUpdater.tsx +++ b/apps/cowswap-frontend/src/modules/twap/updaters/CreatedInOrderBookOrdersUpdater.tsx @@ -14,7 +14,7 @@ import { useTokensForOrdersList, getTokensListFromOrders, useSWRProdOrders } fro import { twapOrdersAtom } from '../state/twapOrdersListAtom' import { TwapPartOrderItem, twapPartOrdersListAtom, updatePartOrdersAtom } from '../state/twapPartOrdersAtom' import { TwapOrderItem } from '../types' -import { mapPartOrderToStoreOrder } from '../utils/mapPartOrderToStoreOrder' +import { isOrder, mapPartOrderToStoreOrder } from '../utils/mapPartOrderToStoreOrder' interface TwapOrderInfo { item: TwapPartOrderItem @@ -65,9 +65,11 @@ export function CreatedInOrderBookOrdersUpdater() { const allTokens = await getTokensForOrdersList(getTokensListFromOrders(ordersInfo)) - return ordersInfo.map(({ item, parent, order }) => { - return mapPartOrderToStoreOrder(item, order, isVirtualPart, parent, allTokens) - }) + return ordersInfo + .map(({ item, parent, order }) => { + return mapPartOrderToStoreOrder(item, order, isVirtualPart, parent, allTokens) + }) + .filter(isOrder) }, [prodOrders, twapPartOrdersMap, getTokensForOrdersList, twapOrders], [] diff --git a/apps/cowswap-frontend/src/modules/twap/utils/mapPartOrderToStoreOrder.ts b/apps/cowswap-frontend/src/modules/twap/utils/mapPartOrderToStoreOrder.ts index 1a5989c00a..19822b9fc4 100644 --- a/apps/cowswap-frontend/src/modules/twap/utils/mapPartOrderToStoreOrder.ts +++ b/apps/cowswap-frontend/src/modules/twap/utils/mapPartOrderToStoreOrder.ts @@ -17,10 +17,19 @@ export function mapPartOrderToStoreOrder( isVirtualPart: boolean, parent: TwapOrderItem, tokensByAddress: TokensByAddress -): Order { +): Order | undefined { const isCancelling = item.isCancelling || parent.status === TwapOrderStatus.Cancelling const status = getPartOrderStatus(enrichedOrder, parent, isVirtualPart) + const inputToken = tokensByAddress[enrichedOrder.sellToken.toLowerCase()] + const outputToken = tokensByAddress[enrichedOrder.buyToken.toLowerCase()] + + if (!inputToken || !outputToken) { + // FIXME: this is a hack to prevent errors, we should ensure this doesn't happen + console.error('mapTwapOrderToStoreOrder: inputToken or outputToken not found', { inputToken, outputToken }) + return undefined + } + const storeOrder: Order = { ...enrichedOrder, id: enrichedOrder.uid, @@ -30,8 +39,8 @@ export function mapPartOrderToStoreOrder( parentId: parent.id, }, sellAmountBeforeFee: enrichedOrder.sellAmount, - inputToken: tokensByAddress[enrichedOrder.sellToken.toLowerCase()], - outputToken: tokensByAddress[enrichedOrder.buyToken.toLowerCase()], + inputToken, + outputToken, creationTime: enrichedOrder.creationDate, summary: '', status, @@ -45,3 +54,7 @@ export function mapPartOrderToStoreOrder( return storeOrder } + +export function isOrder(order: Order | undefined): order is Order { + return !!order +} diff --git a/apps/cowswap-frontend/src/modules/twap/utils/mapTwapOrderToStoreOrder.ts b/apps/cowswap-frontend/src/modules/twap/utils/mapTwapOrderToStoreOrder.ts index d9d04035ae..1830dbd78d 100644 --- a/apps/cowswap-frontend/src/modules/twap/utils/mapTwapOrderToStoreOrder.ts +++ b/apps/cowswap-frontend/src/modules/twap/utils/mapTwapOrderToStoreOrder.ts @@ -17,10 +17,19 @@ const statusesMap: Record = { [TwapOrderStatus.Cancelling]: OrderStatus.PENDING, } -export function mapTwapOrderToStoreOrder(order: TwapOrderItem, tokensByAddress: TokensByAddress): Order { +export function mapTwapOrderToStoreOrder(order: TwapOrderItem, tokensByAddress: TokensByAddress): Order | undefined { const enrichedOrder = emulateTwapAsOrder(order) const status = statusesMap[order.status] + const inputToken = tokensByAddress[enrichedOrder.sellToken.toLowerCase()] + const outputToken = tokensByAddress[enrichedOrder.buyToken.toLowerCase()] + + if (!inputToken || !outputToken) { + // FIXME: this is a hack to prevent errors, we should ensure this doesn't happen + console.error('mapTwapOrderToStoreOrder: inputToken or outputToken not found', { inputToken, outputToken }) + return undefined + } + const storeOrder: Order = { ...enrichedOrder, id: enrichedOrder.uid, @@ -28,8 +37,8 @@ export function mapTwapOrderToStoreOrder(order: TwapOrderItem, tokensByAddress: id: order.id, }, sellAmountBeforeFee: enrichedOrder.sellAmount, - inputToken: tokensByAddress[enrichedOrder.sellToken.toLowerCase()], - outputToken: tokensByAddress[enrichedOrder.buyToken.toLowerCase()], + inputToken, + outputToken, creationTime: enrichedOrder.creationDate, summary: '', status,