From bfd070d908fbf144e3f5a504d903943166222a0f Mon Sep 17 00:00:00 2001 From: Anxo Rodriguez Date: Mon, 27 Nov 2023 18:16:41 +0000 Subject: [PATCH 1/2] fix: crash unknown tokens --- .../modules/twap/hooks/useEmulatedPartOrders.ts | 4 +++- .../modules/twap/hooks/useEmulatedTwapOrders.ts | 5 ++++- .../updaters/CreatedInOrderBookOrdersUpdater.tsx | 8 +++++--- .../twap/utils/mapPartOrderToStoreOrder.ts | 15 ++++++++++++--- .../twap/utils/mapTwapOrderToStoreOrder.ts | 15 ++++++++++++--- 5 files changed, 36 insertions(+), 11 deletions(-) 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..4e075b7fe1 100644 --- a/apps/cowswap-frontend/src/modules/twap/updaters/CreatedInOrderBookOrdersUpdater.tsx +++ b/apps/cowswap-frontend/src/modules/twap/updaters/CreatedInOrderBookOrdersUpdater.tsx @@ -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((order) => !!order) }, [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..3effdcb220 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, 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, From 24d39246a5343656b402671a5bca8cbc94b79242 Mon Sep 17 00:00:00 2001 From: Anxo Rodriguez Date: Tue, 28 Nov 2023 20:44:03 +0000 Subject: [PATCH 2/2] fix: make typescript happy --- .../modules/twap/updaters/CreatedInOrderBookOrdersUpdater.tsx | 4 ++-- .../src/modules/twap/utils/mapPartOrderToStoreOrder.ts | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/apps/cowswap-frontend/src/modules/twap/updaters/CreatedInOrderBookOrdersUpdater.tsx b/apps/cowswap-frontend/src/modules/twap/updaters/CreatedInOrderBookOrdersUpdater.tsx index 4e075b7fe1..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 @@ -69,7 +69,7 @@ export function CreatedInOrderBookOrdersUpdater() { .map(({ item, parent, order }) => { return mapPartOrderToStoreOrder(item, order, isVirtualPart, parent, allTokens) }) - .filter((order) => !!order) + .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 3effdcb220..19822b9fc4 100644 --- a/apps/cowswap-frontend/src/modules/twap/utils/mapPartOrderToStoreOrder.ts +++ b/apps/cowswap-frontend/src/modules/twap/utils/mapPartOrderToStoreOrder.ts @@ -54,3 +54,7 @@ export function mapPartOrderToStoreOrder( return storeOrder } + +export function isOrder(order: Order | undefined): order is Order { + return !!order +}