diff --git a/.changeset/khaki-llamas-flow.md b/.changeset/khaki-llamas-flow.md new file mode 100644 index 000000000000..2d1fc36ea9a7 --- /dev/null +++ b/.changeset/khaki-llamas-flow.md @@ -0,0 +1,5 @@ +--- +"@ledgerhq/live-common": minor +--- + +swap history works for DEX swaps diff --git a/libs/ledger-live-common/src/exchange/swap/updateAccountSwapStatus.ts b/libs/ledger-live-common/src/exchange/swap/updateAccountSwapStatus.ts index e5c86be4abd4..3db92c5723ae 100644 --- a/libs/ledger-live-common/src/exchange/swap/updateAccountSwapStatus.ts +++ b/libs/ledger-live-common/src/exchange/swap/updateAccountSwapStatus.ts @@ -1,7 +1,7 @@ import { isSwapOperationPending } from "./"; import { getMultipleStatus } from "./getStatus"; import type { SubAccount, Account, SwapOperation, Operation } from "@ledgerhq/types-live"; -import type { SwapStatusRequest, UpdateAccountSwapStatus } from "./types"; +import type { SwapStatus, SwapStatusRequest, UpdateAccountSwapStatus } from "./types"; import { log } from "@ledgerhq/logs"; const maybeGetUpdatedSwapHistory = async ( @@ -9,40 +9,60 @@ const maybeGetUpdatedSwapHistory = async ( operations: Operation[] | null | undefined, ): Promise => { const pendingSwapIds: SwapStatusRequest[] = []; + const atomicSwapIds: SwapStatus[] = []; let accountNeedsUpdating = false; let consolidatedSwapHistory: SwapOperation[] = []; if (swapHistory) { - for (const { provider, swapId, status, operationId } of swapHistory) { + for (const swap of swapHistory) { + const { provider, swapId, status, operationId } = swap; + const updatedSwap: SwapOperation = { ...swap }; + if (isSwapOperationPending(status)) { - const transactionId = - provider === "thorswap" - ? operations?.find(o => o.id.includes(operationId))?.hash - : undefined; - pendingSwapIds.push({ - provider, - swapId, - transactionId, - ...(provider === "thorswap" && { operationId }), // to be removed after Thorswap is fully migrated - }); + // if swapId is in operationId, then we can get the status from the operation + // it means DEX swap like Uniswap + if (operationId && swapId && operationId.includes(swapId)) { + const operation = operations?.find(o => o.id.includes(operationId)); + if (operation) { + let newStatus; + if (operation.blockHeight) { + newStatus = operation.hasFailed ? "refunded" : "finished"; + } else { + newStatus = "pending"; + } + if (newStatus !== swap.status) { + accountNeedsUpdating = true; + updatedSwap.status = newStatus; + atomicSwapIds.push({ provider, swapId, status: newStatus }); + } + } + } else { + // Collect all others swaps that need status update via getMultipleStatus + const transactionId = + provider === "thorswap" + ? operations?.find(o => o.id.includes(operationId))?.hash + : undefined; + pendingSwapIds.push({ + provider, + swapId, + transactionId, + ...(provider === "thorswap" && { operationId }), + }); + } } } - - if (pendingSwapIds.length) { + if (pendingSwapIds.length || atomicSwapIds.length) { const uniquePendingSwapIdsMap = new Map(); for (const item of pendingSwapIds) { const existingItem = uniquePendingSwapIdsMap.get(item.swapId); - if (!existingItem) { uniquePendingSwapIdsMap.set(item.swapId, item); - } else { - if (item.transactionId && !existingItem.transactionId) { - uniquePendingSwapIdsMap.set(item.swapId, item); - } + } else if (item.transactionId && !existingItem.transactionId) { + uniquePendingSwapIdsMap.set(item.swapId, item); } } - const uniquePendingSwapIds = Array.from(uniquePendingSwapIdsMap.values()); + if (uniquePendingSwapIds.length !== pendingSwapIds.length) { log( "error", @@ -50,7 +70,11 @@ const maybeGetUpdatedSwapHistory = async ( pendingSwapIds.length - uniquePendingSwapIds.length, ); } - const newStatusList = await getMultipleStatus(uniquePendingSwapIds); + + const newStatusList = pendingSwapIds.length + ? await getMultipleStatus(uniquePendingSwapIds) + : []; + newStatusList.push(...atomicSwapIds); consolidatedSwapHistory = swapHistory.map((swap: SwapOperation) => { const newStatus = newStatusList.find(s => s.swapId === swap.swapId); @@ -61,10 +85,10 @@ const maybeGetUpdatedSwapHistory = async ( return swap; }); + } - if (accountNeedsUpdating) { - return consolidatedSwapHistory; - } + if (accountNeedsUpdating) { + return consolidatedSwapHistory; } } };