import {z} from 'zod' export type GetApiDefiV3OhlcvPairArgs = {'x-chain'?: ('solana' | 'bsc' | 'base' | 'ethereum' | 'monad' | 'aptos' | 'fogo' | 'mantle' | 'hyperevm' | 'megaeth' | 'robinhood') | undefined, address: string, type: '1s' | '15s' | '30s' | '1m' | '3m' | '5m' | '15m' | '30m' | '1H' | '2H' | '4H' | '6H' | '8H' | '12H' | '1D' | '3D' | '1W' | '1M', time_from: number, time_to: number, mode?: ('range' | 'count') | undefined, count_limit?: number | undefined, padding?: boolean | undefined, outlier?: boolean | undefined, inversion?: boolean | undefined}; export type GetApiDefiV3OhlcvPairOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const defiOhlcvPairV3Response = z.object({success: z.boolean(), data: z.object({items: z.array(z.object({o: z.number(), h: z.number(), l: z.number(), c: z.number(), v: z.number(), v_usd: z.number(), address: z.string(), type: z.string(), unix_time: z.number().int(), currency: 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 }; /** * OHLCV V3 - Pair * * GET /defi/v3/ohlcv/pair */ export const getApiDefiV3OhlcvPair = async (args: GetApiDefiV3OhlcvPairArgs, options?: GetApiDefiV3OhlcvPairOptions) => { 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.address != null) query.append("address", String(args.address)) if (args.type != null) query.append("type", String(args.type)) if (args.time_from != null) query.append("time_from", String(args.time_from)) if (args.time_to != null) query.append("time_to", String(args.time_to)) if (args.mode != null) query.append("mode", String(args.mode)) if (args.count_limit != null) query.append("count_limit", String(args.count_limit)) if (args.padding != null) query.append("padding", String(args.padding)) if (args.outlier != null) query.append("outlier", String(args.outlier)) if (args.inversion != null) query.append("inversion", String(args.inversion)) if (apiKeyAuth !== undefined) headers.set("X-API-KEY", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/defi/v3/ohlcv/pair${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(defiOhlcvPairV3Response, await res.json()) };