import {z} from 'zod' export type GetApiTokensTokenAddressHoldersArgs = {tokenAddress: string, enrich?: string | undefined}; export type GetApiTokensTokenAddressHoldersOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const enrichedWalletPnl = z.object({realized: z.number().optional(), unrealized: z.number().optional(), total: z.number().optional(), invested: z.number().optional(), proceeds: z.number().optional(), totalTrades: z.number().int().optional(), tokensTraded: z.number().int().optional()}); export const enrichedTokenPnl = z.object({realized: z.number().optional(), unrealized: z.number().optional(), total: z.number().optional(), invested: z.number().optional(), proceeds: z.number().optional(), buys: z.number().int().optional(), sells: z.number().int().optional(), totalTrades: z.number().int().optional(), roi: z.number().optional(), balance: z.number().optional(), costBasis: z.number().optional(), value: z.number().optional(), price: z.number().optional(), totalBought: z.number().optional(), totalSold: z.number().optional(), avgBuy: z.number().optional(), avgSell: z.number().optional(), firstBuy: z.number().int().nullable().optional(), lastBuy: z.number().int().nullable().optional(), firstSell: z.number().int().nullable().optional(), lastSell: z.number().int().nullable().optional(), firstTrade: z.number().int().nullable().optional(), lastTrade: z.number().int().nullable().optional(), avgCost: z.number().nullable().optional(), holdTimeSecs: z.number().int().nullable().optional()}); export const enrichedIdentity = z.object({name: z.string().optional(), twitter: z.string().optional(), avatar: z.string().optional(), type: z.string().optional(), tags: z.array(z.string()).optional(), platforms: z.array(z.string()).optional(), pool: z.object({program: z.string().optional(), poolAddress: z.string().optional()}).optional(), developer: z.object({token: z.string().optional(), via: z.array(z.string()).optional(), pools: z.array(z.string()).optional(), creationTx: z.string().optional(), createdAt: z.number().int().optional()}).optional(), bot: z.object({name: z.string().nullable().optional(), avatar: z.string().nullable().optional()}).optional()}); export const holderAccount = z.object({wallet: z.string().optional(), amount: z.number().optional(), value: z.object({quote: z.number().optional(), usd: z.number().optional()}).optional(), percentage: z.number().optional(), pnl: z.object({wallet: enrichedWalletPnl.optional(), token: enrichedTokenPnl.optional()}).optional(), identity: enrichedIdentity.optional()}); export const holdersResponse = z.object({total: z.number().int().optional(), enrich: z.array(z.string()).optional(), accounts: z.array(holderAccount).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 Holders (Top 100) * * GET /tokens/{tokenAddress}/holders */ export const getApiTokensTokenAddressHolders = async (args: GetApiTokensTokenAddressHoldersArgs, options?: GetApiTokensTokenAddressHoldersOptions) => { 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.enrich != null) query.append("enrich", String(args.enrich)) if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/tokens/${encodeURIComponent(String(args.tokenAddress))}/holders${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(holdersResponse, await res.json()) };