import {z} from 'zod' export type GetApiApiV1TradeStandbyOrdersArgs = {symbol?: string | undefined}; export type GetApiApiV1TradeStandbyOrdersOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const orderSide = z.enum(["LONG", "SHORT", "UNSPECIFIED"]); export const orderType = z.enum(["LIMIT", "MARKET", "STOP_LIMIT", "STOP_MARKET", "LIQUIDATION", "STOP_LOSS_MARKET", "TAKE_PROFIT_MARKET", "STOP_LOSS_LIMIT", "TAKE_PROFIT_LIMIT", "BANKRUPTCY_LIQUIDATION", "UNSPECIFIED"]); export const orderTimeInForce = z.enum(["GTT", "IOC", "FOK", "UNSPECIFIED"]); export const orderStatus = z.enum(["STANDBY", "OPEN", "PARTIALLY_FILLED_OPEN", "PARTIALLY_FILLED_CANCELED", "FILLED", "CANCELLED", "EXPIRED", "PARTIALLY_FILLED_EXPIRED", "UNSPECIFIED"]); export const selfTradePreventionType = z.enum(["TAKER", "MAKER", "BOTH", "UNSPECIFIED"]); export const openOrderResponse = z.object({orderHash: z.string(), clientOrderId: z.string().optional(), symbol: z.string(), accountAddress: z.string(), signerAddress: z.string(), priceE9: z.string(), quantityE9: z.string(), side: orderSide, leverageE9: z.string(), isIsolated: z.boolean(), salt: z.string(), expiresAtMillis: z.number().int(), signedAtMillis: z.number().int(), type: orderType, reduceOnly: z.boolean(), postOnly: z.boolean(), timeInForce: orderTimeInForce, triggerPriceE9: z.string().optional(), filledQuantityE9: z.string(), status: orderStatus, selfTradePreventionType: selfTradePreventionType, orderTimeAtMillis: z.number().int(), updatedAtMillis: z.number().int()}); export const getApiApiV1TradeStandbyOrdersResponse = z.array(openOrderResponse); 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 }; /** * /trade/standbyOrders * * GET /api/v1/trade/standbyOrders */ export const getApiApiV1TradeStandbyOrders = async (args: GetApiApiV1TradeStandbyOrdersArgs, options?: GetApiApiV1TradeStandbyOrdersOptions) => { const {baseUrl = "https://trade.api.sui-staging.bluefin.io", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.symbol != null) query.append("symbol", String(args.symbol)) if (bearerAuth !== undefined) headers.set("Authorization", bearerAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/trade/standbyOrders${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(getApiApiV1TradeStandbyOrdersResponse, await res.json()) };