import {z} from 'zod' export type GetApiApiV1WhaleTradesHistoryArgs = {limit?: number | undefined, cursor?: string | undefined, min_size?: number | undefined, condition_id?: string | undefined, trader?: string | undefined, category?: string | undefined, min_grade?: ('S' | 'A' | 'B' | 'C' | 'D' | 'F') | undefined, suspicious_only?: boolean | undefined, platform?: ('polymarket' | 'all') | undefined, from?: string | undefined, to?: string | undefined, min_market_volume_share?: number | undefined, sort?: ('recent' | 'market_volume_share') | undefined, 'If-None-Match'?: string | undefined}; export type GetApiApiV1WhaleTradesHistoryOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const largeTrade = z.object({id: z.string(), traded_at: z.string(), size_usd: z.number(), side: z.enum(["BUY", "SELL"]), outcome: z.string().nullable(), token_id: z.string().nullable(), price: z.number(), review_score: z.number(), signal_score: z.number(), recorded_review_score: z.number().nullable(), recorded_signal_score: z.number().nullable(), suspicion_score: z.number().int().gte(0).lte(100).nullable(), suspicion_track: z.enum(["whale", "fresh_conviction", "sliced_position"]).nullable(), market_volume_share: z.number().gte(0).lte(1).optional(), trader: z.object({id: z.string(), address: z.string(), username: z.string().optional(), grade: z.string().optional(), grade_at_trade: z.enum(["S", "A", "B", "C", "D", "F"]).nullable(), grade_at_trade_status: z.enum(["graded", "ungraded", "unknown"])}), market: z.object({id: z.string(), condition_id: z.string(), title: z.string(), slug: z.string().optional(), category: z.string().optional()})}); export const dataQualityGroup = z.object({group: z.string(), owner: z.string(), status: z.enum(["fresh", "partial", "unknown", "untracked", "unavailable"]), as_of: z.string().optional(), reason: z.string().optional()}); export const dataQuality = z.object({status: z.enum(["fresh", "partial", "unknown", "untracked", "unavailable"]), as_of: z.string().optional(), field_groups: z.array(dataQualityGroup)}); export const largeTradeHistoryMeta = z.object({request_id: z.string(), cached: z.boolean(), cache_age_s: z.number().int().optional(), source: z.object({kind: z.literal("local_replay"), table: z.literal("whale_alerts"), provider_fetch_at_request_time: z.literal(false)}), completeness: z.object({status: z.literal("best_effort"), reason: z.string()})}); export const getApiApiV1WhaleTradesHistoryResponse = z.object({object: z.literal("list"), data: z.array(largeTrade), data_quality: dataQuality, has_more: z.boolean(), next_cursor: z.string().optional(), total: z.number().int().optional(), meta: largeTradeHistoryMeta}); 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 }; /** * Replay historical whale trades * * GET /api/v1/whale-trades/history * * Also accepts auth: oauth2 */ export const getApiApiV1WhaleTradesHistory = async (args: GetApiApiV1WhaleTradesHistoryArgs, options?: GetApiApiV1WhaleTradesHistoryOptions) => { const {baseUrl = "https://api.0xinsider.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.min_size != null) query.append("min_size", String(args.min_size)) if (args.condition_id != null) query.append("condition_id", String(args.condition_id)) if (args.trader != null) query.append("trader", String(args.trader)) if (args.category != null) query.append("category", String(args.category)) if (args.min_grade != null) query.append("min_grade", String(args.min_grade)) if (args.suspicious_only != null) query.append("suspicious_only", String(args.suspicious_only)) if (args.platform != null) query.append("platform", String(args.platform)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.min_market_volume_share != null) query.append("min_market_volume_share", String(args.min_market_volume_share)) if (args.sort != null) query.append("sort", String(args.sort)) if (args['If-None-Match'] != null) headers.set("If-None-Match", String(args['If-None-Match'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/whale-trades/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(getApiApiV1WhaleTradesHistoryResponse, await res.json()) };