import {z} from 'zod' export type GetApiCatalogV2MarketOpeninterestArgs = {markets?: Array | undefined, exchange?: string | undefined, type?: ('spot' | 'future' | 'option') | undefined, base?: string | undefined, quote?: string | undefined, asset?: string | undefined, symbol?: string | undefined, format?: string | undefined, page_size?: number | undefined, paging_from?: ('start' | 'end') | undefined, pretty?: boolean | undefined, next_page_token?: string | undefined}; export type GetApiCatalogV2MarketOpeninterestOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const marketId = z.string(); export const minTime = z.string(); export const maxTime = z.string(); export const catalogMarketOpeninterestInfo = z.object({market: marketId, min_time: minTime, max_time: maxTime}); export const nextPageToken = z.string(); export const nextPageUrl = z.string(); export const catalogV2MarketOpeninterestResponse = z.object({data: z.array(catalogMarketOpeninterestInfo), next_page_token: nextPageToken.optional(), next_page_url: nextPageUrl.optional()}); 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 }; /** * Available market open interest * * GET /catalog-v2/market-openinterest */ export const getApiCatalogV2MarketOpeninterest = async (args: GetApiCatalogV2MarketOpeninterestArgs, options?: GetApiCatalogV2MarketOpeninterestOptions) => { const {baseUrl = "https://api.coinmetrics.io/v4", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.markets != null) query.append("markets", args.markets.join(',')) if (args.exchange != null) query.append("exchange", String(args.exchange)) if (args.type != null) query.append("type", String(args.type)) if (args.base != null) query.append("base", String(args.base)) if (args.quote != null) query.append("quote", String(args.quote)) if (args.asset != null) query.append("asset", String(args.asset)) if (args.symbol != null) query.append("symbol", String(args.symbol)) if (args.format != null) query.append("format", String(args.format)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.paging_from != null) query.append("paging_from", String(args.paging_from)) if (args.pretty != null) query.append("pretty", String(args.pretty)) if (args.next_page_token != null) query.append("next_page_token", String(args.next_page_token)) if (apiKey !== undefined) query.set("api_key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/catalog-v2/market-openinterest${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(catalogV2MarketOpeninterestResponse, await res.json()) };