import {z} from 'zod' export type AssetSymbol = string; export type OptionExpiryDate = string; export type OptionType = 'CALL' | 'PUT'; export type OptionTypeOrExpiryDatetimeSortAsString = 'optionType' | 'expiryDatetime'; export type GetApiV1OptionLadderArgs = {baseSymbol: AssetSymbol, expiry?: OptionExpiryDate | undefined, type?: OptionType | undefined, sort?: OptionTypeOrExpiryDatetimeSortAsString | undefined}; export type GetApiV1OptionLadderOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const optionMarketSymbol = z.string(); export const assetSymbol = z.string(); export const quoteAssetSymbol = z.string(); export const optionType = z.enum(["CALL", "PUT"]); export const dateTime = z.string(); export const optionGreeks = z.object({delta: z.string().optional(), gamma: z.string().optional(), theta: z.string().optional(), vega: z.string().optional()}); export const optionLadderResponse = z.object({symbol: optionMarketSymbol.optional(), baseSymbol: assetSymbol.optional(), settlementAssetSymbol: quoteAssetSymbol.optional(), bidQuantity: z.string().optional(), askQuantity: z.string().optional(), bidIVPercentage: z.string().optional(), askIVPercentage: z.string().optional(), bid: z.string().optional(), ask: z.string().optional(), underlyingPrice: z.string().optional(), optionStrikePrice: z.string().optional(), markPrice: z.string().optional(), quantity: z.string().optional(), openInterest: z.string().optional(), openInterestUSD: z.string().optional(), optionType: optionType.optional(), expiryDatetime: dateTime.optional(), greeks: optionGreeks.optional()}); export const getApiV1OptionLadderResponse = z.array(optionLadderResponse); 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 }; /** * Get Option Ladder by Base Symbol * * GET /v1/option-ladder */ export const getApiV1OptionLadder = async (args: GetApiV1OptionLadderArgs, options?: GetApiV1OptionLadderOptions) => { const {baseUrl = "https://api.exchange.bullish.com/trading-api", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.baseSymbol != null) query.append("baseSymbol", String(args.baseSymbol)) if (args.expiry != null) query.append("expiry", String(args.expiry)) if (args.type != null) query.append("type", String(args.type)) if (args.sort != null) query.append("sort", String(args.sort)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/option-ladder${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(getApiV1OptionLadderResponse, await res.json()) };