import {z} from 'zod' export type Date = {year?: number | undefined, month?: number | undefined, day?: number | undefined}; export type GetTradeStatsRequest = {symbol?: string | undefined, startTime?: string | undefined, endTime?: string | undefined, bars?: number | undefined, startTradeDate?: Date | undefined, endTradeDate?: Date | undefined}; export type CreateApiV1ReportTradesStatsArgs = {body: GetTradeStatsRequest}; export type CreateApiV1ReportTradesStatsOptions = {baseUrl?: string | undefined, XPMAccessKey?: string | undefined, XPMTimestamp?: string | undefined, XPMSignature?: string | undefined, fetch?: typeof fetch | undefined}; export const tradeStats = z.object({first: z.string().optional(), last: z.string().optional(), high: z.string().optional(), low: z.string().optional(), totalTradeCount: z.string().optional(), clearedTradeCount: z.string().optional(), volume: z.string().optional(), clearedVolume: z.string().optional(), notional: z.string().optional(), clearedNotional: z.string().optional()}); export const getTradeStatsResponse = z.object({stats: tradeStats.optional(), bars: z.array(tradeStats).optional(), barStartTime: z.array(z.string()).optional(), barEndTime: 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 }; /** * Get trade stats * * POST /v1/report/trades/stats */ export const createApiV1ReportTradesStats = async (args: CreateApiV1ReportTradesStatsArgs, options?: CreateApiV1ReportTradesStatsOptions) => { 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/report/trades/stats${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(getTradeStatsResponse, await res.json()) };