Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions src/app/api/fee-compare/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1115,31 +1115,40 @@
const INCREASE_ACTIONS = new Set(["TradePosSizeIncrease"]);
const CLOSE_ACTIONS = new Set(["TradeClosedMarket", "TradeClosedTP", "TradeClosedSL", "TradeClosedLIQ"]);

const byId = new Map<number, { open?: GainsApiTrade; close?: GainsApiTrade }>();
const byId = new Map<number, { open?: GainsApiTrade; close?: GainsApiTrade; lastIncrease?: GainsApiTrade }>();
for (const t of trades) {
if (!byId.has(t.id)) byId.set(t.id, {});
const e = byId.get(t.id)!;
if (OPEN_ACTIONS.has(t.action)) e.open = t;
else if (INCREASE_ACTIONS.has(t.action) && e.open) {
e.open = { ...e.open, size: t.size, leverage: t.leverage };
else if (INCREASE_ACTIONS.has(t.action)) {
if (e.open) e.open = { ...e.open, size: t.size, leverage: t.leverage };
// Track increases for positions opened before the window (no open event in data)
else if (!e.lastIncrease || new Date(t.date).getTime() < new Date(e.lastIncrease.date).getTime()) {
e.lastIncrease = t;
}
}
else if (CLOSE_ACTIONS.has(t.action)) e.close = t;
}

const now = Date.now();
const slices: PositionSlice[] = [];
for (const { open, close } of byId.values()) {
if (!open) continue;
const openMs = new Date(open.date).getTime();
if (openMs < cutoffMs) continue;
// Still-open positions use now as close time (same as reconstructHlPositions)
for (const { open, close, lastIncrease } of byId.values()) {
// Use open event if available; fall back to earliest increase in the window
// for positions opened before the fetch window (open event not in data).
const anchor = open ?? lastIncrease;
if (!anchor) continue;
const rawOpenMs = new Date(anchor.date).getTime();
const closeMs = close ? new Date(close.date).getTime() : now;
// Skip positions that closed before the analysis window
if (closeMs < cutoffMs) continue;
// Cap openMs to the window start so long-running positions aren't missed
const openMs = Math.max(rawOpenMs, cutoffMs);
slices.push({
coin: open.pair.split("/")[0],
notionalUsd: open.size * open.leverage,
coin: anchor.pair.split("/")[0],
notionalUsd: anchor.size * anchor.leverage,
openMs,
closeMs,
isLong: open.buy !== false,
isLong: anchor.buy !== false,
});
}

Expand Down Expand Up @@ -1212,7 +1221,7 @@
}

// Estimate GMX borrow fees for a set of position slices.
function estimateGmxBorrowFees(

Check warning on line 1224 in src/app/api/fee-compare/route.ts

View workflow job for this annotation

GitHub Actions / check

'estimateGmxBorrowFees' is defined but never used
positions: PositionSlice[],
borrowPerSecPerCoin: Record<string, number>
): number {
Expand Down Expand Up @@ -1520,7 +1529,7 @@
}));
} else if (fetchEvmWallet && slug === "gains") {
const CLOSE_ACTIONS = new Set(["TradeClosedMarket", "TradeClosedTP", "TradeClosedSL", "TradeClosedLIQ"]);
const usdcTrades = gainsTradesData.filter((t) => t.collateralIndex === 3);

Check warning on line 1532 in src/app/api/fee-compare/route.ts

View workflow job for this annotation

GitHub Actions / check

'CLOSE_ACTIONS' is assigned a value but never used
const otherSlug = slug === venueA ? venueB : venueA;
const otherRate = slug === venueA ? rateB : rateA;
let feesUsdc = 0;
Expand Down
Loading