import {z} from 'zod' export type GetApiV1TradeHistoryArgs = {account?: string | undefined, market_id?: unknown | undefined, start_time?: unknown | undefined, end_time?: unknown | undefined, page?: number | undefined, limit?: number | undefined, sorted_by?: string | undefined}; export type GetApiV1TradeHistoryOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const sharedOrderSide = z.enum(["BUY", "SELL"]); export const sharedLiquidityIndicator = z.enum(["TAKER", "MAKER"]); export const tradeBlockchainData = z.object({block_number: z.string().optional(), tx_hash: z.string().optional(), log_index: z.string().optional()}); export const v1ApiTrade = z.object({id: z.string().optional(), market_id: z.string().optional(), order_id: z.string().optional(), side: sharedOrderSide.optional(), price: z.string().optional(), size: z.string().optional(), fee: z.string().optional(), liquidity_indicator: sharedLiquidityIndicator.optional(), time: z.string().optional(), blockchain_data: tradeBlockchainData.optional(), is_liquidation: z.boolean().optional(), realized_pnl: z.string().optional(), leverage: z.string().optional(), margin_mode: z.number().int().optional(), realized_pnl_percentage: z.string().optional(), avg_price: z.string().optional(), position_side: sharedOrderSide.optional(), is_otc: z.boolean().optional(), client_order_id: z.string().optional()}); export const apiGetAccountTradeHistoryResponse = z.object({market_id: z.string().optional(), wallet_address: z.string().optional(), trades: z.array(v1ApiTrade).optional(), page: z.number().int().optional(), has_next_page: z.boolean().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 account trade history * * GET /v1/trade-history */ export const getApiV1TradeHistory = async (args: GetApiV1TradeHistoryArgs, options?: GetApiV1TradeHistoryOptions) => { 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)) if (args.sorted_by != null) query.append("sorted_by", String(args.sorted_by)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/trade-history${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(apiGetAccountTradeHistoryResponse, await res.json()) };