import {z} from 'zod' export type GetApiTimeseriesMarketOpeninterestArgs = {markets: Array, start_time?: string | undefined, end_time?: string | undefined, start_inclusive?: boolean | undefined, end_inclusive?: boolean | undefined, granularity?: ('raw' | '1m' | '1h' | '1d') | undefined, timezone?: string | undefined, page_size?: number | undefined, paging_from?: ('start' | 'end') | undefined, limit_per_market?: number | undefined, pretty?: boolean | undefined, format?: ('json' | 'csv') | undefined, next_page_token?: string | undefined}; export type GetApiTimeseriesMarketOpeninterestOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const marketId = z.string(); export const time = z.string(); export const contractCount = z.string(); export const contractValueUSD = z.string(); export const databaseTime = z.string(); export const openInterestExchangeTime = z.string(); export const marketOpenInterest = z.object({market: marketId, time: time, contract_count: contractCount, value_usd: contractValueUSD, database_time: databaseTime, exchange_time: openInterestExchangeTime.optional()}); export const marketOpenInterestDataArray = z.array(marketOpenInterest); export const nextPageToken = z.string(); export const nextPageUrl = z.string(); export const marketOpenInterestResponse = z.object({data: marketOpenInterestDataArray, 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 }; /** * Market open interest * * GET /timeseries/market-openinterest */ export const getApiTimeseriesMarketOpeninterest = async (args: GetApiTimeseriesMarketOpeninterestArgs, options?: GetApiTimeseriesMarketOpeninterestOptions) => { 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.start_time != null) query.append("start_time", String(args.start_time)) if (args.end_time != null) query.append("end_time", String(args.end_time)) if (args.start_inclusive != null) query.append("start_inclusive", String(args.start_inclusive)) if (args.end_inclusive != null) query.append("end_inclusive", String(args.end_inclusive)) if (args.granularity != null) query.append("granularity", String(args.granularity)) if (args.timezone != null) query.append("timezone", String(args.timezone)) 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.limit_per_market != null) query.append("limit_per_market", String(args.limit_per_market)) if (args.pretty != null) query.append("pretty", String(args.pretty)) if (args.format != null) query.append("format", String(args.format)) 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(/\/+$/, '')}/timeseries/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(marketOpenInterestResponse, await res.json()) };