diff --git a/core/src/exchanges/polymarket_us/normalizer.ts b/core/src/exchanges/polymarket_us/normalizer.ts index 4720e14f..338aebcb 100644 --- a/core/src/exchanges/polymarket_us/normalizer.ts +++ b/core/src/exchanges/polymarket_us/normalizer.ts @@ -79,7 +79,7 @@ interface RealMarket { marketSides?: RealMarketSide[]; // Fallback price source used by some gateway responses: a 2-element // array of stringified long-side prices, [longPrice, shortPrice]. - outcomePrices?: string[]; + outcomePrices?: string[] | string; // Per-market minimum price increment (e.g. 0.001). Lifted onto // `UnifiedMarket.tickSize` so price-sensitive helpers can honour the // real tick rather than falling back to the hard-coded default. @@ -168,8 +168,18 @@ function buildBinaryOutcomes( function buildOutcomes( slug: string, sides: RealMarketSide[] | undefined, - outcomePrices: string[] | undefined, + outcomePrices: string[] | string | undefined, ): MarketOutcome[] { + const prices = typeof outcomePrices === 'string' + ? (() => { + try { + const parsed: unknown = JSON.parse(outcomePrices); + return Array.isArray(parsed) ? parsed as string[] : undefined; + } catch { + return undefined; + } + })() + : outcomePrices; let longPrice: number | undefined; let shortPrice: number | undefined; let longDescription: string | undefined; @@ -188,11 +198,11 @@ function buildOutcomes( } } - if (longPrice === undefined && outcomePrices && outcomePrices.length >= 1) { - longPrice = parsePriceString(outcomePrices[0]); + if (longPrice === undefined && prices && prices.length >= 1) { + longPrice = parsePriceString(prices[0]); } - if (shortPrice === undefined && outcomePrices && outcomePrices.length >= 2) { - shortPrice = parsePriceString(outcomePrices[1]); + if (shortPrice === undefined && prices && prices.length >= 2) { + shortPrice = parsePriceString(prices[1]); } // Fill in the complementary side from the binary identity when only diff --git a/core/test/normalizers/exchange-normalizers-3.test.ts b/core/test/normalizers/exchange-normalizers-3.test.ts index 68e35ab6..a731fd89 100644 --- a/core/test/normalizers/exchange-normalizers-3.test.ts +++ b/core/test/normalizers/exchange-normalizers-3.test.ts @@ -275,6 +275,15 @@ describe('PolymarketUSNormalizer', () => { }); // ------------------------------------------------------------------------- + test('parses JSON-encoded outcomePrices from the gateway', () => { + const market = normalizer.normalizeMarket({ + slug: 'json-outcome-prices', + outcomePrices: '["0.97","0.03"]', + } as any); + expect(market.outcomes[0].price).toBeCloseTo(0.97, 3); + expect(market.outcomes[1].price).toBeCloseTo(0.03, 3); + }); + // normalizeMarket - missing sides => price defaults to 0 // -------------------------------------------------------------------------