import {z} from 'zod' export type GetApiSapiV1AlgoSpotSubOrdersArgs = {algoId: number, page?: number | undefined, pageSize?: string | undefined, recvWindow?: number | undefined, timestamp: number, signature: string}; export type GetApiSapiV1AlgoSpotSubOrdersOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiSapiV1AlgoSpotSubOrdersResponse = z.object({total: z.number().int(), executedQty: z.string(), executedAmt: z.string(), subOrders: z.array(z.object({algoId: z.number().int(), orderId: z.number().int(), orderStatus: z.string(), executedQty: z.string(), executedAmt: z.string(), feeAmt: z.string(), feeAsset: z.string(), bookTime: z.number().int(), avgPrice: z.string(), side: z.string(), symbol: z.string(), subId: z.number().int(), timeInForce: z.string(), origQty: z.string()}))}); 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 }; /** * Query Sub Orders * * GET /sapi/v1/algo/spot/subOrders */ export const getApiSapiV1AlgoSpotSubOrders = async (args: GetApiSapiV1AlgoSpotSubOrdersArgs, options?: GetApiSapiV1AlgoSpotSubOrdersOptions) => { const {baseUrl = "https://api.binance.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.algoId != null) query.append("algoId", String(args.algoId)) if (args.page != null) query.append("page", String(args.page)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.recvWindow != null) query.append("recvWindow", String(args.recvWindow)) if (args.timestamp != null) query.append("timestamp", String(args.timestamp)) if (args.signature != null) query.append("signature", String(args.signature)) if (ApiKeyAuth !== undefined) headers.set("X-MBX-APIKEY", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/sapi/v1/algo/spot/subOrders${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(getApiSapiV1AlgoSpotSubOrdersResponse, await res.json()) };