import {z} from 'zod' export type PaymentCustomRateRequest = {rate: number, base_currency: string, counter_currency: string, rate_type: string}; export type OrchestrationPaymentRequest = {sender_end_to_end_id: string, internal_id?: string | undefined, orchestration_template?: string | undefined, 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?: PaymentCustomRateRequest | 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, user_info: Record, sender_segregated_account?: string | undefined, quote_limit?: number | undefined, quote_filter_types?: (Array<'LIQUIDITY_WARNINGS' | 'QUOTE_ERRORS'>) | undefined}; export type CreateApiV4OrchestrationPaymentArgs = {body: OrchestrationPaymentRequest}; export type CreateApiV4OrchestrationPaymentOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const orchestrationPaymentSubmitResponse = z.object({sender_end_to_end_id: z.string().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 orchestration payment * * POST /v4/orchestration/payment */ export const createApiV4OrchestrationPayment = async (args: CreateApiV4OrchestrationPaymentArgs, options: CreateApiV4OrchestrationPaymentOptions) => { 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(/\/+$/, '')}/v4/orchestration/payment${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(orchestrationPaymentSubmitResponse, await res.json()) };