import {z} from 'zod' export type GetApiV1MetricsDefiTotalValueLockedArgs = {a: string, s?: number | undefined, u?: number | undefined, i?: string | undefined, f?: ('csv' | 'json') | undefined, timestamp_format?: ('unix' | 'humanized') | undefined}; export type GetApiV1MetricsDefiTotalValueLockedOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const timeSeriesDataPoint = z.object({t: z.number().int().optional(), v: z.union([z.number(), z.object({})]).optional(), o: z.record(z.string(), z.unknown()).optional()}); export const timeSeriesResponse = z.array(timeSeriesDataPoint); 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 }; /** * Total Value Locked in DeFi (TVL) * * GET /v1/metrics/defi/total_value_locked */ export const getApiV1MetricsDefiTotalValueLocked = async (args: GetApiV1MetricsDefiTotalValueLockedArgs, options?: GetApiV1MetricsDefiTotalValueLockedOptions) => { const {baseUrl = "https://api.glassnode.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.a != null) query.append("a", String(args.a)) if (args.s != null) query.append("s", String(args.s)) if (args.u != null) query.append("u", String(args.u)) if (args.i != null) query.append("i", String(args.i)) if (args.f != null) query.append("f", String(args.f)) if (args.timestamp_format != null) query.append("timestamp_format", String(args.timestamp_format)) if (ApiKeyAuth !== undefined) query.set("api_key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/metrics/defi/total_value_locked${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(timeSeriesResponse, await res.json()) };