import {z} from 'zod' export type BatchCancelOrderInput = {order_ids: Array}; export type CreateApiApiV2AccountsAccountIdOrdersCancelArgs = {body: BatchCancelOrderInput}; export type CreateApiApiV2AccountsAccountIdOrdersCancelOptions = {baseUrl?: string | undefined, ApiKeyId?: string | undefined, ApiSecretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const brokerageOrderStatus = z.enum(["PENDING_SUBMIT", "PENDING_CANCEL", "PENDING_ESCROW", "PENDING_FILL", "ESCROWED", "SUBMITTED", "CANCELLED", "PARTIALLY_FILLED", "FILLED", "REJECTED", "REQUIRING_CONTACT", "ERROR"]); export const chainId = z.enum(["eip155:1", "eip155:42161", "eip155:8453", "eip155:81457", "eip155:98866", "eip155:999", "eip155:43114", "eip155:56", "eip155:5042", "eip155:1329", "eip155:11155111", "eip155:421614", "eip155:84532", "eip155:168587773", "eip155:98867", "eip155:998", "eip155:43113", "eip155:97", "eip155:5042002", "eip155:1328", "eip155:202110", "eip155:179205", "eip155:179202", "eip155:98865", "eip155:7887"]); export const orderSide = z.enum(["BUY", "SELL"]); export const orderType = z.enum(["MARKET", "LIMIT"]); export const orderTif = z.enum(["DAY", "GTC", "IOC", "FOK"]); export const order = z.object({id: z.string(), status: brokerageOrderStatus, chain_id: chainId, order_contract_address: z.string(), order_transaction_hash: z.string(), order_side: orderSide, order_type: orderType, order_tif: orderTif, payment_token: z.string(), created_dt: z.string(), stock_id: z.string().nullable().optional(), alloy_id: z.string().nullable().optional(), asset_token: z.string().nullable().optional(), asset_token_quantity: z.string().nullable().optional(), payment_token_quantity: z.string().nullable().optional(), limit_price: z.string().nullable().optional(), fee: z.string().nullable().optional(), cancel_transaction_hash: z.string().nullable().optional(), order_request_id: z.string().nullable().optional(), client_order_id: z.string().nullable().optional()}); export const batchCancelOrderResponse = z.object({cancel_queued_orders: z.array(order), failed_to_cancel_orders: z.array(order)}); 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 }; /** * Cancel Orders * * POST /api/v2/accounts/{account_id}/orders/cancel */ export const createApiApiV2AccountsAccountIdOrdersCancel = async (args: CreateApiApiV2AccountsAccountIdOrdersCancelArgs, options?: CreateApiApiV2AccountsAccountIdOrdersCancelOptions) => { const {baseUrl = "https://api-enterprise.sbt.dinari.com", fetch: fetchFn = fetch, ApiKeyId, ApiSecretKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyId !== undefined) headers.set("X-API-Key-Id", ApiKeyId) if (ApiSecretKey !== undefined) headers.set("X-API-Secret-Key", ApiSecretKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/accounts/${encodeURIComponent(String(args.account_id))}/orders/cancel${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(batchCancelOrderResponse, await res.json()) };