import {z} from 'zod' export type CreateApiV2UsersUserIdOrchestrationAddressesOrchestrationAddressIdOrchestrateArgs = {userId: string, orchestrationAddressId: string}; export type CreateApiV2UsersUserIdOrchestrationAddressesOrchestrationAddressIdOrchestrateOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const orchestrationBatchStatusEnum = z.enum(["PENDING", "PROCESSING", "COMPLETED", "FAILED"]); export const orchestrationBatchFailureReasonEnum = z.enum(["ADDRESS_NOT_ACTIVE", "NOT_INITIATED", "QUOTE_FAILED", "CRYPTO_FAILED", "FIAT_FAILED", "EXPIRED", "REJECTED", "CANCELLED"]); export const orchestrationBatchObject = z.object({id: z.string().optional(), orchestrationAddressId: z.string().optional(), totalAmount: z.string().optional(), currency: z.enum(["usdc", "usdt"]).optional(), status: orchestrationBatchStatusEnum.optional(), failureReason: orchestrationBatchFailureReasonEnum.optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), depositIds: z.array(z.string()).optional(), offrampTransactionId: z.string().nullable().optional()}); export const orchestrationManualTriggerTriggered = z.object({triggered: z.literal(true), batch: orchestrationBatchObject}); export const orchestrationManualTriggerReasonEnum = z.enum(["no_pending_deposits", "address_no_longer_active", "below_offramp_minimum"]); export const orchestrationManualTriggerNoOp = z.object({triggered: z.literal(false), reason: orchestrationManualTriggerReasonEnum, batch: z.object({}).nullable()}); export const orchestrationManualTriggerObject = z.union([orchestrationManualTriggerTriggered, orchestrationManualTriggerNoOp]); 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 }; /** * Manually trigger an orchestration * * POST /v2/users/{userId}/orchestration-addresses/{orchestrationAddressId}/orchestrate */ export const createApiV2UsersUserIdOrchestrationAddressesOrchestrationAddressIdOrchestrate = async (args: CreateApiV2UsersUserIdOrchestrationAddressesOrchestrationAddressIdOrchestrateArgs, options?: CreateApiV2UsersUserIdOrchestrationAddressesOrchestrationAddressIdOrchestrateOptions) => { const {baseUrl = "https://production.hifibridge.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/users/${encodeURIComponent(String(args.userId))}/orchestration-addresses/${encodeURIComponent(String(args.orchestrationAddressId))}/orchestrate${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(orchestrationManualTriggerObject, await res.json()) };