import {z} from 'zod' export type GetApiTradingMarketdataArgs = {symbol: string, accountId?: string | undefined, subaccountId?: string | undefined, sizeBuckets?: Array | undefined, amountBuckets?: Array | undefined, depth?: number | undefined, priceIncrement?: string | undefined}; export type GetApiTradingMarketdataOptions = {baseUrl?: string | undefined, ApiAccessKey?: string | undefined, fetch?: typeof fetch | undefined}; export const priceLevel = z.object({amount: z.string().optional(), price: z.string(), size: z.string().optional()}); export const marketDataSnapshot = z.object({amountBuckets: z.array(z.string()).optional(), bids: z.array(priceLevel), offers: z.array(priceLevel), sizeBuckets: z.array(z.string()).optional(), spread: z.string().optional(), status: z.string().optional(), symbol: z.string(), timestamp: z.string()}); export const marketDataResponse = z.object({data: z.array(marketDataSnapshot)}); 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 data snapshot * * GET /trading/marketdata */ export const getApiTradingMarketdata = async (args: GetApiTradingMarketdataArgs, options?: GetApiTradingMarketdataOptions) => { const {baseUrl = "https://api.anchorage-staging.com/v2", fetch: fetchFn = fetch, ApiAccessKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.symbol != null) query.append("symbol", String(args.symbol)) if (args.accountId != null) query.append("accountId", String(args.accountId)) if (args.subaccountId != null) query.append("subaccountId", String(args.subaccountId)) if (args.sizeBuckets != null) query.append("sizeBuckets", args.sizeBuckets.join(',')) if (args.amountBuckets != null) query.append("amountBuckets", args.amountBuckets.join(',')) if (args.depth != null) query.append("depth", String(args.depth)) if (args.priceIncrement != null) query.append("priceIncrement", String(args.priceIncrement)) if (ApiAccessKey !== undefined) headers.set("Api-Access-Key", ApiAccessKey) const url = `${baseUrl.replace(/\/+$/, '')}/trading/marketdata${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(marketDataResponse, await res.json()) };