Skip to content

markets/src/polymarket

Mostlyright TypeScript SDK


Mostlyright TypeScript SDK / markets/src/polymarket

Defined in: packages-ts/markets/src/polymarket/errors.ts:57

Description exceeded the 16 KB cap. Direct subclass of MostlyRightError (rather than PolymarketEventError) because TS prevents narrowing a static readonly literal type in a subclass.

  • MostlyRightError

new PayloadTooLargeError(message): PayloadTooLargeError

Defined in: packages-ts/markets/src/polymarket/errors.ts:58

string

PayloadTooLargeError

MostlyRightError.constructor

readonly static defaultErrorCode: "POLYMARKET_PAYLOAD_TOO_LARGE" = "POLYMARKET_PAYLOAD_TOO_LARGE"

Defined in: packages-ts/markets/src/polymarket/errors.ts:62

Subclass override — the stable string enum surfaced via errorCode.

MostlyRightError.defaultErrorCode


Defined in: packages-ts/markets/src/polymarket/errors.ts:7

Event payload is malformed (bad event id, oversized description, bad URL).

  • MostlyRightError

new PolymarketEventError(message): PolymarketEventError

Defined in: packages-ts/markets/src/polymarket/errors.ts:8

string

PolymarketEventError

MostlyRightError.constructor

readonly static defaultErrorCode: "POLYMARKET_EVENT_INVALID" = "POLYMARKET_EVENT_INVALID"

Defined in: packages-ts/markets/src/polymarket/errors.ts:12

Subclass override — the stable string enum surfaced via errorCode.

MostlyRightError.defaultErrorCode


Defined in: packages-ts/markets/src/polymarket/errors.ts:16

Settlement engine couldn’t resolve an event to a value.

  • MostlyRightError

new PolymarketSettlementError(message): PolymarketSettlementError

Defined in: packages-ts/markets/src/polymarket/errors.ts:17

string

PolymarketSettlementError

MostlyRightError.constructor

readonly static defaultErrorCode: "POLYMARKET_SETTLEMENT_FAILED" = "POLYMARKET_SETTLEMENT_FAILED"

Defined in: packages-ts/markets/src/polymarket/errors.ts:21

Subclass override — the stable string enum surfaced via errorCode.

MostlyRightError.defaultErrorCode


Defined in: packages-ts/markets/src/polymarket-strike.ts:17

Raised on a strike phrase that cannot be parsed.

  • Error

new PolymarketStrikeError(message): PolymarketStrikeError

Defined in: packages-ts/markets/src/polymarket-strike.ts:18

string

PolymarketStrikeError

Error.constructor


Defined in: packages-ts/markets/src/polymarket/errors.ts:32

Settlement attempted before the resolution source’s publication delay has elapsed. Carries waitHours so the caller can schedule a retry.

Codex iter-1 P2: overrides payload() so toDict() (and any MCP serializer downstream) includes the structured retry metadata. The fields are otherwise only readable via the live JS error object.

  • MostlyRightError

new TooEarlyToSettleError(message, opts): TooEarlyToSettleError

Defined in: packages-ts/markets/src/polymarket/errors.ts:35

string

string

number

TooEarlyToSettleError

MostlyRightError.constructor

readonly static defaultErrorCode: "POLYMARKET_TOO_EARLY" = "POLYMARKET_TOO_EARLY"

Defined in: packages-ts/markets/src/polymarket/errors.ts:41

Subclass override — the stable string enum surfaced via errorCode.

MostlyRightError.defaultErrorCode

readonly resolutionSourceType: string

Defined in: packages-ts/markets/src/polymarket/errors.ts:34

readonly waitHours: number

Defined in: packages-ts/markets/src/polymarket/errors.ts:33

Defined in: packages-ts/markets/src/polymarket/client.ts:32

readonly optional fetchFn: (input, init?) => Promise<Response>(input, init?) => Promise<Response>

Defined in: packages-ts/markets/src/polymarket/client.ts:38

Override fetch (for tests). Defaults to global fetch.

MDN Reference

URL | RequestInfo

RequestInit

Promise<Response>

MDN Reference

string | URL | Request

RequestInit

Promise<Response>

readonly optional signal: AbortSignal

Defined in: packages-ts/markets/src/polymarket/client.ts:36

AbortSignal for the whole paginator.

readonly optional sleepBetweenMs: number

Defined in: packages-ts/markets/src/polymarket/client.ts:34

Politeness sleep between requests, in ms. Default 200 (0.2 s). Pass 0 to skip.


Defined in: packages-ts/markets/src/polymarket-strike.ts:26

readonly optional hiC: number

