import {z} from 'zod' export type GetApiV1TradesSymbolFillsArgs = {symbol: string, limit?: (number | null) | undefined, cursor?: (string | null) | undefined, startTime?: (number | null) | undefined, endTime?: (number | null) | undefined}; export type GetApiV1TradesSymbolFillsOptions = {baseUrl?: string | undefined, PhoenixBearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const fillRecord = z.object({baseQty: z.string(), instructionType: z.string(), marketSymbol: z.string(), price: z.string(), quoteQty: z.string(), timestamp: z.string(), transactionSignature: z.string()}); export const fillsResponse = z.object({data: z.array(fillRecord), hasMore: z.boolean(), nextCursor: z.string().nullable().optional(), prevCursor: z.string().nullable().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 market fills * * GET /v1/trades/{symbol}/fills */ export const getApiV1TradesSymbolFills = async (args: GetApiV1TradesSymbolFillsArgs, options?: GetApiV1TradesSymbolFillsOptions) => { const {baseUrl = "https://perp-api.phoenix.trade", fetch: fetchFn = fetch, PhoenixBearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.startTime != null) query.append("startTime", String(args.startTime)) if (args.endTime != null) query.append("endTime", String(args.endTime)) if (PhoenixBearerAuth !== undefined) headers.set('authorization', `Bearer ${PhoenixBearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/trades/${encodeURIComponent(String(args.symbol))}/fills${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(fillsResponse, await res.json()) };