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 CreateApiApiV1OrderAmendArgs = {body: AmendOrderRequest}; export type CreateApiApiV1OrderAmendOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; 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 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 }; /** * Amend Order * * POST /api/v1/order/amend */ export const createApiApiV1OrderAmend = async (args: CreateApiApiV1OrderAmendArgs, options?: CreateApiApiV1OrderAmendOptions) => { 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${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(amendOrderResponse, await res.json()) };