import {z} from 'zod' export type GetApiApiPublicV1LiveLiquidationHeatmapCoinArgs = {coin: string, buckets?: number | undefined, range?: number | undefined}; export type GetApiApiPublicV1LiveLiquidationHeatmapCoinOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const heatmapBucket = z.object({cumulativeLongNotional: z.number(), cumulativeShortNotional: z.number(), longNotionalAtRisk: z.number(), longPositions: z.number().int(), priceHigh: z.number(), priceLow: z.number(), shortNotionalAtRisk: z.number(), shortPositions: z.number().int(), totalNotionalAtRisk: z.number()}); export const liquidationHeatmapBody = z.object({$schema: z.string().optional(), buckets: z.array(heatmapBucket).nullable(), coin: z.string(), currentPrice: z.number(), timestamp: z.number().int(), totalLongAtRisk: z.number(), totalShortAtRisk: z.number()}); 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 }; /** * Inspect potential liquidation exposure by price * * GET /api/public/v1/live/liquidation-heatmap/{coin} */ export const getApiApiPublicV1LiveLiquidationHeatmapCoin = async (args: GetApiApiPublicV1LiveLiquidationHeatmapCoinArgs, options?: GetApiApiPublicV1LiveLiquidationHeatmapCoinOptions) => { 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.buckets != null) query.append("buckets", String(args.buckets)) if (args.range != null) query.append("range", String(args.range)) if (apiKey !== undefined) headers.set("X-API-Key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/public/v1/live/liquidation-heatmap/${encodeURIComponent(String(args.coin))}${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(liquidationHeatmapBody, await res.json()) };