Defined in: packages-ts/markets/src/polymarket-strike.ts:32

readonly kind: PolymarketStrikeKind

Defined in: packages-ts/markets/src/polymarket-strike.ts:27

readonly optional loC: number

Defined in: packages-ts/markets/src/polymarket-strike.ts:31

Range bounds in Celsius (between).

readonly optional strikeC: number

Defined in: packages-ts/markets/src/polymarket-strike.ts:29

Strike in Celsius (above/below).


Defined in: packages-ts/markets/src/polymarket/discover.ts:15

readonly optional fetchFn: (input, init?) => Promise<Response>(input, init?) => Promise<Response>

Defined in: packages-ts/markets/src/polymarket/client.ts:38

Override fetch (for tests). Defaults to global fetch.

MDN Reference

URL | RequestInfo

RequestInit

Promise<Response>

MDN Reference

string | URL | Request

RequestInit

Promise<Response>

FetchEventsOptions.fetchFn

optional onSkip: (info) => void

Defined in: packages-ts/markets/src/polymarket/discover.ts:20

Sink for dropped events. Tests pass a recorder; production can pass console.info or omit. Receives {slug, reason} per skipped event.

string

null | string

void

readonly optional signal: AbortSignal

Defined in: packages-ts/markets/src/polymarket/client.ts:36

AbortSignal for the whole paginator.

FetchEventsOptions.signal

readonly optional sleepBetweenMs: number

Defined in: packages-ts/markets/src/polymarket/client.ts:34

Politeness sleep between requests, in ms. Default 200 (0.2 s). Pass 0 to skip.

FetchEventsOptions.sleepBetweenMs


Defined in: packages-ts/markets/src/polymarket/types.ts:88

Discovery row shape — one per active weather event.

readonly city: null | string

Defined in: packages-ts/markets/src/polymarket/types.ts:92

readonly endTime: null | string

Defined in: packages-ts/markets/src/polymarket/types.ts:95

readonly eventId: null | string

Defined in: packages-ts/markets/src/polymarket/types.ts:89

readonly icao: null | string

Defined in: packages-ts/markets/src/polymarket/types.ts:93

readonly measure: null | "default" | "high" | "low"

Defined in: packages-ts/markets/src/polymarket/types.ts:94

readonly resolutionSourceType: null | "wunderground" | "noaa_wrh" | "hko" | "cwa" | "other"

Defined in: packages-ts/markets/src/polymarket/types.ts:96

readonly slug: null | string

Defined in: packages-ts/markets/src/polymarket/types.ts:90

readonly title: null | string

Defined in: packages-ts/markets/src/polymarket/types.ts:91


Defined in: packages-ts/markets/src/polymarket/client.ts:19

Raw Gamma event payload — narrow shape we depend on.

[key: string]: unknown

optional active: boolean

Defined in: packages-ts/markets/src/polymarket/client.ts:25

optional archived: boolean

Defined in: packages-ts/markets/src/polymarket/client.ts:27

optional closed: boolean

Defined in: packages-ts/markets/src/polymarket/client.ts:26

optional description: string

Defined in: packages-ts/markets/src/polymarket/client.ts:23

optional endDate: string

Defined in: packages-ts/markets/src/polymarket/client.ts:24

optional id: string

Defined in: packages-ts/markets/src/polymarket/client.ts:20

optional slug: string

Defined in: packages-ts/markets/src/polymarket/client.ts:21

optional tags: (string | { label: string; slug: string; })[]

Defined in: packages-ts/markets/src/polymarket/client.ts:28

optional title: string

Defined in: packages-ts/markets/src/polymarket/client.ts:22


Defined in: packages-ts/markets/src/polymarket/settle.ts:50

Settlement options.

readonly optional description: string

Defined in: packages-ts/markets/src/polymarket/types.ts:131

Optional description override (live discovery normally supplies this).

PolymarketSettleOptions.description

readonly event: PolymarketEventRaw

Defined in: packages-ts/markets/src/polymarket/settle.ts:57

The raw Gamma event payload. Required because the settle engine reads slug (for the resolution date), description (for the resolution source), and title/slug/name (for the measure). Callers can fetch via fetchEventById if they only have an id.

readonly optional loader: ObservationLoader

Defined in: packages-ts/markets/src/polymarket/settle.ts:62

Loader that returns observation rows for [fromDate, toDate]. Defaults to an in-memory empty loader so callers MUST wire this for production.

readonly optional now: Date

Defined in: packages-ts/markets/src/polymarket/types.ts:133

Reference “now” for the finalization-delay check. Defaults to new Date().

PolymarketSettleOptions.now

readonly optional polymarketPublishedValue: number

