import {z} from 'zod' export type GetApiV1OrdersOpenArgs = {orderIds?: string | undefined}; export type GetApiV1OrdersOpenOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const getApiV1OrdersOpenResponse = z.object({networkId: z.string(), contractAddress: z.string(), orders: z.array(z.object({orderId: z.string(), batcherCiphertext: z.string(), transientOrderCommit: z.string(), persistentOrderCommit: z.string(), cancellationCommit: z.string(), firstCoinColor: z.string(), firstCoinValue: z.string(), firstCoinNonce: z.string(), firstCoinMerkleIndex: z.string(), secondCoinColor: z.string().nullable(), secondCoinValue: z.string().nullable(), secondCoinNonce: z.string().nullable(), secondCoinMerkleIndex: z.string().nullable(), candidatePoolId: z.string().nullable(), status: z.enum(["open", "closed"]), openedAtBlockHeight: z.number().int(), openedAtBlockHash: z.string(), openedAtTxId: z.string(), openedAtTxHash: z.string(), openedAtTimestamp: z.number().int(), closedAtBlockHeight: z.number().int().nullable(), closedAtBlockHash: z.string().nullable(), closedAtTxId: z.string().nullable(), closedAtTxHash: z.string().nullable(), closedAtTimestamp: z.number().int().nullable(), closeReason: z.enum(["cancelled", "batched", "unknown_closed"]).nullable()})), requestedOrderIds: z.array(z.string()).optional(), missingOrderIds: 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 }; /** * List currently open public orders, optionally filtered by orderId. * * GET /v1/orders/open */ export const getApiV1OrdersOpen = async (args: GetApiV1OrdersOpenArgs, options: GetApiV1OrdersOpenOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.orderIds != null) query.append("orderIds", String(args.orderIds)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/orders/open${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(getApiV1OrdersOpenResponse, await res.json()) };