import {z} from 'zod' export type GetApiV2PnlLeaderboardKolsArgs = {sort?: ('total' | 'realized' | 'unrealized' | 'invested' | 'proceeds' | 'value' | 'trades' | 'tokens' | 'roi' | 'win_percentage' | 'last_trade') | undefined, direction?: ('asc' | 'desc') | undefined, limit?: number | undefined, cursor?: string | undefined}; export type GetApiV2PnlLeaderboardKolsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const pnlBlock = z.object({realized: z.number().nullable().optional(), realizedRaw: z.number().nullable().optional(), unrealized: z.number().nullable().optional(), total: z.number().nullable().optional()}); export const identity = z.object({name: z.string().nullable().optional(), twitter: z.string().nullable().optional(), avatar: z.string().nullable().optional(), type: z.string().nullable().optional(), tags: z.array(z.string()).optional(), platforms: z.array(z.string()).optional(), bot: z.object({name: z.string().nullable().optional(), avatar: z.string().nullable().optional()}).optional(), pool: z.object({program: z.string().nullable().optional(), poolAddress: z.string().nullable().optional()}).optional(), developer: z.object({token: z.string().nullable().optional(), via: z.array(z.string()).optional(), pools: z.array(z.string()).optional(), creationTx: z.string().nullable().optional(), createdAt: z.number().int().nullable().optional()}).optional(), hacker: z.object({label: z.string().nullable().optional()}).optional(), spamDusting: z.object({label: z.string().nullable().optional()}).optional(), exchange: z.object({name: z.string().nullable().optional()}).optional()}); export const pagination = z.object({hasMore: z.boolean(), nextCursor: z.string().nullable(), count: z.number().int(), total: z.number().int(), pnlMode: z.enum(["strict", "adjusted", "raw"]).optional(), enrich: z.array(z.string()).optional()}); export const getApiV2PnlLeaderboardKolsResponse = z.object({traders: z.array(z.object({wallet: z.string().optional(), pnl: pnlBlock.optional(), invested: z.number().nullable().optional(), proceeds: z.number().nullable().optional(), openPositions: z.object({cost: z.number().nullable().optional(), value: z.number().nullable().optional()}).optional(), counts: z.object({buys: z.number().int().optional(), sells: z.number().int().optional(), trades: z.number().int().optional(), tokensTraded: z.number().int().optional(), tokensHeldEver: z.number().int().optional()}).optional(), averages: z.object({buy: z.number().nullable().optional(), sell: z.number().nullable().optional()}).optional(), tokens: z.object({profitable: z.number().int().optional(), losing: z.number().int().optional(), closed: z.number().int().optional()}).optional(), winRate: z.number().nullable().optional(), roi: z.number().nullable().optional(), timing: z.object({firstTrade: z.number().int().nullable().optional(), lastTrade: z.number().int().nullable().optional()}).optional(), updatedAt: z.string().nullable().optional(), identity: identity.optional()})).optional(), pagination: pagination.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 Leaderboard * * GET /v2/pnl/leaderboard/kols */ export const getApiV2PnlLeaderboardKols = async (args: GetApiV2PnlLeaderboardKolsArgs, options?: GetApiV2PnlLeaderboardKolsOptions) => { 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.sort != null) query.append("sort", String(args.sort)) if (args.direction != null) query.append("direction", String(args.direction)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/pnl/leaderboard/kols${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(getApiV2PnlLeaderboardKolsResponse, await res.json()) };