import {z} from 'zod' export type CreateApiSapiV1AlgoSpotNewOrderTwapArgs = {symbol: string, side: 'SELL' | 'BUY', quantity: number, duration: number, clientAlgoId?: string | undefined, limitPrice?: number | undefined, recvWindow?: number | undefined, timestamp: number, signature: string}; export type CreateApiSapiV1AlgoSpotNewOrderTwapOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiSapiV1AlgoSpotNewOrderTwapResponse = z.object({clientAlgoId: z.string(), success: z.boolean(), code: z.number().int(), msg: 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 }; /** * Time-Weighted Average Price (Twap) New Order * * POST /sapi/v1/algo/spot/newOrderTwap */ export const createApiSapiV1AlgoSpotNewOrderTwap = async (args: CreateApiSapiV1AlgoSpotNewOrderTwapArgs, options?: CreateApiSapiV1AlgoSpotNewOrderTwapOptions) => { 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.symbol != null) query.append("symbol", String(args.symbol)) if (args.side != null) query.append("side", String(args.side)) if (args.quantity != null) query.append("quantity", String(args.quantity)) if (args.duration != null) query.append("duration", String(args.duration)) if (args.clientAlgoId != null) query.append("clientAlgoId", String(args.clientAlgoId)) if (args.limitPrice != null) query.append("limitPrice", String(args.limitPrice)) 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/newOrderTwap${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiSapiV1AlgoSpotNewOrderTwapResponse, await res.json()) };