import {z} from 'zod' export type OrderId = string; export type OrderLinkId = string; export type Symbol = string; export type TpSlMode = 'Full' | 'Partial'; export type TriggerBy = 'LastPrice' | 'IndexPrice' | 'MarkPrice'; export type TpSlSpec = {market: {trigger_by: TriggerBy, trigger_price: string}} | {limit: {limit_price: string, trigger_by: TriggerBy, trigger_price: string}}; export type TpSl = {mode: TpSlMode, stop_loss?: (TpSlSpec | null) | undefined, take_profit?: (TpSlSpec | null) | undefined}; export type TriggerDirection = 'Up' | 'Down'; export type ConditionalOrder = {trigger_by: TriggerBy, trigger_direction: TriggerDirection, trigger_price: string}; export type AmendOrderRequest = {order_id?: (OrderId | null) | undefined, order_iv?: (number | null) | undefined, order_link_id?: (OrderLinkId | null) | undefined, price: string, qty: string, sub_account_address: string, symbol: Symbol, tpsl?: (TpSl | null) | undefined, trigger?: (ConditionalOrder | null) | undefined}; export type BatchAmendOrdersRequest = {await_outcome?: boolean | undefined, request: Array}; export type CreateApiApiV1OrderAmendBatchArgs = {body: BatchAmendOrdersRequest}; export type CreateApiApiV1OrderAmendBatchOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const string = z.string(); export const exitMessage = z.union([z.object({Error: z.object({code: z.number().int().gte(0), msg: string})}), z.object({Success: z.object({code: z.number().int().gte(0), msg: z.string()})})]); export const orderId = z.string(); export const orderLinkId = z.string(); export const amendOrderResponse = z.object({order_id: orderId.nullable().optional(), order_link_id: orderLinkId.nullable().optional()}); export const batchAmendOrdersResponse = z.object({exit_info: z.array(exitMessage), result: z.array(amendOrderResponse), time: 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 }; /** * Batch Amend Orders * * POST /api/v1/order/amend-batch */ export const createApiApiV1OrderAmendBatch = async (args: CreateApiApiV1OrderAmendBatchArgs, options?: CreateApiApiV1OrderAmendBatchOptions) => { const {baseUrl = "https://api.ekiden.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 (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/order/amend-batch${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(batchAmendOrdersResponse, await res.json()) };