import {z} from 'zod' export type GetApiDefiV3PriceStatsSingleArgs = {'x-chain'?: ('solana' | 'ethereum' | 'arbitrum' | 'avalanche' | 'bsc' | 'optimism' | 'polygon' | 'base' | 'zksync' | 'monad' | 'hyperevm' | 'aptos' | 'fogo' | 'mantle' | 'megaeth' | 'robinhood' | 'sui') | undefined, list_timeframe?: string | undefined, address: string, ui_amount_mode?: ('raw' | 'scaled' | 'both') | undefined}; export type GetApiDefiV3PriceStatsSingleOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const defiTokenPriceStatsWithTimeframesObject = z.object({address: z.string(), is_scaled_ui_token: z.boolean().optional(), multiplier: z.number().optional(), data: z.array(z.object({unix_time_update_price: z.number().int(), time_frame: z.string(), price: z.number(), price_change_percent: z.number(), high: z.number(), low: z.number(), scaled_price: z.number().optional(), scaled_high: z.number().optional(), scaled_low: z.number().optional()}))}); export const defiPriceStatsResponse = z.object({success: z.boolean(), data: z.array(defiTokenPriceStatsWithTimeframesObject)}); 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 }; /** * Price stats (Single) * * GET /defi/v3/price/stats/single */ export const getApiDefiV3PriceStatsSingle = async (args: GetApiDefiV3PriceStatsSingleArgs, options?: GetApiDefiV3PriceStatsSingleOptions) => { 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.list_timeframe != null) query.append("list_timeframe", String(args.list_timeframe)) if (args.address != null) query.append("address", String(args.address)) if (args.ui_amount_mode != null) query.append("ui_amount_mode", String(args.ui_amount_mode)) if (apiKeyAuth !== undefined) headers.set("X-API-KEY", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/defi/v3/price/stats/single${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(defiPriceStatsResponse, await res.json()) };