import {z} from 'zod' export type GetApiV1AnalyticsOverviewArgs = {timeFrame?: ('24H' | '7D' | '30D') | undefined}; export type GetApiV1AnalyticsOverviewOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const getApiV1AnalyticsOverviewResponse = z.object({networkId: z.string(), contractAddress: z.string(), asOf: z.string(), timeFrame: z.enum(["24H", "7D", "30D"]), currentTvl: z.object({byPoolType: z.object({shielded: z.object({poolCount: z.number().int().gte(0), tokenTotals: z.array(z.object({tokenType: z.string(), amount: z.string()}))}), unshielded: z.object({poolCount: z.number().int().gte(0), tokenTotals: z.array(z.object({tokenType: z.string(), amount: z.string()}))}), mixed: z.object({poolCount: z.number().int().gte(0), tokenTotals: z.array(z.object({tokenType: z.string(), amount: z.string()}))}), stableUnshielded: z.object({poolCount: z.number().int().gte(0), tokenTotals: z.array(z.object({tokenType: z.string(), amount: z.string()}))})}), byPoolFamily: z.object({shielded: z.object({poolCount: z.number().int().gte(0), tokenTotals: z.array(z.object({tokenType: z.string(), amount: z.string()}))}), unshielded: z.object({poolCount: z.number().int().gte(0), tokenTotals: z.array(z.object({tokenType: z.string(), amount: z.string()}))}), mixed: z.object({poolCount: z.number().int().gte(0), tokenTotals: z.array(z.object({tokenType: z.string(), amount: z.string()}))}), total: z.object({poolCount: z.number().int().gte(0), tokenTotals: z.array(z.object({tokenType: z.string(), amount: z.string()}))})})}), estimatedTvl: z.object({quoteTokenType: z.string(), current: z.object({shielded: z.object({amount: z.string(), pricedPoolCount: z.number().int().gte(0), totalPoolCount: z.number().int().gte(0)}), unshielded: z.object({amount: z.string(), pricedPoolCount: z.number().int().gte(0), totalPoolCount: z.number().int().gte(0)}), mixed: z.object({amount: z.string(), pricedPoolCount: z.number().int().gte(0), totalPoolCount: z.number().int().gte(0)}), total: z.object({amount: z.string(), pricedPoolCount: z.number().int().gte(0), totalPoolCount: z.number().int().gte(0)})}), buckets: z.array(z.object({startTime: z.string(), endTime: z.string(), total: z.object({amount: z.string(), pricedPoolCount: z.number().int().gte(0), totalPoolCount: z.number().int().gte(0)}), shielded: z.object({amount: z.string(), pricedPoolCount: z.number().int().gte(0), totalPoolCount: z.number().int().gte(0)}), unshielded: z.object({amount: z.string(), pricedPoolCount: z.number().int().gte(0), totalPoolCount: z.number().int().gte(0)}), mixed: z.object({amount: z.string(), pricedPoolCount: z.number().int().gte(0), totalPoolCount: z.number().int().gte(0)})}))}), estimatedVolume: z.object({quoteTokenType: z.string(), selectedTimeFrame: z.object({total: z.object({amount: z.string(), pricedTradeCount: z.number().int().gte(0), totalTradeCount: z.number().int().gte(0)}), shielded: z.object({amount: z.string(), pricedTradeCount: z.number().int().gte(0), totalTradeCount: z.number().int().gte(0)}), unshielded: z.object({amount: z.string(), pricedTradeCount: z.number().int().gte(0), totalTradeCount: z.number().int().gte(0)}), mixed: z.object({amount: z.string(), pricedTradeCount: z.number().int().gte(0), totalTradeCount: z.number().int().gte(0)})}), last24h: z.object({total: z.object({amount: z.string(), pricedTradeCount: z.number().int().gte(0), totalTradeCount: z.number().int().gte(0)}), shielded: z.object({amount: z.string(), pricedTradeCount: z.number().int().gte(0), totalTradeCount: z.number().int().gte(0)}), unshielded: z.object({amount: z.string(), pricedTradeCount: z.number().int().gte(0), totalTradeCount: z.number().int().gte(0)}), mixed: z.object({amount: z.string(), pricedTradeCount: z.number().int().gte(0), totalTradeCount: z.number().int().gte(0)})}), cumulative: z.object({total: z.object({amount: z.string(), pricedTradeCount: z.number().int().gte(0), totalTradeCount: z.number().int().gte(0)}), shielded: z.object({amount: z.string(), pricedTradeCount: z.number().int().gte(0), totalTradeCount: z.number().int().gte(0)}), unshielded: z.object({amount: z.string(), pricedTradeCount: z.number().int().gte(0), totalTradeCount: z.number().int().gte(0)}), mixed: z.object({amount: z.string(), pricedTradeCount: z.number().int().gte(0), totalTradeCount: z.number().int().gte(0)})}), buckets: z.array(z.object({startTime: z.string(), endTime: z.string(), total: z.object({amount: z.string(), pricedTradeCount: z.number().int().gte(0), totalTradeCount: z.number().int().gte(0)}), shielded: z.object({amount: z.string(), pricedTradeCount: z.number().int().gte(0), totalTradeCount: z.number().int().gte(0)}), unshielded: z.object({amount: z.string(), pricedTradeCount: z.number().int().gte(0), totalTradeCount: z.number().int().gte(0)}), mixed: z.object({amount: z.string(), pricedTradeCount: z.number().int().gte(0), totalTradeCount: z.number().int().gte(0)})}))}), tradeVolume: z.object({selectedTimeFrame: z.object({total: z.object({tradeCount: z.number().int().gte(0), tokenTotals: z.array(z.object({tokenType: z.string(), amount: z.string()}))}), shielded: z.object({tradeCount: z.number().int().gte(0), tokenTotals: z.array(z.object({tokenType: z.string(), amount: z.string()}))}), unshielded: z.object({tradeCount: z.number().int().gte(0), tokenTotals: z.array(z.object({tokenType: z.string(), amount: z.string()}))}), mixed: z.object({tradeCount: z.number().int().gte(0), tokenTotals: z.array(z.object({tokenType: z.string(), amount: z.string()}))})}), last24h: z.object({total: z.object({tradeCount: z.number().int().gte(0), tokenTotals: z.array(z.object({tokenType: z.string(), amount: z.string()}))}), shielded: z.object({tradeCount: z.number().int().gte(0), tokenTotals: z.array(z.object({tokenType: z.string(), amount: z.string()}))}), unshielded: z.object({tradeCount: z.number().int().gte(0), tokenTotals: z.array(z.object({tokenType: z.string(), amount: z.string()}))}), mixed: z.object({tradeCount: z.number().int().gte(0), tokenTotals: z.array(z.object({tokenType: z.string(), amount: z.string()}))})}), cumulative: z.object({total: z.object({tradeCount: z.number().int().gte(0), tokenTotals: z.array(z.object({tokenType: z.string(), amount: z.string()}))}), shielded: z.object({tradeCount: z.number().int().gte(0), tokenTotals: z.array(z.object({tokenType: z.string(), amount: z.string()}))}), unshielded: z.object({tradeCount: z.number().int().gte(0), tokenTotals: z.array(z.object({tokenType: z.string(), amount: z.string()}))}), mixed: z.object({tradeCount: z.number().int().gte(0), tokenTotals: z.array(z.object({tokenType: z.string(), amount: z.string()}))})}), lastTradeAt: z.string().nullable(), buckets: z.array(z.object({startTime: z.string(), endTime: z.string(), tradeCount: z.number().int().gte(0), shieldedTradeCount: z.number().int().gte(0), unshieldedTradeCount: z.number().int().gte(0), mixedTradeCount: z.number().int().gte(0)}))}), limitations: z.array(z.object({title: z.string(), reason: z.string()}))}); 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 }; /** * Read live protocol analytics for the selected time frame, including TVL token breakdowns and trade counts. * * GET /v1/analytics/overview */ export const getApiV1AnalyticsOverview = async (args: GetApiV1AnalyticsOverviewArgs, options: GetApiV1AnalyticsOverviewOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.timeFrame != null) query.append("timeFrame", String(args.timeFrame)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/analytics/overview${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(getApiV1AnalyticsOverviewResponse, await res.json()) };