import {z} from 'zod' export type GetApiV2PnlLeaderboardKolsCalendarArgs = {year?: number | undefined, month?: number | undefined}; export type GetApiV2PnlLeaderboardKolsCalendarOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV2PnlLeaderboardKolsCalendarResponse = z.object({year: z.number().int().optional(), month: z.number().int().optional(), days: z.record(z.string(), z.object({realizedPnl: z.number().nullable().optional(), volume: z.number().nullable().optional(), buys: z.number().int().optional(), sells: z.number().int().optional(), traders: z.number().int().optional()})).optional(), summary: z.object({tradingDays: z.number().int().optional(), positiveDays: z.number().int().optional(), negativeDays: z.number().int().optional(), totalRealizedPnl: z.number().nullable().optional(), totalVolume: z.number().nullable().optional()}).optional()}); 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 KOL Calendar * * GET /v2/pnl/leaderboard/kols/calendar */ export const getApiV2PnlLeaderboardKolsCalendar = async (args: GetApiV2PnlLeaderboardKolsCalendarArgs, options?: GetApiV2PnlLeaderboardKolsCalendarOptions) => { const {baseUrl = "https://data.solanatracker.io", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.year != null) query.append("year", String(args.year)) if (args.month != null) query.append("month", String(args.month)) if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/pnl/leaderboard/kols/calendar${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(getApiV2PnlLeaderboardKolsCalendarResponse, await res.json()) };