import {z} from 'zod' export type GetApiV1PositionsArgs = {name?: string | undefined, symbol?: string | undefined, as_of_time?: string | undefined, 'as_of_date.year'?: number | undefined, 'as_of_date.month'?: number | undefined, 'as_of_date.day'?: number | undefined}; export type GetApiV1PositionsOptions = {baseUrl?: string | undefined, XPMAccessKey?: string | undefined, XPMTimestamp?: string | undefined, XPMSignature?: string | undefined, fetch?: typeof fetch | undefined}; export const position = z.object({account: z.string().optional(), symbol: z.string().optional(), netPosition: z.string().optional(), qtyBought: z.string().optional(), qtySold: z.string().optional(), cost: z.string().optional(), realized: z.string().optional(), bodPosition: z.string().optional(), expired: z.boolean().optional(), updateTime: z.string().optional()}); export const listAccountPositionsResponse = z.object({positions: z.array(position).optional(), availablePosition: 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 }; /** * List account positions * * GET /v1/positions */ export const getApiV1Positions = async (args: GetApiV1PositionsArgs, options?: GetApiV1PositionsOptions) => { 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 (args.name != null) query.append("name", String(args.name)) if (args.symbol != null) query.append("symbol", String(args.symbol)) if (args.as_of_time != null) query.append("as_of_time", String(args.as_of_time)) if (args['as_of_date.year'] != null) query.append("as_of_date.year", String(args['as_of_date.year'])) if (args['as_of_date.month'] != null) query.append("as_of_date.month", String(args['as_of_date.month'])) if (args['as_of_date.day'] != null) query.append("as_of_date.day", String(args['as_of_date.day'])) 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/positions${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(listAccountPositionsResponse, await res.json()) };