import {z} from 'zod' export type FxPairDto = {sourceCurrency: string, sourceNetwork?: string | undefined, sourceRail?: string | undefined, destinationCurrency: string, destinationNetwork?: string | undefined, destinationRail?: string | undefined}; export type FxAmountDto = {value: string, side: 'SOURCE' | 'DESTINATION'}; export type CreateFxQuoteDto = {onBehalfOf: string, pair: FxPairDto, amount: FxAmountDto, maxSlippageBps?: number | undefined}; export type CreateApiV1FxQuotesArgs = {'X-API-Key'?: string | undefined, 'Idempotency-Key'?: string | undefined, body: CreateFxQuoteDto}; export type CreateApiV1FxQuotesOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const fxPairDto = z.object({sourceCurrency: z.string(), sourceNetwork: z.string().optional(), sourceRail: z.string().optional(), destinationCurrency: z.string(), destinationNetwork: z.string().optional(), destinationRail: z.string().optional()}); export const fxAmountDto = z.object({value: z.string(), side: z.enum(["SOURCE", "DESTINATION"])}); export const fxQuoteExpectedDepositDetailsDto = z.object({kind: z.enum(["CRYPTO_ADDRESS", "FIAT_INSTRUCTIONS_ON_ACCEPT"]), address: z.string().optional(), network: z.string().optional(), note: z.string()}); export const fxQuoteResponseDto = z.object({id: z.string(), pair: fxPairDto, amount: fxAmountDto, grossSource: z.string(), displayedDestination: z.string(), rate: z.string(), referenceRate: z.string().optional(), referenceRateAsOf: z.string().optional(), referenceProvider: z.literal("XE").optional(), expiresAt: z.string(), indicativeRateExpiresAt: z.string().optional(), quoteBinding: z.enum(["PERSISTED", "AT_ACCEPT"]).optional(), expectedDepositDetails: fxQuoteExpectedDepositDetailsDto.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 }; /** * Create a firm FX quote * * POST /v1/fx/quotes * * Also accepts auth: bearer */ export const createApiV1FxQuotes = async (args: CreateApiV1FxQuotesArgs, options?: CreateApiV1FxQuotesOptions) => { const {baseUrl = "https://sandbox.api.infinite.dev", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-API-Key'] != null) headers.set("X-API-Key", String(args['X-API-Key'])) if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/fx/quotes${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(fxQuoteResponseDto, await res.json()) };