import {z} from 'zod' export type SfcApiUnixEpoch = number; export type SfcApiOrderbookFillsCursor = string; export type GetApiPreviewV2OrderbookFillsArgs = {requirements?: string | undefined, start_at: SfcApiUnixEpoch, end_at: SfcApiUnixEpoch, since_at?: SfcApiUnixEpoch | undefined, limit?: number | undefined, starting_after?: SfcApiOrderbookFillsCursor | undefined}; export type GetApiPreviewV2OrderbookFillsOptions = {baseUrl: string, sfcApiBearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const sfcApiInstanceTypeCategoryVariant = z.string(); export const sfcApiInstanceTypeCategoryRequirements = z.array(sfcApiInstanceTypeCategoryVariant); export const sfcApiInstanceTypeRequirements = z.record(z.string(), sfcApiInstanceTypeCategoryRequirements); export const sfcApiUnixEpoch = z.number().int(); export const sfcApiOrderbookFillsCursor = z.string(); export const sfcApiDollarsPerNodeHour = z.string(); export const sfcApiOrderbookFill = z.object({dollars_per_node_hour: sfcApiDollarsPerNodeHour, node_count: z.number().int(), filled_at: sfcApiUnixEpoch}); export const sfcApiListOrderbookFillsResponse = z.object({object: z.literal("list"), requirements: sfcApiInstanceTypeRequirements, start_at: sfcApiUnixEpoch, end_at: sfcApiUnixEpoch, requested_at: sfcApiUnixEpoch, cursor: sfcApiOrderbookFillsCursor.nullable().optional(), has_more: z.boolean(), data: z.array(sfcApiOrderbookFill)}); 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 market fills * * GET /preview/v2/orderbook/fills */ export const getApiPreviewV2OrderbookFills = async (args: GetApiPreviewV2OrderbookFillsArgs, options: GetApiPreviewV2OrderbookFillsOptions) => { const {baseUrl, fetch: fetchFn = fetch, sfcApiBearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.requirements != null) query.append("requirements", String(args.requirements)) if (args.start_at != null) query.append("start_at", String(args.start_at)) if (args.end_at != null) query.append("end_at", String(args.end_at)) if (args.since_at != null) query.append("since_at", String(args.since_at)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.starting_after != null) query.append("starting_after", String(args.starting_after)) if (sfcApiBearerAuth !== undefined) headers.set('authorization', `Bearer ${sfcApiBearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/preview/v2/orderbook/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(sfcApiListOrderbookFillsResponse, await res.json()) };