Skip to content
Merged
Show file tree
Hide file tree
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
29 changes: 6 additions & 23 deletions src/app/api/fee-compare/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { clientKey, rateLimit, tooManyRequests } from "@/lib/rate-limit";
import { keccak256 } from "js-sha3";

export const runtime = "nodejs";
export const maxDuration = 30;
export const maxDuration = 50;

const HL_API = "https://api.hyperliquid.xyz/info";
const GAINS_VARS_URL = "https://backend-arbitrum.gains.trade/trading-variables";
Expand Down Expand Up @@ -499,37 +499,20 @@ async function fetchDydxFills(dydxAddress: string, cutoffMs: number): Promise<Dy
page++;
}

// fetch funding from perpetualPositions (netFunding = settled + unsettled)
let fundingUsd = 0;
try {
const pfRes = await fetch(
`${DYDX_INDEXER}/v4/perpetualPositions?address=${encodeURIComponent(dydxAddress)}&subaccountNumber=0&limit=100`,
{ signal: AbortSignal.timeout(8000) }
);
if (pfRes.ok) {
const pfBody = (await pfRes.json()) as {
positions?: Array<{ netFunding?: string; closedAt?: string | null }>;
};
for (const p of pfBody.positions ?? []) {
fundingUsd += parseFloat(p.netFunding ?? "0");
}
}
} catch { /* funding is optional */ }

// Only count taker fills — maker fees are rebates and distort the comparison
const takers = allFills.filter((f) => (f.liquidity ?? "").toUpperCase() !== "MAKER");
let feesUsdc = 0;
let notionalUsd = 0;
for (const f of takers) {
feesUsdc += parseFloat(f.fee);
notionalUsd += parseFloat(f.price) * parseFloat(f.size);
}
const netCostUsdc = feesUsdc - fundingUsd;

return {
fills: allFills.length,
fills: takers.length,
feesUsdc,
fundingUsd,
netCostUsdc,
fundingUsd: 0,
netCostUsdc: feesUsdc,
notionalUsd,
avgFeeRateBps: notionalUsd > 0 ? (feesUsdc / notionalUsd) * 10000 : 0,
};
Expand All @@ -540,7 +523,7 @@ async function fetchGainsTrades(wallet: string, cutoffMs: number): Promise<Gains
const all: GainsApiTrade[] = [];
let cursor: number | undefined;
let pages = 0;
const MAX_PAGES = 10;
const MAX_PAGES = 6;

do {
const params = new URLSearchParams({ chainId: "42161", limit: "100", startDate });
Expand Down
3 changes: 1 addition & 2 deletions src/components/fee-compare-client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1257,8 +1257,7 @@ function Footnote({ venueA, venueB }: { venueA: VenueResult; venueB: VenueResult
)}
{hasDydx && (
<>
dYdX v4: public indexer with full pagination and date filter. Funding from{" "}
<code className="font-mono text-[10px]">perpetualPositions.netFunding</code>. Address must be{" "}
dYdX v4: public indexer with full pagination and date filter. Taker fills only. Funding excluded (not date-scopable from public API). Address must be{" "}
<code className="font-mono text-[10px]">dydx1...</code> Cosmos format.{" "}
</>
)}
Expand Down
Loading