import {z} from 'zod' export type GetQuoteRequest = {accountId?: (string | null) | undefined, currency: string, idempotentId?: (string | null) | undefined, quantity: string, quantityRounding?: boolean | undefined, side: 'BUY' | 'SELL' | 'TWOWAY', tradingPair: string}; export type CreateApiTradingQuoteArgs = {body: GetQuoteRequest}; export type CreateApiTradingQuoteOptions = {baseUrl?: string | undefined, ApiAccessKey?: string | undefined, fetch?: typeof fetch | undefined}; export const amount = z.object({assetType: z.string(), currentPrice: z.string().optional(), currentUSDValue: z.string().optional(), quantity: z.string()}); export const getQuoteRequest = z.object({accountId: z.string().nullable().optional(), currency: z.string(), idempotentId: z.string().max(128).nullable().optional(), quantity: z.string(), quantityRounding: z.boolean().optional(), side: z.enum(["BUY", "SELL", "TWOWAY"]), tradingPair: z.string()}); export const quoteStatus = z.enum(["OPEN", "REJECTED", "EXECUTED"]); export const quote = z.object({bidAmount: amount.optional(), bidPrice: z.string().optional(), offerAmount: amount.optional(), offerPrice: z.string().optional(), quoteID: z.string(), quoteRequest: getQuoteRequest, quoteStatus: quoteStatus, timestamp: z.string(), validUntilTime: z.string().nullable().optional()}); export const getQuoteResponse = z.object({data: quote}); 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 }; /** * Request a quote * * POST /trading/quote */ export const createApiTradingQuote = async (args: CreateApiTradingQuoteArgs, options?: CreateApiTradingQuoteOptions) => { const {baseUrl = "https://api.anchorage-staging.com/v2", fetch: fetchFn = fetch, ApiAccessKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiAccessKey !== undefined) headers.set("Api-Access-Key", ApiAccessKey) const url = `${baseUrl.replace(/\/+$/, '')}/trading/quote${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(getQuoteResponse, await res.json()) };