import {z} from 'zod' export type MarketSymbol = string; export type GetApiV1MarketsSymbolTickArgs = {symbol: MarketSymbol}; export type GetApiV1MarketsSymbolTickOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const dateTime = z.string(); export const timeStampAsString = z.string(); export const assetValue = z.string(); export const feeTierId = z.string(); export const ammData = z.object({feeTierId: feeTierId, bidSpreadFee: z.string(), askSpreadFee: z.string(), currentPrice: z.string()}); export const tick = z.object({createdAtDatetime: dateTime, createdAtTimestamp: timeStampAsString, high: assetValue, low: assetValue, bestBid: assetValue, bidIVPercentage: z.string().optional(), bidVolume: assetValue, bestAsk: assetValue, askIVPercentage: z.string().optional(), askVolume: assetValue, vwap: assetValue, open: assetValue, close: assetValue, last: assetValue, change: assetValue, percentage: assetValue, average: assetValue, baseVolume: assetValue, quoteVolume: assetValue, bancorPrice: assetValue, markPrice: z.string().optional(), fundingRate: z.string().optional(), openInterest: z.string().optional(), lastTradeDatetime: dateTime, lastTradeTimestamp: timeStampAsString, lastTradeQuantity: assetValue, ammData: z.array(ammData), delta: z.string().optional(), gamma: z.string().optional(), theta: z.string().optional(), vega: z.string().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 }; /** * Get Market Tick * * GET /v1/markets/{symbol}/tick */ export const getApiV1MarketsSymbolTick = async (args: GetApiV1MarketsSymbolTickArgs, options?: GetApiV1MarketsSymbolTickOptions) => { 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") const url = `${baseUrl.replace(/\/+$/, '')}/v1/markets/${encodeURIComponent(String(args.symbol))}/tick${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(tick, await res.json()) };