From 56fa61d0b45c7d87ec762ed1c97f9b70ab0e4fce Mon Sep 17 00:00:00 2001 From: Thomas Brillard Date: Wed, 30 Oct 2024 10:12:22 +0100 Subject: [PATCH] fixed: thorswap get status send transactionId instead of LL ids (#8228) --- .changeset/ninety-moles-listen.md | 5 +++ .../src/exchange/swap/types.ts | 1 + .../exchange/swap/updateAccountSwapStatus.ts | 35 ++++++++++++++++--- 3 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 .changeset/ninety-moles-listen.md diff --git a/.changeset/ninety-moles-listen.md b/.changeset/ninety-moles-listen.md new file mode 100644 index 000000000000..97a74a88fe6e --- /dev/null +++ b/.changeset/ninety-moles-listen.md @@ -0,0 +1,5 @@ +--- +"@ledgerhq/live-common": minor +--- + +fixed: thorswap get status send transactionId instead of LL ids diff --git a/libs/ledger-live-common/src/exchange/swap/types.ts b/libs/ledger-live-common/src/exchange/swap/types.ts index 88ee733bdfee..dd051baed23e 100644 --- a/libs/ledger-live-common/src/exchange/swap/types.ts +++ b/libs/ledger-live-common/src/exchange/swap/types.ts @@ -173,6 +173,7 @@ type ValidSwapStatus = "pending" | "onhold" | "expired" | "finished" | "refunded export type SwapStatusRequest = { provider: string; swapId: string; + transactionId?: string; operationId?: string; }; export type SwapStatus = { diff --git a/libs/ledger-live-common/src/exchange/swap/updateAccountSwapStatus.ts b/libs/ledger-live-common/src/exchange/swap/updateAccountSwapStatus.ts index ceece6e8e36e..e5c86be4abd4 100644 --- a/libs/ledger-live-common/src/exchange/swap/updateAccountSwapStatus.ts +++ b/libs/ledger-live-common/src/exchange/swap/updateAccountSwapStatus.ts @@ -1,28 +1,47 @@ import { isSwapOperationPending } from "./"; import { getMultipleStatus } from "./getStatus"; -import type { SubAccount, Account, SwapOperation } from "@ledgerhq/types-live"; +import type { SubAccount, Account, SwapOperation, Operation } from "@ledgerhq/types-live"; import type { SwapStatusRequest, UpdateAccountSwapStatus } from "./types"; import { log } from "@ledgerhq/logs"; const maybeGetUpdatedSwapHistory = async ( swapHistory: SwapOperation[] | null | undefined, + operations: Operation[] | null | undefined, ): Promise => { const pendingSwapIds: SwapStatusRequest[] = []; let accountNeedsUpdating = false; let consolidatedSwapHistory: SwapOperation[] = []; + if (swapHistory) { for (const { provider, swapId, status, operationId } of swapHistory) { if (isSwapOperationPending(status)) { + const transactionId = + provider === "thorswap" + ? operations?.find(o => o.id.includes(operationId))?.hash + : undefined; pendingSwapIds.push({ provider, swapId, - ...(provider === "thorswap" && { operationId }), + transactionId, + ...(provider === "thorswap" && { operationId }), // to be removed after Thorswap is fully migrated }); } } if (pendingSwapIds.length) { - const uniquePendingSwapIdsMap = new Map(pendingSwapIds.map(item => [item.swapId, item])); + 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); + } + } + } + const uniquePendingSwapIds = Array.from(uniquePendingSwapIdsMap.values()); if (uniquePendingSwapIds.length !== pendingSwapIds.length) { log( @@ -51,14 +70,20 @@ const maybeGetUpdatedSwapHistory = async ( }; const updateAccountSwapStatus: UpdateAccountSwapStatus = async (account: Account) => { - const swapHistoryUpdated = await maybeGetUpdatedSwapHistory(account.swapHistory); + const swapHistoryUpdated = await maybeGetUpdatedSwapHistory( + account.swapHistory, + account.operations, + ); let subAccountSwapHistoryUpdated = false; let subAccounts: SubAccount[] = []; if (account.type === "Account" && account.subAccounts?.length) { subAccounts = await Promise.all( account.subAccounts.map(async (subAccount: SubAccount): Promise => { - const updatedSwapHistory = await maybeGetUpdatedSwapHistory(subAccount.swapHistory); + const updatedSwapHistory = await maybeGetUpdatedSwapHistory( + subAccount.swapHistory, + subAccount.operations, + ); //As soon as we get one update, we will need to update the parent account if (updatedSwapHistory) subAccountSwapHistoryUpdated = true; return {