import {z} from 'zod' export type CreateApiApiV4OrderCollateralLimitArgs = {body: {market: string, side: 'buy' | 'sell', amount: string, price: string, clientOrderId?: string | undefined, stopLoss?: string | undefined, takeProfit?: string | undefined, postOnly?: boolean | undefined, ioc?: boolean | undefined, rpi?: boolean | undefined, positionSide?: ('LONG' | 'SHORT' | 'BOTH') | undefined, reduceOnly?: boolean | undefined, stp?: ('no' | 'cb' | 'cn' | 'co') | undefined, request: string, nonce: number}}; export type CreateApiApiV4OrderCollateralLimitOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, PayloadAuth?: string | undefined, SignatureAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const activeOrderStatus = z.enum(["NEW", "FILLED", "PARTIALLY_FILLED", "CANCELLED"]); export const createApiApiV4OrderCollateralLimitResponse = z.object({orderId: z.number().int().optional(), clientOrderId: z.string().optional(), market: z.string().optional(), side: z.enum(["buy", "sell"]).optional(), type: z.string().optional(), timestamp: z.number().optional(), dealMoney: z.string().optional(), dealStock: z.string().optional(), amount: z.string().optional(), left: z.string().optional(), dealFee: z.string().optional(), price: z.string().optional(), postOnly: z.boolean().optional(), ioc: z.boolean().optional(), status: activeOrderStatus.optional(), stp: z.string().optional(), oto: z.object({otoId: z.number().int().optional(), stopLoss: z.string().optional(), takeProfit: z.string().optional()}).nullable().optional(), positionSide: z.enum(["LONG", "SHORT", "BOTH"]).optional(), rpi: z.boolean().optional(), reduceOnly: z.boolean().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 }; /** * Collateral Limit Order * * POST /api/v4/order/collateral/limit */ export const createApiApiV4OrderCollateralLimit = async (args: CreateApiApiV4OrderCollateralLimitArgs, options?: CreateApiApiV4OrderCollateralLimitOptions) => { const {baseUrl = "https://whitebit.com", fetch: fetchFn = fetch, ApiKeyAuth, PayloadAuth, SignatureAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("X-TXC-APIKEY", ApiKeyAuth) if (PayloadAuth !== undefined) headers.set("X-TXC-PAYLOAD", PayloadAuth) if (SignatureAuth !== undefined) headers.set("X-TXC-SIGNATURE", SignatureAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v4/order/collateral/limit${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(createApiApiV4OrderCollateralLimitResponse, await res.json()) };