import {z} from 'zod' export type GetApiV1PositionsLedgerArgs = {account: string, symbol?: string | undefined, start_time?: string | undefined, end_time?: string | undefined, page_size?: number | undefined, page_token?: string | undefined, newest_first?: boolean | undefined}; export type GetApiV1PositionsLedgerOptions = {baseUrl?: string | undefined, XPMAccessKey?: string | undefined, XPMTimestamp?: string | undefined, XPMSignature?: string | undefined, fetch?: typeof fetch | undefined}; export const positionLedgerEntry = z.object({id: z.string().optional(), account: z.string().optional(), symbol: z.string().optional(), quantityChange: z.string().optional(), costChange: z.string().optional(), realizedChange: z.string().optional(), netPosition: z.string().optional(), cost: z.string().optional(), realized: z.string().optional(), updateTime: z.string().optional(), updateBusinessDate: z.string().optional(), description: z.string().optional()}); export const getPositionLedgerResponse = z.object({entries: z.array(positionLedgerEntry).optional(), nextPageToken: z.string().optional(), eof: z.boolean().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 position ledger * * GET /v1/positions/ledger */ export const getApiV1PositionsLedger = async (args: GetApiV1PositionsLedgerArgs, options?: GetApiV1PositionsLedgerOptions) => { 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.account != null) query.append("account", String(args.account)) if (args.symbol != null) query.append("symbol", String(args.symbol)) if (args.start_time != null) query.append("start_time", String(args.start_time)) if (args.end_time != null) query.append("end_time", String(args.end_time)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.page_token != null) query.append("page_token", String(args.page_token)) if (args.newest_first != null) query.append("newest_first", String(args.newest_first)) 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/ledger${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(getPositionLedgerResponse, await res.json()) };