import {z} from 'zod' export type TGMHoldersChain = 'arbitrum' | 'avalanche' | 'base' | 'bitcoin' | 'bnb' | 'ethereum' | 'hyperevm' | 'injective' | 'iotaevm' | 'linea' | 'mantle' | 'mantra' | 'monad' | 'near' | 'optimism' | 'plasma' | 'polygon' | 'robinhood' | 'sei' | 'solana' | 'sonic' | 'starknet' | 'sui' | 'ton' | 'tron'; export type TGMHoldersLabel = 'whale' | 'public_figure' | 'smart_money' | 'all_holders' | 'exchange'; export type PaginationRequest = {page?: number | undefined, per_page?: number | undefined}; export type LabelType = '30D Smart Trader' | '90D Smart Trader' | '180D Smart Trader' | 'Fund' | 'Smart Trader' | 'Public Figure' | 'Exchange' | 'Whale' | 'BananaGun Bot User' | 'Top Maestro Bot User' | 'Top BananaGun Bot User' | 'Maestro Bot User' | 'Early MAGIC Miner' | 'First Mover LP' | 'First Mover Staking' | 'Profitable LP' | 'Smart HL Perps Trader'; export type NumericRangeFilter = {min?: number | undefined, max?: number | undefined}; export type TGMHoldersFilters = {include_smart_money_labels?: Array | undefined, exclude_smart_money_labels?: Array | undefined, address?: (string | Array | null) | undefined, address_label?: (string | Array | null) | undefined, token_amount?: NumericRangeFilter | undefined, total_outflow?: NumericRangeFilter | undefined, total_inflow?: NumericRangeFilter | undefined, balance_change_24h?: NumericRangeFilter | undefined, balance_change_7d?: NumericRangeFilter | undefined, balance_change_30d?: NumericRangeFilter | undefined, ownership_percentage?: NumericRangeFilter | undefined, value_usd?: NumericRangeFilter | undefined}; export type TGMHoldersSortField = 'address' | 'name' | 'token_amount' | 'total_outflow' | 'total_inflow' | 'balance_change_24h' | 'balance_change_7d' | 'balance_change_30d' | 'ownership_percentage' | 'value_usd'; export type SortDirection = 'ASC' | 'DESC'; export type SortOrderTGMHoldersSortField = {field: TGMHoldersSortField, direction: SortDirection}; export type TGMHoldersRequest = {chain: TGMHoldersChain, token_address: string, aggregate_by_entity?: boolean | undefined, label_type?: TGMHoldersLabel | undefined, pagination?: PaginationRequest | undefined, filters?: TGMHoldersFilters | undefined, premium_labels?: boolean | undefined, order_by?: Array | undefined}; export type CreateApiApiV1TgmHoldersArgs = {body: TGMHoldersRequest}; export type CreateApiApiV1TgmHoldersOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const tGMHolder = z.object({address: z.string().optional(), address_label: z.string().optional(), token_amount: z.number().optional(), total_outflow: z.number().optional(), total_inflow: z.number().optional(), balance_change_24h: z.number().optional(), balance_change_7d: z.number().optional(), balance_change_30d: z.number().optional(), ownership_percentage: z.number().optional(), value_usd: 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 tGMHoldersResponse = z.object({data: z.array(tGMHolder), pagination: paginationInfo, warnings: z.array(z.string()).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 "Token God Mode" (TGM) holders data * * POST /api/v1/tgm/holders */ export const createApiApiV1TgmHolders = async (args: CreateApiApiV1TgmHoldersArgs, options?: CreateApiApiV1TgmHoldersOptions) => { 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/tgm/holders${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(tGMHoldersResponse, await res.json()) };