import {z} from 'zod' export type GetApiApiPublicV1LiveRiskCoinsCoinHistoryArgs = {coin: string, hours?: number | undefined}; export type GetApiApiPublicV1LiveRiskCoinsCoinHistoryOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const riskAvailability = z.object({historicalClearinghouse: z.boolean(), liquidations: z.boolean(), liveCandles: z.boolean(), markPrices: z.boolean(), userActions: z.boolean()}); export const candlePoint = z.object({close: z.number(), high: z.number(), low: z.number(), open: z.number(), timestamp: z.number().int(), volume: z.number()}); export const riskFreshness = z.object({historicalClearinghouse: z.string().optional(), liquidations: z.string().optional(), liveCandles: z.string().optional(), livePositions: z.string().optional(), longShort: z.string().optional(), markPrices: z.string().optional()}); export const getLiquidationSummaryByCohortRow = z.object({count: z.number().int(), tier: z.string(), totalClosedPnl: z.unknown(), totalNotional: z.unknown(), wallets: z.number().int()}); export const getLiquidationSummaryByCoinRow = z.object({coin: z.string(), count: z.number().int(), totalClosedPnl: z.unknown(), totalNotional: z.unknown(), totalPenaltyFees: z.unknown()}); export const getLiquidationSummaryTimelineRow = z.object({bucketStart: z.number().int(), count: z.number().int(), totalClosedPnl: z.unknown(), totalNotional: z.unknown(), totalPenaltyFees: z.unknown()}); export const liquidationSummaryBody = z.object({$schema: z.string().optional(), availability: riskAvailability, avgClosedPnl: z.number(), avgPenaltyFee: z.number(), bucketSizeMs: z.number().int(), byCohort: z.array(getLiquidationSummaryByCohortRow).nullable().optional(), byCoin: z.array(getLiquidationSummaryByCoinRow).nullable(), coin: z.string().optional(), count: z.number().int(), freshness: riskFreshness, generatedAt: z.string(), longEvents: z.number().int(), longNotional: z.number(), shortEvents: z.number().int(), shortNotional: z.number(), since: z.string(), timeline: z.array(getLiquidationSummaryTimelineRow).nullable(), totalClosedPnl: z.number(), totalNotional: z.number(), totalPenaltyFees: z.number(), wallets: z.number().int()}); export const timestampValue = z.object({timestamp: z.number().int(), value: z.number()}); export const markDislocation = z.object({basisPct: z.number(), markPx: z.number(), oraclePx: z.number(), timestamp: z.number().int()}); export const oISnapshot = z.object({coin: z.string(), markPx: z.unknown(), openInterest: z.number(), timestamp: z.unknown()}); export const coinRiskHistoryBody = z.object({$schema: z.string().optional(), availability: riskAvailability, candleHistory: z.array(candlePoint).nullable(), cohortBiasHistory: z.unknown(), coin: z.string(), freshness: riskFreshness, generatedAt: z.string(), hours: z.number().int(), liquidations: liquidationSummaryBody, longShortHistory: z.array(timestampValue).nullable(), markDislocations: z.array(markDislocation).nullable(), oiHistory: z.array(oISnapshot).nullable()}); 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 API public v1 live risk coins by coin history * * GET /api/public/v1/live/risk/coins/{coin}/history */ export const getApiApiPublicV1LiveRiskCoinsCoinHistory = async (args: GetApiApiPublicV1LiveRiskCoinsCoinHistoryArgs, options?: GetApiApiPublicV1LiveRiskCoinsCoinHistoryOptions) => { const {baseUrl = "https://api.coinversa.ai", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.hours != null) query.append("hours", String(args.hours)) if (apiKey !== undefined) headers.set("X-API-Key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/public/v1/live/risk/coins/${encodeURIComponent(String(args.coin))}/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(coinRiskHistoryBody, await res.json()) };