// Common pattern: walk pages until you have enough
async function findCompletedTrade(externalId: string): Promise<Trade | null> {
let cursor: string | undefined;
for (let i = 0; i < 10; i++) { // bound the search
const page = await api<{ items: Trade[]; nextCursor: string | null }>(
`/merchant/trades?status=completed&externalId=${externalId}&limit=50` +
(cursor ? `&cursor=${cursor}` : ""),
);
const hit = page.items.find((t) => t.externalId === externalId);
if (hit) return hit;
if (!page.nextCursor) return null;
cursor = page.nextCursor;
}
throw new Error("trade not found in last 500 rows");
}