MCPcopy Create free account
hub / github.com/AmazingAng/PolyWorld / fetchEventsFromAPI

Function fetchEventsFromAPI

src/lib/polymarket.ts:7–77  ·  view source on GitHub ↗
(locale?: string)

Source from the content-addressed store, hash-verified

5const API_BASE = "https://gamma-api.polymarket.com";
6
7export async function fetchEventsFromAPI(locale?: string): Promise<PolymarketEvent[]> {
8 const events: PolymarketEvent[] = [];
9 const seen = new Set<string>();
10 const BATCH = 100;
11 const CONCURRENCY = 5;
12 const localeSuffix = locale ? `&locale=${locale}` : "";
13
14 // Fetch active events
15 let offset = 0;
16 let done = false;
17
18 while (!done) {
19 const offsets = Array.from({ length: CONCURRENCY }, (_, i) => offset + i * BATCH);
20 const results = await Promise.all(
21 offsets.map(async (off) => {
22 const url = `${API_BASE}/events?active=true&closed=false&limit=${BATCH}&offset=${off}&order=volume24hr&ascending=false${localeSuffix}`;
23 try {
24 const res = await fetchWithRetry(url, { next: { revalidate: 30 }, signal: AbortSignal.timeout(10_000) } as RequestInit, 2);
25 if (!res.ok) return [];
26 return res.json();
27 } catch {
28 return [];
29 }
30 })
31 );
32
33 let pageHadData = false;
34 for (const page of results) {
35 const arr = Array.isArray(page)
36 ? page
37 : page?.data || page?.events || [];
38 if (arr.length === 0) {
39 done = true;
40 } else {
41 pageHadData = true;
42 for (const e of arr) {
43 if (e?.id && !seen.has(e.id)) {
44 seen.add(e.id);
45 events.push(e);
46 }
47 }
48 }
49 }
50
51 if (!pageHadData) break;
52 offset += CONCURRENCY * BATCH;
53 }
54
55 // Also fetch recently closed events (top 100 by volume) to update status
56 if (!locale) {
57 try {
58 const closedUrl = `${API_BASE}/events?closed=true&limit=200&offset=0&order=volume24hr&ascending=false`;
59 const res = await fetch(closedUrl, { next: { revalidate: 60 }, signal: AbortSignal.timeout(15_000) });
60 if (res.ok) {
61 const data = await res.json();
62 const arr = Array.isArray(data) ? data : data?.data || data?.events || [];
63 for (const e of arr) {
64 if (e?.id && !seen.has(e.id)) {

Callers 2

runSyncFunction · 0.90
fetchZhTranslationsFunction · 0.85

Calls 1

fetchWithRetryFunction · 0.90

Tested by

no test coverage detected