import {z} from 'zod' export type Symbol = string; export type GetApiApiV1PositionClosedPnlArgs = {sub_account_address: string, symbol?: (Symbol | null) | undefined, start_time?: (string | null) | undefined, end_time?: (string | null) | undefined, limit?: (number | null) | undefined, cursor?: (string | null) | undefined}; export type GetApiApiV1PositionClosedPnlOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const execType = z.enum(["Trade", "AdlTrade", "Funding", "BustTrade", "Delivery", "Settle", "BlockTrade", "MovePosition", "FutureSpread"]); export const orderId = z.string(); export const orderType = z.enum(["Limit", "Market"]); export const side = z.enum(["Sell", "Buy"]); export const symbol = z.string(); export const closedPnl = z.object({avg_entry_price: z.string(), avg_exit_price: z.string(), close_fee: z.string(), closed_pnl: z.string(), closed_size: z.string(), created_time: z.string(), cum_entry_value: z.string(), cum_exit_value: z.string(), exec_type: execType, fill_count: z.string(), leverage: z.string(), open_fee: z.string(), order_id: orderId, order_price: z.string(), order_type: orderType, qty: z.string(), side: side, sub_account_address: z.string(), symbol: symbol, updated_time: z.string()}); export const getClosedPnlResponse = z.object({list: z.array(closedPnl), next_page_cursor: z.string()}); 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 Closed PnL * * GET /api/v1/position/closed-pnl */ export const getApiApiV1PositionClosedPnl = async (args: GetApiApiV1PositionClosedPnlArgs, options?: GetApiApiV1PositionClosedPnlOptions) => { 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.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/position/closed-pnl${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(getClosedPnlResponse, await res.json()) };