Skip to content

Commit f8477e6

Browse files
authored
fix(core): batch Polymarket position condition_ids gamma lookup (#981)
Chunk Gamma /markets enrichment in fetchPositions so wallets with 50+ historical positions no longer hit Cloudflare 403 HTML
1 parent 5cb0829 commit f8477e6

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

core/src/exchanges/polymarket/fetcher.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ export interface PolymarketRawPosition {
112112

113113
const GAMMA_MARKETS_URL = process.env.POLYMARKET_GAMMA_MARKETS_URL || 'https://gamma-api.polymarket.com/markets';
114114

115+
// Cloudflare blocks large single-shot Gamma lookups (~50 condition_ids / ~3.5KB query).
116+
const GAMMA_CONDITION_IDS_BATCH_SIZE = 40;
117+
115118
export class PolymarketFetcher implements IExchangeFetcher<PolymarketRawEvent, PolymarketRawEvent> {
116119
private readonly ctx: FetcherContext;
117120
private readonly http: AxiosInstance;
@@ -327,14 +330,17 @@ export class PolymarketFetcher implements IExchangeFetcher<PolymarketRawEvent, P
327330
}
328331

329332
private async resolveConditionIds(conditionIds: string[]): Promise<Map<string, string>> {
330-
const response = await this.http.get(GAMMA_MARKETS_URL, {
331-
params: { condition_ids: conditionIds.join(',') },
332-
});
333-
const markets: any[] = Array.isArray(response.data) ? response.data : [];
334333
const result = new Map<string, string>();
335-
for (const market of markets) {
336-
if (market.conditionId && market.id) {
337-
result.set(market.conditionId, String(market.id));
334+
for (let offset = 0; offset < conditionIds.length; offset += GAMMA_CONDITION_IDS_BATCH_SIZE) {
335+
const batch = conditionIds.slice(offset, offset + GAMMA_CONDITION_IDS_BATCH_SIZE);
336+
const response = await this.http.get(GAMMA_MARKETS_URL, {
337+
params: { condition_ids: batch.join(',') },
338+
});
339+
const markets: any[] = Array.isArray(response.data) ? response.data : [];
340+
for (const market of markets) {
341+
if (market.conditionId && market.id) {
342+
result.set(market.conditionId, String(market.id));
343+
}
338344
}
339345
}
340346
return result;

0 commit comments

Comments
 (0)