import {z} from 'zod' export type GetApiTimeseriesMarketImpliedVolatilityArgs = {markets: Array, start_time?: string | undefined, end_time?: string | undefined, start_inclusive?: boolean | undefined, end_inclusive?: boolean | undefined, timezone?: string | undefined, page_size?: number | undefined, paging_from?: ('start' | 'end') | undefined, limit_per_market?: number | undefined, granularity?: ('raw' | '1m' | '1h' | '1d') | undefined, pretty?: boolean | undefined, format?: ('json' | 'csv') | undefined, next_page_token?: string | undefined}; export type GetApiTimeseriesMarketImpliedVolatilityOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const marketId = z.string(); export const time = z.string(); export const impliedVolatilityTrade = z.string(); export const impliedVolatilityBid = z.string(); export const impliedVolatilityAsk = z.string(); export const impliedVolatilityMark = z.string(); export const databaseTime = z.string(); export const optionTickerExchangeTime = z.string(); export const marketImpliedVolatility = z.object({market: marketId, time: time, iv_trade: impliedVolatilityTrade.optional(), iv_bid: impliedVolatilityBid.optional(), iv_ask: impliedVolatilityAsk.optional(), iv_mark: impliedVolatilityMark.optional(), database_time: databaseTime, exchange_time: optionTickerExchangeTime.optional()}); export const nextPageToken = z.string(); export const nextPageUrl = z.string(); export const marketImpliedVolatilityResponse = z.object({data: z.array(marketImpliedVolatility), 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 implied volatility * * GET /timeseries/market-implied-volatility */ export const getApiTimeseriesMarketImpliedVolatility = async (args: GetApiTimeseriesMarketImpliedVolatilityArgs, options?: GetApiTimeseriesMarketImpliedVolatilityOptions) => { 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.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.granularity != null) query.append("granularity", String(args.granularity)) 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-implied-volatility${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(marketImpliedVolatilityResponse, await res.json()) };