import {z} from 'zod' export type GetApiApiPublicV1LiveRiskOverviewOptions = {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 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 timestampLeverage = z.object({long: z.number().optional(), short: z.number().optional(), timestamp: z.number().int()}); export const getLiquidationSummaryTimelineRow = z.object({bucketStart: z.number().int(), count: z.number().int(), totalClosedPnl: z.unknown(), totalNotional: z.unknown(), totalPenaltyFees: z.unknown()}); export const timestampValue = z.object({timestamp: z.number().int(), value: z.number()}); export const riskOverviewHistory = z.object({avgLeverage: z.array(timestampLeverage).nullable(), liquidations: z.array(getLiquidationSummaryTimelineRow).nullable(), netBias: z.array(timestampValue).nullable(), oi: z.array(timestampValue).nullable()}); 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 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 lSTopPosition = z.object({address: z.string(), notional: z.number(), pnl: z.number(), size: z.number()}); export const coinLongShortBody = z.object({$schema: z.string().optional(), avgLongLeverage: z.number().optional(), avgLongSize: z.number(), avgShortLeverage: z.number().optional(), avgShortSize: z.number(), coin: z.string(), concentrationTop5: z.number(), longAccounts: z.number().int(), longUnrealizedPnl: z.number(), longVolume: z.number(), maxLeverage: z.number().optional(), netBias: z.number(), ratio: z.number(), sampleSize: z.number().int(), shortAccounts: z.number().int(), shortUnrealizedPnl: z.number(), shortVolume: z.number(), timestamp: z.number().int(), topLongs: z.array(lSTopPosition).nullable(), topShorts: z.array(lSTopPosition).nullable(), totalUnrealizedPnl: z.number(), volumeRatio: z.number()}); export const globalLongShortBody = z.object({$schema: z.string().optional(), accountRatio: z.number(), avgLongLeverage: z.number().optional(), avgLongSize: z.number(), avgShortLeverage: z.number().optional(), avgShortSize: z.number(), bySymbol: z.array(coinLongShortBody).nullable(), concentrationTop5: z.number(), longUnrealizedPnl: z.number(), maxLeverage: z.number().optional(), netBias: z.number(), sampleSize: z.number().int(), shortUnrealizedPnl: z.number(), timestamp: z.number().int(), totalLongAccounts: z.number().int(), totalLongVolume: z.number(), totalShortAccounts: z.number().int(), totalShortVolume: z.number(), totalUnrealizedPnl: z.number(), tradersTracked: z.number().int(), volumeRatio: z.number()}); export const riskOverviewCoin = z.object({avgLeverage: z.number().optional(), avgLiquidationDistancePct: z.number().optional(), coin: z.string(), concentrationTop5: z.number(), liquidationClosedPnl7d: z.number(), liquidationCount7d: z.number().int(), liquidationPenaltyFees7d: z.number(), longPositions: z.number().int(), nearLiquidationNotional: z.number(), netBias: z.number(), shortPositions: z.number().int(), totalOI: z.number(), totalUnrealizedPnl: z.number(), uniqueWallets: z.number().int()}); export const riskOverviewBody = z.object({$schema: z.string().optional(), availability: riskAvailability, avgLeverage: z.number().optional(), concentrationTop10: z.number(), freshness: riskFreshness, generatedAt: z.string(), history: riskOverviewHistory, liquidationSummary7d: liquidationSummaryBody, longPositions: z.number().int(), longShort: globalLongShortBody, nearLiquidationNotional: z.number(), netBias: z.number(), shortPositions: z.number().int(), topCrowdedCoins: z.array(riskOverviewCoin).nullable(), totalOI: z.number(), totalUnrealizedPnl: z.number(), uniqueWallets: z.number().int()}); 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 overview * * GET /api/public/v1/live/risk/overview */ export const getApiApiPublicV1LiveRiskOverview = async (options?: GetApiApiPublicV1LiveRiskOverviewOptions) => { 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 (apiKey !== undefined) headers.set("X-API-Key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/public/v1/live/risk/overview${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(riskOverviewBody, await res.json()) };