import {z} from 'zod' export type GetApiHolderV1DistributionArgs = {'x-chain'?: 'solana' | undefined, token_address: string, address_type?: ('wallet' | 'token_account') | undefined, mode?: ('percent' | 'top') | undefined, top_n?: number | undefined, min_percent?: number | undefined, max_percent?: number | undefined, include_list?: boolean | undefined, offset?: number | undefined, limit?: number | undefined}; export type GetApiHolderV1DistributionOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const tokenHolderDistributionResponse = z.object({success: z.boolean(), data: z.object({token_address: z.string(), mode: z.enum(["percent", "top"]), summary: z.object({wallet_count: z.number().int().optional(), total_holding: z.string().optional(), percent_of_supply: z.number().optional()}), holders: z.array(z.object({wallet: z.string().optional(), token_account: z.string().optional(), holding: z.string().optional(), percent_of_supply: z.number().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 }; /** * Token - Holder Distribution * * GET /holder/v1/distribution */ export const getApiHolderV1Distribution = async (args: GetApiHolderV1DistributionArgs, options?: GetApiHolderV1DistributionOptions) => { const {baseUrl = "https://public-api.birdeye.so", fetch: fetchFn = fetch, apiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['x-chain'] != null) headers.set("x-chain", String(args['x-chain'])) if (args.token_address != null) query.append("token_address", String(args.token_address)) if (args.address_type != null) query.append("address_type", String(args.address_type)) if (args.mode != null) query.append("mode", String(args.mode)) if (args.top_n != null) query.append("top_n", String(args.top_n)) if (args.min_percent != null) query.append("min_percent", String(args.min_percent)) if (args.max_percent != null) query.append("max_percent", String(args.max_percent)) if (args.include_list != null) query.append("include_list", String(args.include_list)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (apiKeyAuth !== undefined) headers.set("X-API-KEY", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/holder/v1/distribution${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(tokenHolderDistributionResponse, await res.json()) };