import {z} from 'zod' export type Symbol = string; export type OrderId = string; export type OrderLinkId = string; export type GetApiApiV1ExecutionListArgs = {sub_account_address: string, symbol?: (Symbol | null) | undefined, order_id?: (OrderId | null) | undefined, order_link_id?: (OrderLinkId | null) | undefined, start_time?: (string | null) | undefined, end_time?: (string | null) | undefined, limit?: (number | null) | undefined, cursor?: (string | null) | undefined}; export type GetApiApiV1ExecutionListOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createType = z.enum(["CreateByUser", "CreateByFutureSpread", "CreateByAdminClosing", "CreateBySettle", "CreateByStopOrder", "CreateByTakeProfit", "CreateByPartialTakeProfit", "CreateByStopLoss", "CreateByPartialStopLoss", "CreateByTrailingStop", "CreateByTrailingProfit", "CreateByLiq", "CreateByTakeOverPassThrough", "CreateByAdlPassThrough", "CreateByBlockPassThrough", "CreateByBlockTradeMovePositionPassThrough", "CreateByClosing", "CreateByFGridBot", "CloseByFGridBot", "CreateByTWAP", "CreateByTVSignal", "CreateByMmRateClose", "CreateByMartingaleBot", "CloseByMartingaleBot", "CreateByIceBerg", "CreateByArbitrage", "CreateByDdh", "CreateByBboOrder"]); export const executionId = z.string(); export const execType = z.enum(["Trade", "AdlTrade", "Funding", "BustTrade", "Delivery", "Settle", "BlockTrade", "MovePosition", "FutureSpread"]); export const orderId = z.string(); export const orderLinkId = z.string(); export const orderType = z.enum(["Limit", "Market"]); export const side = z.enum(["Sell", "Buy"]); export const stopOrderType = z.enum(["TakeProfit", "StopLoss", "TrailingStop", "Stop", "PartialTakeProfit", "PartialStopLoss", "TpslOrder", "OcoOrder", "MmRateClose", "BidirectionalTpslOrder"]); export const symbol = z.string(); export const execution = z.object({base_fee: z.string(), closed_size: z.string().nullable().optional(), create_type: createType.nullable().optional(), exec_id: executionId, exec_price: z.string(), exec_qty: z.string(), exec_time: z.string(), exec_type: execType, exec_value: z.string(), fee_rate: z.string(), index_price: z.string(), is_maker: z.boolean(), leaves_qty: z.string(), mark_price: z.string(), order_id: orderId, order_link_id: orderLinkId.nullable().optional(), order_price: z.string(), order_qty: z.string(), order_type: orderType, seq: z.number().int().gte(0), side: side, stop_order_type: stopOrderType.nullable().optional(), sub_account_address: z.string(), symbol: symbol}); export const getTradeHistoryResponse = z.object({list: z.array(execution), next_page_cursor: z.string().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 }; /** * Get Trade History * * GET /api/v1/execution/list */ export const getApiApiV1ExecutionList = async (args: GetApiApiV1ExecutionListArgs, options?: GetApiApiV1ExecutionListOptions) => { const {baseUrl = "https://api.ekiden.fi", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.sub_account_address != null) query.append("sub_account_address", String(args.sub_account_address)) if (args.symbol != null) query.append("symbol", String(args.symbol)) if (args.order_id != null) query.append("order_id", String(args.order_id)) if (args.order_link_id != null) query.append("order_link_id", String(args.order_link_id)) if (args.start_time != null) query.append("start_time", String(args.start_time)) if (args.end_time != null) query.append("end_time", String(args.end_time)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/execution/list${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(getTradeHistoryResponse, await res.json()) };