Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions core/src/exchanges/polymarket_us/normalizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down
9 changes: 9 additions & 0 deletions core/test/normalizers/exchange-normalizers-3.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
// -------------------------------------------------------------------------

Expand Down