import {z} from 'zod' export type InstrumentName = string; export type Timestamp = number; export type GetApiPublicGetFundingRateHistoryArgs = {instrument_name: InstrumentName, start_timestamp: Timestamp, end_timestamp: Timestamp}; export type GetApiPublicGetFundingRateHistoryOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const timestamp = z.number().int(); export const price = z.number(); export const publicGetFundingRateHistoryResponse = z.object({jsonrpc: z.literal("2.0"), id: z.number().int().optional(), result: z.array(z.object({timestamp: timestamp.optional(), prev_index_price: price.optional(), index_price: price.optional(), interest_1h: z.number().optional(), interest_8h: z.number().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 }; /** * Retrieves hourly historical funding rate (interest rate) data for a PERPETUAL instrument over a specified time period. Funding rates are periodic payments exchanged between long and short positions in perpetual contracts. * * GET /public/get_funding_rate_history */ export const getApiPublicGetFundingRateHistory = async (args: GetApiPublicGetFundingRateHistoryArgs, options?: GetApiPublicGetFundingRateHistoryOptions) => { const {baseUrl = "https://test.deribit.com/api/v2", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.instrument_name != null) query.append("instrument_name", String(args.instrument_name)) if (args.start_timestamp != null) query.append("start_timestamp", String(args.start_timestamp)) if (args.end_timestamp != null) query.append("end_timestamp", String(args.end_timestamp)) const url = `${baseUrl.replace(/\/+$/, '')}/public/get_funding_rate_history${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(publicGetFundingRateHistoryResponse, await res.json()) };