import {z} from 'zod' export type GetApiApiPublicV1LiveWhaleTradesCoinArgs = {coin: string, since?: string | undefined, minNotional?: number | undefined, limit?: number | undefined}; export type GetApiApiPublicV1LiveWhaleTradesCoinOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const whaleTradesCoverage = z.object({completeness: z.string()}); export const whaleTradesSource = z.object({asOf: z.number().int(), cadence: z.string(), kind: z.string(), observedThrough: z.number().int().nullable(), table: z.string()}); export const whaleTradeParty = z.object({address: z.string(), direction: z.string(), pnlTier: z.string(), sizeTier: z.string()}); export const whaleTrade = z.object({buyer: whaleTradeParty, idKind: z.string(), notional: z.number(), price: z.number(), seller: whaleTradeParty, size: z.number(), tid: z.string(), time: z.number().int()}); export const whaleTradesBody = z.object({$schema: z.string().optional(), aggressor: z.string(), coin: z.string(), count: z.number().int(), coverage: whaleTradesCoverage, minNotional: z.number(), notes: z.array(z.string()).nullable(), ranking: z.string(), since: z.string(), source: whaleTradesSource, tierAssignment: z.string(), trades: z.array(whaleTrade).nullable(), truncated: z.boolean(), windowEnd: z.number().int(), windowStart: 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 whale trades by coin * * GET /api/public/v1/live/whale-trades/{coin} */ export const getApiApiPublicV1LiveWhaleTradesCoin = async (args: GetApiApiPublicV1LiveWhaleTradesCoinArgs, options?: GetApiApiPublicV1LiveWhaleTradesCoinOptions) => { 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.since != null) query.append("since", String(args.since)) if (args.minNotional != null) query.append("minNotional", String(args.minNotional)) if (args.limit != null) query.append("limit", String(args.limit)) if (apiKey !== undefined) headers.set("X-API-Key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/public/v1/live/whale-trades/${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(whaleTradesBody, await res.json()) };