Skip to content

Commit

Permalink
fixed: thorswap get status send transactionId instead of LL ids (#8228)
Browse files Browse the repository at this point in the history
  • Loading branch information
CremaFR authored Oct 30, 2024
1 parent 22f700c commit 56fa61d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/ninety-moles-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ledgerhq/live-common": minor
---

fixed: thorswap get status send transactionId instead of LL ids
1 change: 1 addition & 0 deletions libs/ledger-live-common/src/exchange/swap/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<SwapOperation[] | null | undefined> => {
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<string, SwapStatusRequest>();
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(
Expand Down Expand Up @@ -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<SubAccount> => {
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 {
Expand Down

0 comments on commit 56fa61d

Please sign in to comment.