Skip to content

Commit

Permalink
feat: uni history (#8373)
Browse files Browse the repository at this point in the history
* feat: uni history

* fix: tests

* chore: lint

* fix: Uniswap history for tokens
  • Loading branch information
CremaFR authored Nov 22, 2024
1 parent d7f58b6 commit cd565e3
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changeset/khaki-llamas-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ledgerhq/live-common": minor
---

swap history works for DEX swaps
Original file line number Diff line number Diff line change
@@ -1,56 +1,80 @@
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 (
swapHistory: SwapOperation[] | null | undefined,
operations: Operation[] | null | undefined,
): Promise<SwapOperation[] | null | undefined> => {
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<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);
}
} else if (item.transactionId && !existingItem.transactionId) {
uniquePendingSwapIdsMap.set(item.swapId, item);
}
}

const uniquePendingSwapIds = Array.from(uniquePendingSwapIdsMap.values());

if (uniquePendingSwapIds.length !== pendingSwapIds.length) {
log(
"error",
"swap: duplicate ids inside app.json, number",
pendingSwapIds.length - uniquePendingSwapIds.length,
);
}
const newStatusList = await getMultipleStatus(uniquePendingSwapIds);

const newStatusList = pendingSwapIds.length
? await getMultipleStatus(uniquePendingSwapIds)
: [];
newStatusList.push(...atomicSwapIds);
consolidatedSwapHistory = swapHistory.map<SwapOperation>((swap: SwapOperation) => {
const newStatus = newStatusList.find(s => s.swapId === swap.swapId);

Expand All @@ -61,10 +85,10 @@ const maybeGetUpdatedSwapHistory = async (

return swap;
});
}

if (accountNeedsUpdating) {
return consolidatedSwapHistory;
}
if (accountNeedsUpdating) {
return consolidatedSwapHistory;
}
}
};
Expand Down

0 comments on commit cd565e3

Please sign in to comment.