import {z} from 'zod' export type GetApiGatewayV1MarketOnchainIndicatorArgs = {symbol: 'BTC' | 'ETH', metric: 'nupl' | 'sopr' | 'mvrv' | 'puell-multiple' | 'nvm' | 'nvt' | 'nvt-golden-cross' | 'exchange-flows/inflow' | 'exchange-flows/outflow' | 'exchange-flows/netflow' | 'exchange-flows/reserve', granularity?: 'day' | undefined, from?: string | undefined, to?: string | undefined, exchange?: ('all_exchange' | 'spot_exchange' | 'derivative_exchange' | 'coinbase_prime' | 'binance' | 'coinbase_advanced' | 'okx' | 'kraken' | 'bybit' | 'bitstamp' | 'gemini' | 'bitget' | 'kucoin' | 'bitfinex' | 'gate_io' | 'deribit' | 'mexc' | 'htx_global' | 'bitflyer' | 'coinone' | 'bithumb' | 'upbit' | 'hashkey_exchange') | undefined}; export type GetApiGatewayV1MarketOnchainIndicatorOptions = {baseUrl?: string | undefined, AccessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const marketMetricPoint = z.object({exchange: z.string().optional(), exchange_name: z.string().optional(), metric: z.string().optional(), project_id: z.string().optional(), symbol: z.string().optional(), timestamp: z.number().int(), value: z.number()}); export const offsetMeta = z.object({cached: z.boolean(), credits_used: z.number().int(), empty_reason: z.string().optional(), has_more: z.boolean().optional(), limit: z.number().int(), offset: z.number().int(), total: z.number().int().optional(), watermark: z.number().int().optional()}); export const dataResponseMarketMetricPoint = z.object({$schema: z.string().optional(), data: z.array(marketMetricPoint).nullable(), meta: offsetMeta}); 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 }; /** * On-Chain Indicator * * GET /gateway/v1/market/onchain-indicator */ export const getApiGatewayV1MarketOnchainIndicator = async (args: GetApiGatewayV1MarketOnchainIndicatorArgs, options?: GetApiGatewayV1MarketOnchainIndicatorOptions) => { const {baseUrl = "https://api.asksurf.ai", fetch: fetchFn = fetch, AccessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.symbol != null) query.append("symbol", String(args.symbol)) if (args.metric != null) query.append("metric", String(args.metric)) if (args.granularity != null) query.append("granularity", String(args.granularity)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.exchange != null) query.append("exchange", String(args.exchange)) if (AccessToken !== undefined) headers.set('authorization', `Bearer ${AccessToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/gateway/v1/market/onchain-indicator${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(dataResponseMarketMetricPoint, await res.json()) };