Defined in: packages-ts/markets/src/polymarket/types.ts:139

Polymarket’s published settlement value, if known. The comparison uses whichever unit unit is set to. ±1°F (or ±0.6°C) diff emits an alert; values outside that band don’t throw.

PolymarketSettleOptions.polymarketPublishedValue

readonly optional unit: SettlementUnit

Defined in: packages-ts/markets/src/polymarket/types.ts:146

Resolved-value unit. Defaults to the station’s native unit: °F for the 20 US Kalshi cities; °C for international stations (matches Polymarket’s published-bucket convention per .planning/research/INGEST-PLANNER-RESEARCH.md). Codex iter-3 P2.

PolymarketSettleOptions.unit


Defined in: packages-ts/markets/src/polymarket/types.ts:108

Settlement result shape.

readonly dataQualityAlert: null | string

Defined in: packages-ts/markets/src/polymarket/types.ts:125

readonly eventId: string

Defined in: packages-ts/markets/src/polymarket/types.ts:109

readonly icao: string

Defined in: packages-ts/markets/src/polymarket/types.ts:111

readonly measure: "default" | "high" | "low"

Defined in: packages-ts/markets/src/polymarket/types.ts:112

readonly resolutionSourceType: "wunderground" | "noaa_wrh" | "hko" | "cwa" | "other"

Defined in: packages-ts/markets/src/polymarket/types.ts:124

readonly resolvedValue: number

Defined in: packages-ts/markets/src/polymarket/types.ts:119

Resolved temperature in the unit the caller asked for (the unit option; defaults to the station’s native unit — F for US-registry stations, C for international). Convenience pointer to resolvedValueF or resolvedValueC.

readonly resolvedValueC: number

Defined in: packages-ts/markets/src/polymarket/types.ts:120

readonly resolvedValueF: number

Defined in: packages-ts/markets/src/polymarket/types.ts:121

readonly settlementDate: string

Defined in: packages-ts/markets/src/polymarket/types.ts:110

readonly unit: SettlementUnit

Defined in: packages-ts/markets/src/polymarket/types.ts:123

Which unit resolvedValue carries.


Defined in: packages-ts/markets/src/polymarket/types.ts:129

Settlement options.

readonly optional description: string

Defined in: packages-ts/markets/src/polymarket/types.ts:131

Optional description override (live discovery normally supplies this).

readonly optional now: Date

Defined in: packages-ts/markets/src/polymarket/types.ts:133

Reference “now” for the finalization-delay check. Defaults to new Date().

readonly optional polymarketPublishedValue: number

Defined in: packages-ts/markets/src/polymarket/types.ts:139

Polymarket’s published settlement value, if known. The comparison uses whichever unit unit is set to. ±1°F (or ±0.6°C) diff emits an alert; values outside that band don’t throw.

readonly optional unit: SettlementUnit

Defined in: packages-ts/markets/src/polymarket/types.ts:146

Resolved-value unit. Defaults to the station’s native unit: °F for the 20 US Kalshi cities; °C for international stations (matches Polymarket’s published-bucket convention per .planning/research/INGEST-PLANNER-RESEARCH.md). Codex iter-3 P2.

ObservationLoader: (args) => Promise<ReadonlyArray<InternationalRow>>

Defined in: packages-ts/markets/src/polymarket/settle.ts:44

Loader contract for the resolution-source observation rows. Defaults to a no-op stub so the security/validation gates can be unit-tested without pulling live cache data; production callers wire the cache reader here.

Returning an empty array signals “no rows available for this date” and surfaces as PolymarketSettlementError downstream.

string

string

string

Promise<ReadonlyArray<InternationalRow>>


PolymarketResolutionSourceType: typeof POLYMARKET_RESOLUTION_SOURCE_TYPES[number]

Defined in: packages-ts/markets/src/polymarket/types.ts:38


PolymarketStrikeKind: "above" | "below" | "between"

Defined in: packages-ts/markets/src/polymarket-strike.ts:24


SettlementUnit: "fahrenheit" | "celsius"

Defined in: packages-ts/markets/src/polymarket/types.ts:105

Native unit of the market’s published settlement value. Codex iter-3 P2: international Polymarket markets publish in whole-°C, US in °F. The settle engine returns the resolved value in BOTH units so the caller’s comparison against Polymarket’s published value uses the matching unit.

const DEFERRED_STATIONS: ReadonlySet<string>

Defined in: packages-ts/markets/src/polymarket/types.ts:85

Markets routed to v0.2 sources. Phase 23: Hong Kong settles against HKO (the Observatory, weather.gov.hk) and Taipei moved RCTP→RCSS (CWA). Both fully defer every measure; the old VHHH/RCTP airport ICAOs are no longer deferred.


