import {z} from 'zod' export type ConnectProtocolVersion = number; export type ConnectTimeoutHeader = number; export type BaseObjectsV1Side = 'SIDE_LONG' | 'SIDE_SHORT'; export type OrderServiceV1GetExecutableOrdersRequest = {marketId: number | unknown, price: number, side?: (BaseObjectsV1Side | null) | undefined, limit?: (number | null) | undefined}; export type CreateApiOrderServiceV1OrderServiceGetExecutableOrdersArgs = {'Connect-Protocol-Version': ConnectProtocolVersion, 'Connect-Timeout-Ms'?: ConnectTimeoutHeader | undefined, body: OrderServiceV1GetExecutableOrdersRequest}; export type CreateApiOrderServiceV1OrderServiceGetExecutableOrdersOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const googleProtobufTimestamp = z.string(); export const baseObjectsV1OrderType = z.literal("ORDER_TYPE_MARKET"); export const baseObjectsV1Side = z.enum(["SIDE_LONG", "SIDE_SHORT"]); export const baseObjectsV1OrderStatus = z.enum(["ORDER_STATUS_PENDING", "ORDER_STATUS_PARTIALLY_FILLED", "ORDER_STATUS_FULLY_FILLED", "ORDER_STATUS_CLOSED", "ORDER_STATUS_CANCELLED", "ORDER_STATUS_MERGED", "ORDER_STATUS_PARTIALLY_MERGED", "ORDER_STATUS_PARTIALLY_CANCELLED"]); export const baseObjectsV1Order = z.object({owner: z.string().optional(), orderId: z.string().optional(), timestamp: googleProtobufTimestamp.optional(), marketId: z.string().optional(), market: z.string().optional(), type: baseObjectsV1OrderType.optional(), side: baseObjectsV1Side.optional(), thresholdPrice: z.number().optional(), size: z.number().optional(), leverage: z.number().optional(), status: baseObjectsV1OrderStatus.optional(), filledSize: z.number().optional(), unfilledSize: z.number().optional(), reduceOnly: z.boolean().optional(), canceled: z.boolean().optional(), initialMargin: z.number().optional(), tickDecimals: z.number().int().optional(), fees: z.number().optional(), rebates: z.number().optional(), reduceOrderType: z.string().optional(), initialNotional: z.number().optional(), stopLossPrice: z.number().optional(), takeProfitPrice: z.number().optional()}); export const orderServiceV1GetExecutableOrdersResponse = z.object({orders: z.array(baseObjectsV1Order).optional(), totalCount: z.number().int().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 }; /** * GetExecutableOrders * * POST /order_service.v1.OrderService/GetExecutableOrders */ export const createApiOrderServiceV1OrderServiceGetExecutableOrders = async (args: CreateApiOrderServiceV1OrderServiceGetExecutableOrdersArgs, options?: CreateApiOrderServiceV1OrderServiceGetExecutableOrdersOptions) => { const {baseUrl = "https://api.fairground.fi", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Connect-Protocol-Version'] != null) headers.set("Connect-Protocol-Version", String(args['Connect-Protocol-Version'])) if (args['Connect-Timeout-Ms'] != null) headers.set("Connect-Timeout-Ms", String(args['Connect-Timeout-Ms'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/order_service.v1.OrderService/GetExecutableOrders${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(orderServiceV1GetExecutableOrdersResponse, await res.json()) };