import {z} from 'zod' export type GetApiV1IncentivesArgs = {pageSize?: number | undefined, pageToken?: string | undefined, symbols?: Array | undefined, orderBy?: 'created_at' | undefined, orderDirection?: ('asc' | 'desc') | undefined, statuses?: (Array<'active' | 'closed' | 'pending'>) | undefined}; export type GetApiV1IncentivesOptions = {baseUrl?: string | undefined, XPMAccessKey?: string | undefined, XPMTimestamp?: string | undefined, XPMSignature?: string | undefined, fetch?: typeof fetch | undefined}; export const timePeriod = z.object({programId: z.string().optional(), programType: z.string().optional(), start: z.string().optional(), end: z.string().optional(), rewardPool: z.number().optional(), status: z.enum(["active", "closed", "pending"]).optional(), discountFactor: z.number().nullable().optional(), targetSize: z.number().int().nullable().optional(), period: z.string().optional(), createdAt: z.string().optional()}); export const incentiveProgram = z.object({marketSlug: z.string().optional(), timePeriods: z.array(timePeriod).optional()}); export const listIncentivesResponse = z.object({programs: z.array(incentiveProgram).optional(), nextPageToken: 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 Incentive Programs * * GET /v1/incentives */ export const getApiV1Incentives = async (args: GetApiV1IncentivesArgs, options?: GetApiV1IncentivesOptions) => { 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.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.pageToken != null) query.append("pageToken", String(args.pageToken)) for (const item of args.symbols ?? []) query.append("symbols", String(item)) if (args.orderBy != null) query.append("orderBy", String(args.orderBy)) if (args.orderDirection != null) query.append("orderDirection", String(args.orderDirection)) for (const item of args.statuses ?? []) query.append("statuses", String(item)) 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/incentives${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(listIncentivesResponse, await res.json()) };