import {z} from 'zod' export type DateOnlyRange = {from?: string | undefined, to?: string | undefined}; export type PaginationRequest = {page?: number | undefined, per_page?: number | undefined}; export type NumericRangeFilter = {min?: number | undefined, max?: number | undefined}; export type SmartMoneyFilterType = 'Fund' | 'Smart Trader' | '30D Smart Trader' | '90D Smart Trader' | '180D Smart Trader' | 'Smart HL Perps Trader'; export type PerpLeaderboardFilters = {trader_address?: (string | Array | null) | undefined, trader_address_label?: (string | Array | null) | undefined, total_pnl?: NumericRangeFilter | undefined, roi?: NumericRangeFilter | undefined, account_value?: NumericRangeFilter | undefined, include_smart_money_labels?: Array | undefined, exclude_smart_money_labels?: Array | undefined}; export type PerpLeaderboardSortField = 'total_pnl' | 'roi' | 'account_value'; export type SortDirection = 'ASC' | 'DESC'; export type SortOrderPerpLeaderboardSortField = {field: PerpLeaderboardSortField, direction: SortDirection}; export type PerpLeaderboardRequest = {date: DateOnlyRange, pagination?: PaginationRequest | undefined, filters?: PerpLeaderboardFilters | undefined, premium_labels?: boolean | undefined, order_by?: Array | undefined}; export type CreateApiApiV1PerpLeaderboardArgs = {body: PerpLeaderboardRequest}; export type CreateApiApiV1PerpLeaderboardOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const perpLeaderboard = z.object({trader_address: z.string(), trader_address_label: z.string().optional(), total_pnl: z.number().optional(), roi: z.number().optional(), account_value: z.number().optional()}); export const paginationInfo = z.object({page: z.number().int().gte(1).optional(), per_page: z.number().int().gte(1).lte(1000).optional(), is_last_page: z.boolean().optional()}); export const perpLeaderboardResponse = z.object({data: z.array(perpLeaderboard), pagination: paginationInfo}); 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 Perpetual Trading Leaderboard Data * * POST /api/v1/perp-leaderboard */ export const createApiApiV1PerpLeaderboard = async (args: CreateApiApiV1PerpLeaderboardArgs, options?: CreateApiApiV1PerpLeaderboardOptions) => { const {baseUrl = "https://api.nansen.ai", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("apiKey", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/perp-leaderboard${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(perpLeaderboardResponse, await res.json()) };