const EVENT_ID_RE: RegExp

Defined in: packages-ts/markets/src/polymarket/types.ts:50

Event id pattern. Python widened this in codex iter-2 P1: real Gamma IDs are numeric strings ("12345"), but condition-tag UUIDs + slugs also appear in the wild. Wide enough to accept real Gamma payloads but narrow enough to defend against URL-path injection.

The plan called this “UUID4 regex” but follows Python’s actual behavior: the strict UUID4 form rejected every real Gamma event, breaking the discover → settle round-trip.


const MAX_DESCRIPTION_BYTES: number

Defined in: packages-ts/markets/src/polymarket/types.ts:57

Max bytes of a Polymarket event description we’ll parse. Polymarket descriptions are concise; oversized payloads indicate hostile input (ReDoS defense).


const NETLOC_TO_RESOLUTION_TYPE: Readonly<Record<string, string>>

Defined in: packages-ts/markets/src/polymarket/types.ts:23

Per-netloc → enum value for the resolutionSourceType field on settlement records. hko / cwa are predeclared for v0.2 (HKO/CWA clients).


const POLYMARKET_KNOWN_WRONG_STATIONS: Readonly<Record<string, ReadonlySet<string>>>

Defined in: packages-ts/markets/src/polymarket/known-wrong-stations.ts:14


const POLYMARKET_RESOLUTION_SOURCE_TYPES: readonly ["wunderground", "noaa_wrh", "hko", "cwa", "other"]

Defined in: packages-ts/markets/src/polymarket/types.ts:31

Enum values for the resolutionSourceType column.


const RESOLUTION_SOURCE_ALLOWLIST: ReadonlySet<string>

Defined in: packages-ts/markets/src/polymarket/types.ts:12

Resolution-source netlocs we trust. Anything else throws PolymarketEventError. Mirrors Python RESOLUTION_SOURCE_ALLOWLIST.


const SETTLE_DELAY_HOURS: Readonly<Record<string, number>>

Defined in: packages-ts/markets/src/polymarket/types.ts:67

Per-resolution-source publication delay. Settlement refuses to settle until now - settlementDate >= delay to avoid settling on values the issuer hasn’t published yet.

Wunderground typically posts daily extremes ~6h after local midnight; NOAA WRH ~4h. “other” gets a conservative 24h fallback.


const SLUG_DATE_RE: RegExp

Defined in: packages-ts/markets/src/polymarket/types.ts:78

Slug date extractor. Polymarket weather slugs embed the resolution date (e.g. will-nyc-be-above-80f-on-2026-05-23). Used by polymarketSettle to derive the resolution date from the slug instead of event.endDate.

deriveCity(event): null | string

Defined in: packages-ts/markets/src/polymarket/resolver.ts:50

Derive a city key from slug + title + tags. Lowercase substring match against the catalog; longest-first so multi-token cities outrank prefixes. Returns null when no match — caller decides whether to drop or surface.

PolymarketEventRaw

null | string


detectMarketMeasure(event): "default" | "high" | "low"

Defined in: packages-ts/markets/src/polymarket/resolver.ts:29

Detect whether the market resolves on the daily HIGH or LOW from keywords in the event title/slug/name. Distinct from the station-level measure: many cities have one airport for both, but the market still resolves on tmax XOR tmin.

PolymarketEventRaw

"default" | "high" | "low"


extractIcaoFromResolutionSource(text): null | string

Defined in: packages-ts/markets/src/polymarket/resolver.ts:129

Extract the canonical Wunderground PWS / airport ICAO from text.

Tier 1.5 of the resolver chain — runs between explicit event.city and slug-derive. When a Polymarket event embeds a Wunderground PWS URL, the URL IS the source of truth; no catalog lookup needed.

Multi-URL disambiguation: when multiple canonical Wunderground URLs appear, ALL extracted ICAOs MUST agree. Disagreement returns null so the resolver falls through to Tier 2 city-derive (prevents an issuer-side citation URL from silently swapping the settlement station).

Returns uppercase ICAO (4 chars, leading K) when a canonical URL is found AND any additional canonical URLs agree. Null otherwise — including the disagreement case.

undefined | null | string

null | string


extractResolutionSourceType(description): "wunderground" | "noaa_wrh" | "hko" | "cwa" | "other"

Defined in: packages-ts/markets/src/polymarket/description.ts:63

Classify a description’s resolution source by the first allowlisted netloc found. Returns "other" when no allowlisted URL appears (the settlement engine falls back to the 24-hour delay for “other”).

string

