import {z} from 'zod' export type OrderType = 'ORDER_TYPE_MARKET_TO_LIMIT' | 'ORDER_TYPE_LIMIT' | 'ORDER_TYPE_STOP' | 'ORDER_TYPE_STOP_LIMIT'; export type Amount = {value: unknown, currency: string}; export type TimeInForce = 'TIME_IN_FORCE_DAY' | 'TIME_IN_FORCE_GOOD_TILL_CANCEL' | 'TIME_IN_FORCE_IMMEDIATE_OR_CANCEL' | 'TIME_IN_FORCE_GOOD_TILL_TIME' | 'TIME_IN_FORCE_FILL_OR_KILL'; export type OrderIntent = 'ORDER_INTENT_BUY_LONG' | 'ORDER_INTENT_SELL_LONG' | 'ORDER_INTENT_BUY_SHORT' | 'ORDER_INTENT_SELL_SHORT'; export type OutcomeSide = 'OUTCOME_SIDE_YES' | 'OUTCOME_SIDE_NO'; export type OrderAction = 'ORDER_ACTION_BUY' | 'ORDER_ACTION_SELL'; export type ManualOrderIndicator = 'MANUAL_ORDER_INDICATOR_MANUAL' | 'MANUAL_ORDER_INDICATOR_AUTOMATED'; export type SlippageTolerance = {currentPrice?: Amount | undefined, bips?: (number | null) | undefined, ticks?: (number | null) | undefined}; export type CreateOrderRequest = {marketSlug: string, type?: OrderType | undefined, price?: Amount | undefined, quantity?: number | undefined, tif?: TimeInForce | undefined, participateDontInitiate?: boolean | undefined, goodTillTime?: string | undefined, intent?: OrderIntent | undefined, outcomeSide?: OutcomeSide | undefined, action?: OrderAction | undefined, cashOrderQty?: Amount | undefined, manualOrderIndicator?: ManualOrderIndicator | undefined, synchronousExecution?: boolean | undefined, maxBlockTime?: unknown | undefined, slippageTolerance?: SlippageTolerance | undefined}; export type CreateOrderListRequest = {orders: Array}; export type CreateApiV1OrdersBatchedArgs = {body: CreateOrderListRequest}; export type CreateApiV1OrdersBatchedOptions = {baseUrl?: string | undefined, XPMAccessKey?: string | undefined, XPMTimestamp?: string | undefined, XPMSignature?: string | undefined, fetch?: typeof fetch | undefined}; export const createOrderListResponse = z.object({createdOrderIds: z.array(z.string()).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 }; /** * Create Multiple Orders * * POST /v1/orders/batched */ export const createApiV1OrdersBatched = async (args: CreateApiV1OrdersBatchedArgs, options?: CreateApiV1OrdersBatchedOptions) => { const {baseUrl = "https://api.polymarket.us", fetch: fetchFn = fetch, XPMAccessKey, XPMTimestamp, XPMSignature} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") 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/orders/batched${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createOrderListResponse, await res.json()) };