import {z} from 'zod' export type GetApiV1OrdersTpslArgs = {account?: string | undefined, market_id?: unknown | undefined, start_time?: unknown | undefined, end_time?: unknown | undefined, page?: number | undefined, limit?: number | undefined, statuses?: (Array<'TPSL_ORDER_STATUS_ACCEPTED' | 'TPSL_ORDER_STATUS_TRIGGERED' | 'TPSL_ORDER_STATUS_SUCCESS' | 'TPSL_ORDER_STATUS_CANCELLED'>) | undefined, stop_type?: ('TAKE_PROFIT' | 'STOP_LOSS' | 'STOP_TYPE_NONE') | undefined}; export type GetApiV1OrdersTpslOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const sharedOrderSide = z.enum(["BUY", "SELL"]); export const sharedOrderStopType = z.enum(["TAKE_PROFIT", "STOP_LOSS", "STOP_TYPE_NONE"]); export const sharedOrderType = z.enum(["MARKET", "LIMIT"]); export const sharedStopPriceOption = z.enum(["LAST_TRADED_PRICE", "MARK_PRICE", "PRICE_OPTION_NONE"]); export const apiTpslOrderStatus = z.enum(["TPSL_ORDER_STATUS_ACCEPTED", "TPSL_ORDER_STATUS_TRIGGERED", "TPSL_ORDER_STATUS_SUCCESS", "TPSL_ORDER_STATUS_CANCELLED"]); export const sharedOrderTimeInforce = z.enum(["GTC", "GTT", "FOK", "IOC"]); export const apiTpslOrder = z.object({order_id: z.string().optional(), account: z.string().optional(), market_id: z.string().optional(), side: sharedOrderSide.optional(), size: z.string().optional(), stop_type: sharedOrderStopType.optional(), order_type: sharedOrderType.optional(), stop_price: z.string().optional(), limit_price: z.string().optional(), stop_price_option: sharedStopPriceOption.optional(), status: apiTpslOrderStatus.optional(), tif: sharedOrderTimeInforce.optional(), created_at: z.string().optional(), expires_at: z.string().optional(), triggered_at: z.string().optional(), triggered_price: z.string().optional(), trigger_tx_hash: z.string().optional(), triggered_tx_hash: z.string().optional(), triggered_order_id: z.string().optional(), cancel_reason: z.string().optional(), size_percent_bps: z.number().int().optional(), filled_size: z.string().optional()}); export const apiGetTpslOrdersResponse = z.object({orders: z.array(apiTpslOrder).optional(), total: z.string().optional(), page: z.number().int().optional(), limit: z.number().int().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 TP/SL orders * * GET /v1/orders/tpsl */ export const getApiV1OrdersTpsl = async (args: GetApiV1OrdersTpslArgs, options?: GetApiV1OrdersTpslOptions) => { const {baseUrl = "https://api.testnet.rise.trade", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.account != null) query.append("account", String(args.account)) if (args.market_id != null) query.append("market_id", String(args.market_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.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) for (const item of args.statuses ?? []) query.append("statuses", String(item)) if (args.stop_type != null) query.append("stop_type", String(args.stop_type)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/orders/tpsl${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(apiGetTpslOrdersResponse, await res.json()) };