import {z} from 'zod' export type GetApiV1MarketsSlugBookArgs = {slug: string}; export type GetApiV1MarketsSlugBookOptions = {baseUrl?: string | undefined, XPMAccessKey?: string | undefined, XPMTimestamp?: string | undefined, XPMSignature?: string | undefined, fetch?: typeof fetch | undefined}; export const gatewayTypesV1Amount = z.object({value: z.string(), currency: z.string()}); export const sharedMarketDataV1BookEntry = z.object({px: gatewayTypesV1Amount.optional(), qty: z.string().optional()}); export const sharedMarketDataV1MarketState = z.enum(["MARKET_STATE_OPEN", "MARKET_STATE_PREOPEN", "MARKET_STATE_SUSPENDED", "MARKET_STATE_EXPIRED", "MARKET_STATE_TERMINATED", "MARKET_STATE_HALTED", "MARKET_STATE_MATCH_AND_CLOSE_AUCTION"]); export const sharedMarketDataV1SettlementPriceCalculationMethod = z.enum(["SETTLEMENT_PRICE_CALCULATION_METHOD_VWAP_TIER_1", "SETTLEMENT_PRICE_CALCULATION_METHOD_VWAP_TIER_2_RULE_1", "SETTLEMENT_PRICE_CALCULATION_METHOD_VWAP_TIER_2_RULE_2", "SETTLEMENT_PRICE_CALCULATION_METHOD_VWAP_TIER_2_RULE_3", "SETTLEMENT_PRICE_CALCULATION_METHOD_VWAP_TIER_2_RULE_4", "SETTLEMENT_PRICE_CALCULATION_METHOD_EVENT_TIER_1", "SETTLEMENT_PRICE_CALCULATION_METHOD_EVENT_TIER_2", "SETTLEMENT_PRICE_CALCULATION_METHOD_EVENT_TIER_3", "SETTLEMENT_PRICE_CALCULATION_METHOD_EVENT_TIER_4", "SETTLEMENT_PRICE_CALCULATION_METHOD_OVERRIDE", "SETTLEMENT_PRICE_CALCULATION_METHOD_VWAP_TIER_2_RULE_5", "SETTLEMENT_PRICE_CALCULATION_METHOD_VWAP_TIER_2_RULE_6", "SETTLEMENT_PRICE_CALCULATION_METHOD_VWAP_TIER_2_RULE_7", "SETTLEMENT_PRICE_CALCULATION_METHOD_VWAP_TIER_2_RULE_8"]); export const sharedMarketDataV1PriceSample = z.object({longPx: gatewayTypesV1Amount.optional(), shortPx: gatewayTypesV1Amount.optional(), ts: z.string().optional()}); export const sharedMarketDataV1MarketStats = z.object({openPx: gatewayTypesV1Amount.optional(), closePx: gatewayTypesV1Amount.optional(), lowPx: gatewayTypesV1Amount.optional(), highPx: gatewayTypesV1Amount.optional(), lastTradePx: gatewayTypesV1Amount.optional(), indicativeOpenPx: gatewayTypesV1Amount.optional(), settlementPx: gatewayTypesV1Amount.optional(), sharesTraded: z.string().optional(), notionalTraded: gatewayTypesV1Amount.optional(), lastTradeQty: z.string().optional(), openInterest: z.string().optional(), settlementPreliminaryFlag: z.boolean().optional(), openSetTime: z.string().optional(), closeSetTime: z.string().optional(), highSetTime: z.string().optional(), lowSetTime: z.string().optional(), lastTradeSetTime: z.string().optional(), indicativeOpenSetTime: z.string().optional(), settlementSetTime: z.string().optional(), openInterestSetTime: z.string().optional(), notionalSetTime: z.string().optional(), settlementPriceCalculationMethod: sharedMarketDataV1SettlementPriceCalculationMethod.optional(), tradingReferencePx: gatewayTypesV1Amount.optional(), tradingReferenceSetTime: z.string().optional(), currentPx: gatewayTypesV1Amount.optional(), lastPriceSample: sharedMarketDataV1PriceSample.optional()}); export const sharedMarketDataV1MarketData = z.object({marketSlug: z.string().optional(), bids: z.array(sharedMarketDataV1BookEntry).optional(), offers: z.array(sharedMarketDataV1BookEntry).optional(), state: sharedMarketDataV1MarketState.optional(), stats: sharedMarketDataV1MarketStats.optional(), transactTime: z.string().optional()}); export const gatewayMarketV1GetMarketBookResponse = z.object({marketData: sharedMarketDataV1MarketData.optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Get Market Book * * GET /v1/markets/{slug}/book */ export const getApiV1MarketsSlugBook = async (args: GetApiV1MarketsSlugBookArgs, options?: GetApiV1MarketsSlugBookOptions) => { const {baseUrl = "https://api.polymarket.us", fetch: fetchFn = fetch, XPMAccessKey, XPMTimestamp, XPMSignature} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (XPMAccessKey !== undefined) headers.set("X-PM-Access-Key", XPMAccessKey) if (XPMTimestamp !== undefined) headers.set("X-PM-Timestamp", XPMTimestamp) if (XPMSignature !== undefined) headers.set("X-PM-Signature", XPMSignature) const url = `${baseUrl.replace(/\/+$/, '')}/v1/markets/${encodeURIComponent(String(args.slug))}/book${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(gatewayMarketV1GetMarketBookResponse, await res.json()) };