import {z} from 'zod' export type TGMOHLCVChain = 'algorand' | 'aptos' | 'arbitrum' | 'avalanche' | 'base' | 'bitcoin' | 'bitlayer' | 'bnb' | 'chiliz' | 'ethereum' | 'gravity' | 'hyperevm' | 'hyperliquid' | 'injective' | 'iotaevm' | 'linea' | 'mantle' | 'mantra' | 'monad' | 'near' | 'optimism' | 'plasma' | 'polygon' | 'robinhood' | 'sei' | 'solana' | 'sonic' | 'stacks' | 'starknet' | 'stellar' | 'sui' | 'ton' | 'tron' | 'viction'; export type TokenOHLCVDateRange = {start: string, end: string}; export type DateRange = {from?: string | undefined, to?: string | undefined}; export type ResolutionEnum = '1m' | '5m' | '15m' | '30m' | '1h' | '4h' | '1d' | '1w' | '1M'; export type TokenOHLCVRequest = {chain: TGMOHLCVChain, token_address?: string | undefined, token_addresses?: Array | undefined, date_range?: TokenOHLCVDateRange | undefined, date?: DateRange | undefined, timeframe: ResolutionEnum}; export type CreateApiApiV1TgmTokenOhlcvArgs = {body: TokenOHLCVRequest}; export type CreateApiApiV1TgmTokenOhlcvOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const marketCapData = z.object({open: z.number().optional(), high: z.number().optional(), low: z.number().optional(), close: z.number().optional()}); export const oHLCVCandle = z.object({interval_start: z.string(), open: z.number().optional(), high: z.number().optional(), low: z.number().optional(), close: z.number().optional(), volume: z.number().optional(), volume_usd: z.number().optional(), market_cap: marketCapData}); export const tokenOHLCVResponse = z.object({chain: z.string(), token_address: z.string(), timeframe: z.string(), data: z.array(oHLCVCandle), truncated: z.boolean().optional(), truncation_note: z.string().optional()}); export const tokenOHLCVTokenData = z.object({token_address: z.string(), data: z.array(oHLCVCandle)}); export const tokenOHLCVBatchResponse = z.object({chain: z.string(), timeframe: z.string(), tokens: z.array(tokenOHLCVTokenData), truncated: z.boolean().optional(), truncation_note: z.string().optional()}); export const createApiApiV1TgmTokenOhlcvResponse = z.union([tokenOHLCVResponse, tokenOHLCVBatchResponse]); 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 }; /** * Retrieve token OHLCV candle data * * POST /api/v1/tgm/token-ohlcv */ export const createApiApiV1TgmTokenOhlcv = async (args: CreateApiApiV1TgmTokenOhlcvArgs, options?: CreateApiApiV1TgmTokenOhlcvOptions) => { const {baseUrl = "https://api.nansen.ai", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("apiKey", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/tgm/token-ohlcv${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiApiV1TgmTokenOhlcvResponse, await res.json()) };