import {z} from 'zod' export type GetApiV1AssetsSymbolMarketArgs = {symbol: string}; export type GetApiV1AssetsSymbolMarketOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const market = z.object({primaryMarket: z.object({symbol: z.string().optional(), price: z.string().optional(), priceChange24h: z.string().optional(), priceChangePct24h: z.string().optional(), priceHistory24h: z.array(z.object({timestamp: z.number().optional(), price: z.string().optional()})).optional(), totalHolders: z.number().optional(), sharesMultiplier: z.string().optional(), tradableSessions: z.array(z.enum(["premarket", "regular", "postmarket", "overnight", "offhours"])).optional()}), underlyingMarket: z.object({ticker: z.string().optional(), name: z.string().optional(), price: z.string().optional(), priceHigh52w: z.string().optional(), priceLow52w: z.string().optional(), volume: z.string().optional(), averageVolume: z.string().optional(), sharesOutstanding: z.string().optional(), marketCap: z.string().optional()}).nullable(), constituentTokens: z.array(z.string()), timestamp: z.number()}); 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 for an Asset * * GET /v1/assets/{symbol}/market */ export const getApiV1AssetsSymbolMarket = async (args: GetApiV1AssetsSymbolMarketArgs, options?: GetApiV1AssetsSymbolMarketOptions) => { const {baseUrl = "https://api.gm.ondo.finance", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/assets/${encodeURIComponent(String(args.symbol))}/market${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(market, await res.json()) };