import {z} from 'zod' export type TimeWindow = '1d' | '7d' | '30d' | 'all_time'; export type GetApiV2PolymarketMarketConditionIdSmartMoneyArgs = {condition_id: string, window?: TimeWindow | undefined, min_realized_pnl?: (number | null) | undefined, min_total_pnl?: (number | null) | undefined, min_roi?: (number | null) | undefined, min_win_rate?: (number | null) | undefined, min_profit_factor?: (number | null) | undefined, min_trades?: (number | null) | undefined, min_volume?: (number | null) | undefined}; export type GetApiV2PolymarketMarketConditionIdSmartMoneyOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const timeWindow = z.enum(["1d", "7d", "30d", "all_time"]); export const smartMoneyPositioning = z.object({condition_id: z.string(), title: z.string().nullable().optional(), market_slug: z.string().nullable().optional(), smart_wallet_count: z.number().int(), net_buyers: z.number().int(), net_sellers: z.number().int(), neutral: z.number().int(), net_buyers_pct: z.number(), total_smart_volume: z.number(), total_smart_buy_volume: z.number(), total_smart_sell_volume: z.number(), avg_smart_buy_price: z.number(), avg_smart_sell_price: z.number(), total_smart_realized_pnl: z.number(), total_smart_total_pnl: z.number(), avg_smart_roi: z.number(), avg_smart_win_rate: z.number()}); export const smartMoneyResponse = z.object({smart_wallet_criteria: z.object({}), window: timeWindow, positioning: smartMoneyPositioning}); 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 Smart Money Positioning * * GET /v2/polymarket/market/{condition_id}/smart-money */ export const getApiV2PolymarketMarketConditionIdSmartMoney = async (args: GetApiV2PolymarketMarketConditionIdSmartMoneyArgs, options?: GetApiV2PolymarketMarketConditionIdSmartMoneyOptions) => { const {baseUrl = "https://api.predexon.com", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.window != null) query.append("window", String(args.window)) if (args.min_realized_pnl != null) query.append("min_realized_pnl", String(args.min_realized_pnl)) if (args.min_total_pnl != null) query.append("min_total_pnl", String(args.min_total_pnl)) if (args.min_roi != null) query.append("min_roi", String(args.min_roi)) if (args.min_win_rate != null) query.append("min_win_rate", String(args.min_win_rate)) if (args.min_profit_factor != null) query.append("min_profit_factor", String(args.min_profit_factor)) if (args.min_trades != null) query.append("min_trades", String(args.min_trades)) if (args.min_volume != null) query.append("min_volume", String(args.min_volume)) if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/polymarket/market/${encodeURIComponent(String(args.condition_id))}/smart-money${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(smartMoneyResponse, await res.json()) };