import {z} from 'zod' export type ExternalOrder = {input_mint: string, output_mint: string, input_amount: string, output_amount: string, use_exact_output_amount: boolean, min_fill_size: string}; export type ExternalQuoteRequest = {external_order: ExternalOrder}; export type CreateApiV2ExternalMatchesGetQuoteArgs = {body: ExternalQuoteRequest}; export type CreateApiV2ExternalMatchesGetQuoteOptions = {baseUrl?: string | undefined, apiKeyHmac?: string | undefined, fetch?: typeof fetch | undefined}; export const externalOrder = z.object({input_mint: z.string(), output_mint: z.string(), input_amount: z.string(), output_amount: z.string(), use_exact_output_amount: z.boolean(), min_fill_size: z.string()}); export const fixedPoint = z.string(); export const apiTimestampedPriceFp = z.object({price: fixedPoint, timestamp: z.number().int()}); export const apiExternalMatchResult = z.object({input_mint: z.string(), output_mint: z.string(), input_amount: z.string(), output_amount: z.string(), price_fp: apiTimestampedPriceFp}); export const feeTake = z.object({relayer_fee: z.string(), protocol_fee: z.string()}); export const apiExternalAssetTransfer = z.object({mint: z.string(), amount: z.string()}); export const apiTimestampedPrice = z.object({price: z.string(), timestamp: z.number().int()}); export const apiExternalQuote = z.object({order: externalOrder, match_result: apiExternalMatchResult, fees: feeTake, send: apiExternalAssetTransfer, receive: apiExternalAssetTransfer, price: apiTimestampedPrice, timestamp: z.number().int()}); export const apiSignedQuote = z.object({quote: apiExternalQuote, signature: z.string(), deadline: z.number().int()}); export const gasSponsorshipInfo = z.object({refund_amount: z.number().int(), refund_native_eth: z.boolean(), refund_address: z.string().optional()}); export const externalQuoteResponse = z.object({signed_quote: apiSignedQuote, gas_sponsorship_info: gasSponsorshipInfo.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 }; /** * POST /v2/external-matches/get-quote */ export const createApiV2ExternalMatchesGetQuote = async (args: CreateApiV2ExternalMatchesGetQuoteArgs, options?: CreateApiV2ExternalMatchesGetQuoteOptions) => { const {baseUrl = "https://api.renegade.fi", fetch: fetchFn = fetch, apiKeyHmac} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKeyHmac !== undefined) headers.set("X-Renegade-Auth", apiKeyHmac) const url = `${baseUrl.replace(/\/+$/, '')}/v2/external-matches/get-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(externalQuoteResponse, await res.json()) };