import {z} from 'zod' export type GetApiTokenV1FirstBuyersArgs = {'x-chain'?: 'solana' | undefined, token_address: string, offset?: number | undefined, limit?: number | undefined}; export type GetApiTokenV1FirstBuyersOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const tokenFirstBuyersResponse = z.object({success: z.boolean(), data: z.object({token_address: z.string().optional(), flow: z.literal("buy").optional(), page_summary: z.object({total_wallets: z.number().int().optional(), buy_more: z.number().int().optional(), hold: z.number().int().optional(), sell_partial: z.number().int().optional(), sell_all: z.number().int().optional()}).optional(), buyers: z.array(z.object({wallet_address: z.string().optional(), block_number: z.number().int().optional(), block_unix_time: z.number().int().optional(), first_buy_volume: z.number().optional(), first_buy_volume_usd: z.number().optional(), total_buy_volume: z.number().optional(), total_buy_volume_usd: z.number().optional(), initial_holding: z.number().optional(), current_holding: z.number().optional(), position_status: z.enum(["buy_more", "hold", "sell_partial", "sell_all"]).optional(), tags: z.array(z.enum(["bundler", "sniper", "insider", "dev", "smart_trader"])).optional()})).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 - First Buyers * * GET /token/v1/first-buyers */ export const getApiTokenV1FirstBuyers = async (args: GetApiTokenV1FirstBuyersArgs, options?: GetApiTokenV1FirstBuyersOptions) => { 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.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(/\/+$/, '')}/token/v1/first-buyers${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(tokenFirstBuyersResponse, await res.json()) };