Skip to content

Commit 1a7314b

Browse files
fix(predict): use v2 CLOB endpoint for market info
Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent e62b321 commit 1a7314b

2 files changed

Lines changed: 67 additions & 13 deletions

File tree

app/components/UI/Predict/providers/polymarket/utils.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,47 @@ describe('polymarket utils', () => {
524524
);
525525
});
526526

527+
it('uses v2 CLOB endpoint for buy preview order book and market info', async () => {
528+
const v2ClobBaseUrl = 'https://clob-v2.example.com';
529+
mockFetch
530+
.mockResolvedValueOnce({
531+
ok: true,
532+
json: jest.fn().mockResolvedValue(orderBook),
533+
})
534+
.mockResolvedValueOnce({
535+
ok: true,
536+
json: jest.fn().mockResolvedValue({
537+
fd: {
538+
r: 0.02,
539+
e: 1,
540+
to: true,
541+
},
542+
}),
543+
});
544+
545+
await previewOrder({
546+
marketId: 'market-1',
547+
outcomeId:
548+
'0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
549+
outcomeTokenId: 'token-1',
550+
side: Side.BUY,
551+
size: 10,
552+
isV2: true,
553+
clobBaseUrl: v2ClobBaseUrl,
554+
});
555+
556+
expect(mockFetch).toHaveBeenNthCalledWith(
557+
1,
558+
`${v2ClobBaseUrl}/book?token_id=token-1`,
559+
{ method: 'GET' },
560+
);
561+
expect(mockFetch).toHaveBeenNthCalledWith(
562+
2,
563+
`${v2ClobBaseUrl}/clob-markets/0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`,
564+
{ method: 'GET' },
565+
);
566+
});
567+
527568
it('does not fetch CLOB market info for sell previews', async () => {
528569
mockFetch.mockResolvedValue({
529570
ok: true,

app/components/UI/Predict/providers/polymarket/utils.ts

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -372,21 +372,24 @@ export const priceValid = (price: number, tickSize: TickSize): boolean =>
372372

373373
export const getClobMarketInfo = async ({
374374
conditionId,
375+
clobVersion = 'v1',
376+
clobBaseUrl,
375377
}: {
376378
conditionId: string;
379+
clobVersion?: 'v1' | 'v2';
380+
clobBaseUrl?: string;
377381
}): Promise<ClobMarketInfo> => {
378-
const cachedMarketInfo = clobMarketInfoCache.get(conditionId);
382+
const clobEndpoint = getClobEndpoint({ clobVersion, clobBaseUrl });
383+
const cacheKey = `${clobEndpoint}:${conditionId}`;
384+
const cachedMarketInfo = clobMarketInfoCache.get(cacheKey);
379385

380386
if (cachedMarketInfo) {
381387
return cachedMarketInfo;
382388
}
383389

384-
const response = await fetch(
385-
`${getClobEndpoint()}/clob-markets/${conditionId}`,
386-
{
387-
method: 'GET',
388-
},
389-
);
390+
const response = await fetch(`${clobEndpoint}/clob-markets/${conditionId}`, {
391+
method: 'GET',
392+
});
390393

391394
if (!response.ok) {
392395
throw new Error('Failed to get CLOB market info');
@@ -398,25 +401,31 @@ export const getClobMarketInfo = async ({
398401
throw new Error('Invalid CLOB market info response');
399402
}
400403

401-
clobMarketInfoCache.set(conditionId, marketInfo);
402-
reportedClobMarketInfoFailures.delete(conditionId);
404+
clobMarketInfoCache.set(cacheKey, marketInfo);
405+
reportedClobMarketInfoFailures.delete(cacheKey);
403406
return marketInfo;
404407
};
405408

406409
export const getClobMarketInfoSafe = async ({
407410
conditionId,
411+
clobVersion = 'v1',
412+
clobBaseUrl,
408413
}: {
409414
conditionId: string;
415+
clobVersion?: 'v1' | 'v2';
416+
clobBaseUrl?: string;
410417
}): Promise<ClobMarketInfo | undefined> => {
418+
const failureKey = `${getClobEndpoint({ clobVersion, clobBaseUrl })}:${conditionId}`;
419+
411420
try {
412-
return await getClobMarketInfo({ conditionId });
421+
return await getClobMarketInfo({ conditionId, clobVersion, clobBaseUrl });
413422
} catch (error) {
414-
if (!reportedClobMarketInfoFailures.has(conditionId)) {
423+
if (!reportedClobMarketInfoFailures.has(failureKey)) {
415424
Logger.error(
416425
ensureClobMarketInfoError(error),
417426
getClobMarketInfoFailureContext(conditionId),
418427
);
419-
reportedClobMarketInfoFailures.add(conditionId);
428+
reportedClobMarketInfoFailures.add(failureKey);
420429
}
421430

422431
return undefined;
@@ -2065,7 +2074,11 @@ export const previewOrder = async (
20652074
}),
20662075
Promise.resolve('0'),
20672076
side === Side.BUY
2068-
? getClobMarketInfoSafe({ conditionId: outcomeId })
2077+
? getClobMarketInfoSafe({
2078+
conditionId: outcomeId,
2079+
clobVersion: isV2 ? 'v2' : 'v1',
2080+
clobBaseUrl: isV2 ? clobBaseUrl : undefined,
2081+
})
20692082
: Promise.resolve(undefined),
20702083
]);
20712084
if (!book) {

0 commit comments

Comments
 (0)