import {z} from 'zod' export type GetApiDefiV3LiquidityOhlcPairArgs = {'x-chain'?: 'solana' | undefined, address: string, time?: number | undefined, direction?: ('back' | 'forward') | undefined, count?: number | undefined}; export type GetApiDefiV3LiquidityOhlcPairOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const defiLiquidityOhlcPairResponse = z.object({success: z.boolean(), data: z.object({items: z.array(z.object({pair_address: z.string(), base_mint: z.string(), quote_mint: z.string(), timestamp: z.string(), unix_time: z.number().int(), open_liquidity_usd: z.number(), high_liquidity_usd: z.number(), low_liquidity_usd: z.number(), close_liquidity_usd: z.number(), open_base_balance: z.number(), high_base_balance: z.number(), low_base_balance: z.number(), close_base_balance: z.number(), open_quote_balance: z.number(), high_quote_balance: z.number(), low_quote_balance: z.number(), close_quote_balance: z.number(), open_base_amount_ui: z.number().optional(), high_base_amount_ui: z.number().optional(), low_base_amount_ui: z.number().optional(), close_base_amount_ui: z.number().optional(), open_quote_amount_ui: z.number().optional(), high_quote_amount_ui: z.number().optional(), low_quote_amount_ui: z.number().optional(), close_quote_amount_ui: z.number().optional(), base_open_price: z.number(), base_high_price: z.number(), base_low_price: z.number(), base_close_price: z.number(), quote_open_price: z.number(), quote_high_price: z.number(), quote_low_price: z.number(), quote_close_price: z.number()})), direction: z.enum(["next", "prev"]), limit: z.number().int(), next_cursor: z.number().int().nullable(), prev_cursor: z.number().int().nullable(), has_more: z.boolean()})}); 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 }; /** * Liquidity OHLC - Pair * * GET /defi/v3/liquidity/ohlc/pair */ export const getApiDefiV3LiquidityOhlcPair = async (args: GetApiDefiV3LiquidityOhlcPairArgs, options?: GetApiDefiV3LiquidityOhlcPairOptions) => { 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.time != null) query.append("time", String(args.time)) if (args.direction != null) query.append("direction", String(args.direction)) if (args.count != null) query.append("count", String(args.count)) if (apiKeyAuth !== undefined) headers.set("X-API-KEY", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/defi/v3/liquidity/ohlc/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(defiLiquidityOhlcPairResponse, await res.json()) };