import {z} from 'zod' export type PaymentQuoteCustomRateRequest = {rate: number, base_currency: string, counter_currency: string, rate_type: 'buy' | 'sell'}; export type PaymentQuoteRequest = {sending_address: string, receiving_address: string, amount: string, currency: string, quote_type: 'SENDER_AMOUNT' | 'RECEIVER_AMOUNT' | 'SENDER_INSTITUTION_AMOUNT' | 'RECEIVER_INSTITUTION_AMOUNT', sender_or_receiver_currency?: string | undefined, custom_fee?: number | undefined, custom_rate?: PaymentQuoteCustomRateRequest | undefined, quote_route?: Array | undefined, payment_method?: string | undefined, enable_quote_per_payout_method?: boolean | undefined, digital_asset_origination?: boolean | undefined, force_path_finding_and_liquidity_path_finding?: boolean | undefined, payout_method_category?: ('REAL_TIME_GROSS_SETTLEMENT_SYSTEM' | 'REAL_TIME_NET_SETTLEMENT_SYSTEM' | 'MASS_NET_PAYMENT_SYSTEM' | 'BOOK_TRANSFER' | 'CASH_PAYOUT' | 'WALLET_PAYMENT' | 'OTHER') | undefined, sender_segregated_account?: string | undefined, quote_limit?: number | undefined, quote_filter_types?: (Array<'LIQUIDITY_WARNINGS' | 'QUOTE_ERRORS'>) | undefined}; export type CreateApiQuoteCollectionsArgs = {body: PaymentQuoteRequest}; export type CreateApiQuoteCollectionsOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const fXRate = z.object({rate: z.number(), base_currency_code: z.string(), counter_currency_code: z.string(), type: z.string()}); export const quoteElement = z.object({quote_element_id: z.string(), quote_element_type: z.enum(["TRANSFER", "EXCHANGE", "EXCHANGE_TRADE", "CRYPTO_TRANSFER"]), quote_element_order: z.string(), sender_address: z.string(), receiver_address: z.string(), sending_amount: z.number(), receiving_amount: z.number(), sending_fee: z.number(), receiving_fee: z.number(), sending_currency_code: z.string().optional(), receiving_currency_code: z.string().optional(), fx_rate: fXRate.optional(), transfer_currency_code: z.string().optional()}); export const quote = z.object({quote_id: z.string(), created_at: z.string(), expires_at: z.string(), type: z.enum(["SENDER_AMOUNT", "RECEIVER_AMOUNT", "SENDER_INSTITUTION_AMOUNT", "RECEIVER_INSTITUTION_AMOUNT", "REVERSAL_AMOUNT"]), price_guarantee: z.string(), sender_address: z.string(), receiver_address: z.string(), amount: z.string(), currency_code: z.string(), currency_code_filter: z.string(), service_type: z.string(), quote_elements: z.array(quoteElement), liquidity_warning: z.string(), payment_method: z.string().optional(), payment_method_fields: z.string().optional(), payout_method_info: z.object({payout_method_name: z.string().optional(), payout_method_category: z.enum(["REAL_TIME_GROSS_SETTLEMENT_SYSTEM", "REAL_TIME_NET_SETTLEMENT_SYSTEM", "MASS_NET_PAYMENT_SYSTEM", "BOOK_TRANSFER", "CASH_PAYOUT", "WALLET_PAYMENT", "OTHER"]).optional(), description: z.string().optional(), estimated_time_to_credit: z.string().optional()}).optional()}); export const quotePathRos = z.object({source: z.string(), destination: z.string(), source_currency: z.string(), destination_currency: z.string(), account_liquidity_relationship_id: z.string().optional(), peer_liquidity_relationship_id: z.string().optional()}); export const quoteError = z.object({failed_path: z.array(quotePathRos), error_origin: z.string().optional(), error_message: z.string()}); export const quoteCollection = z.object({quote_collection_id: z.string(), quotes: z.array(quote), quote_errors: z.array(quoteError).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 quote collection * * POST /quote_collections */ export const createApiQuoteCollections = async (args: CreateApiQuoteCollectionsArgs, options: CreateApiQuoteCollectionsOptions) => { const {baseUrl, 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 (Bearer !== undefined) headers.set("Authorization", Bearer) const url = `${baseUrl.replace(/\/+$/, '')}/quote_collections${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(quoteCollection, await res.json()) };