"wunderground" | "noaa_wrh" | "hko" | "cwa" | "other"


fetchEventById(eventId, opts): Promise<null | PolymarketEventRaw>

Defined in: packages-ts/markets/src/polymarket/client.ts:135

Fetch a single event by id. Useful for the settle() flow when only an id is known.

string

FetchEventsOptions = {}

Promise<null | PolymarketEventRaw>


fetchEvents(opts): Promise<PolymarketEventRaw[]>

Defined in: packages-ts/markets/src/polymarket/client.ts:73

Fetch every active weather event from Gamma, paginated by offset in PAGE_SIZE increments until either an empty page is returned or MAX_EVENTS is reached. Dedup by slug to defend against the rare case where pagination overlaps under concurrent edits on Gamma’s side.

FetchEventsOptions = {}

Promise<PolymarketEventRaw[]>


parsePolymarketStrike(text): ParsedPolymarketStrike

Defined in: packages-ts/markets/src/polymarket-strike.ts:106

Parse a Polymarket temperature strike phrase (above 30C, below 86F, between 20 and 25C) into a normalized Celsius strike.

string

ParsedPolymarketStrike

when no recognized phrase is present.


polymarketDiscover(opts): Promise<PolymarketDiscoveryRow[]>

Defined in: packages-ts/markets/src/polymarket/discover.ts:31

Discover active Polymarket weather events.

Returns one row per resolvable event. Events the resolver can’t match are dropped silently (with optional onSkip callback). Events that route to a deferred station (Taipei, HK-low) appear in the result with icao: null and measure: null so callers can SEE them.

PolymarketDiscoverOptions = {}

Promise<PolymarketDiscoveryRow[]>


polymarketSettle(args): Promise<PolymarketSettlementResult>

Defined in: packages-ts/markets/src/polymarket/settle.ts:88

Settle a single Polymarket event.

Validates the event id, description (16 KB cap, netloc allowlist), resolves the station via the city catalog, parses the resolution date from the slug, enforces the per-source publication-delay window, and pulls the daily extreme via internationalDailyExtremes.

PolymarketSettleArgs

Promise<PolymarketSettlementResult>

PolymarketEventError on bad id / bad description / unsupported station.

PolymarketSettlementError when no rows resolve for the station/date.

TooEarlyToSettleError when the publication delay hasn’t elapsed.


polymarketSettleById(eventId, args): Promise<PolymarketSettlementResult>

Defined in: packages-ts/markets/src/polymarket/settle.ts:218

Settle by event id alone. Fetches the event from Gamma first, then delegates to polymarketSettle. Useful when a caller only has the id (e.g. from a Kalshi-side cross-reference).

string

Omit<PolymarketSettleArgs, "event">

Promise<PolymarketSettlementResult>


resolveStationForEvent(event, marketMeasure): null | { city: string; icao: string; stationMeasure: "default" | "high" | "low"; }

Defined in: packages-ts/markets/src/polymarket/resolver.ts:150

Resolve an event to {icao, stationMeasure} using the city catalog.

Returns null when no city matches (caller drops the event). Raises DeferredMarketError when the resolution would route to a v0.2 source (Taipei RCTP, Hong Kong VHHH for the low-extreme market).

PolymarketEventRaw

"default" | "high" | "low"

null | { city: string; icao: string; stationMeasure: "default" | "high" | "low"; }


settlementDateFromSlug(slug): string

Defined in: packages-ts/markets/src/polymarket/resolver.ts:243

Parse the resolution date from a Polymarket weather slug. The LAST YYYY-MM-DD match wins because slugs may carry both a creation date and a resolution date (created-2026-01-01-resolves-2026-05-23) — the resolution date is typically rightmost in Polymarket’s convention.

Mirrors Python _settlement_date_from_slug architect iter-1 HIGH-4.

string

string


settlePolymarketOutcome(parsed, observedC): null | 0 | 1

Defined in: packages-ts/markets/src/polymarket-strike.ts:163

Settle an observed Celsius value against a parsed Polymarket strike.

INCLUSIVE comparison (mirrors Kalshi for cross-venue consistency): above → observed >= strike; below → observed <= strike; between → lo <= observed <= hi.

ParsedPolymarketStrike

null | number

null | 0 | 1

1 (YES), 0 (NO), or null when observedC is null (NaN label → NA outcome, row preserved).


validateDescription(description): void

Defined in: packages-ts/markets/src/polymarket/description.ts:26

Apply the 16 KB cap + netloc allowlist to a description string.

string

void

PayloadTooLargeError when the UTF-8 byte length exceeds 16 KB.

PolymarketEventError when any URL has a netloc outside the allowlist.