import {z} from 'zod' export type GetApiOfframpOrdersArgs = {cursor?: string | undefined, limit?: number | undefined, 'x-api-key'?: string | undefined, authorization?: string | undefined}; export type GetApiOfframpOrdersOptions = {baseUrl?: string | undefined, tenantApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const onrampReceiptStage = z.union([z.object({status: z.literal("not_started")}), z.object({status: z.literal("pending")}), z.object({status: z.literal("on_hold"), reason: z.enum(["kyc_required", "rfi_open", "review"])}), z.object({status: z.literal("processing")}), z.object({status: z.literal("completed")}), z.object({status: z.literal("failed"), reason: z.enum(["cancelled", "declined", "expired", "provider_error"])})]); export const onrampConversionStage = z.union([z.object({status: z.literal("not_started")}), z.object({status: z.literal("processing")}), z.object({status: z.literal("on_hold"), reason: z.enum(["rfi_open", "review"])}), z.object({status: z.literal("completed")}), z.object({status: z.literal("failed"), reason: z.enum(["sub_fee", "provider_error"])})]); export const offrampPayoutStage = z.union([z.object({status: z.literal("not_started")}), z.object({status: z.literal("pending")}), z.object({status: z.literal("on_hold"), reason: z.enum(["rfi_open", "review"])}), z.object({status: z.literal("processing")}), z.object({status: z.literal("submitted")}), z.object({status: z.literal("completed")}), z.object({status: z.literal("failed"), reason: z.enum(["sub_fee", "provider_error"])})]); export const onrampReturnStage = z.union([z.object({status: z.literal("none")}), z.object({status: z.literal("pending")}), z.object({status: z.literal("processing")}), z.object({status: z.literal("completed"), outcome: z.enum(["full_refund", "adjustment", "unresolved"])}), z.object({status: z.literal("failed")})]); export const offrampDirectPayment = z.object({kind: z.literal("direct"), from: z.string(), to: z.string(), chain: z.string(), token: z.string(), amount: z.string()}); export const offrampWithdrawalAccountPayment = z.object({kind: z.literal("withdrawal_account"), from: z.string(), to: z.string(), chain: z.string(), token: z.string(), amount: z.string()}); export const offrampPayment = z.discriminatedUnion("kind", [offrampDirectPayment, offrampWithdrawalAccountPayment]); export const offrampOrder = z.object({id: z.string(), provider: z.enum(["noah", "swapped"]), status: z.enum(["pending", "processing", "completed", "failed", "refunded"]), terminal: z.boolean(), stages: z.object({receipt: onrampReceiptStage, conversion: onrampConversionStage, delivery: offrampPayoutStage, return: onrampReturnStage}), rail: z.union([z.object({kind: z.literal("fiat"), method: z.string()}), z.object({kind: z.literal("exchange"), connection: z.string()})]), fiat: z.object({charged: z.object({total: z.string().nullable(), net: z.string().nullable(), currency: z.string()}).nullable(), eur: z.object({total: z.string().nullable(), net: z.string().nullable()}).nullable(), usd: z.object({total: z.string().nullable(), net: z.string().nullable()}).nullable()}), crypto: z.object({asset: z.string(), network: z.string(), amount: z.string()}).nullable(), providerStatus: z.string(), createdAt: z.string(), updatedAt: z.string(), sender: z.string(), payment: offrampPayment.nullable(), expiresAt: z.string()}); export const offrampOrderPage = z.object({items: z.array(offrampOrder), nextCursor: z.string().nullable()}); 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 }; /** * List offramp orders * * GET /offramp/orders * * Also accepts auth: internalProjectBearer, complianceSessionBearer */ export const getApiOfframpOrders = async (args: GetApiOfframpOrdersArgs, options?: GetApiOfframpOrdersOptions) => { const {baseUrl = "https://v1.orchestrator.rhinestone.dev/deposit-processor", fetch: fetchFn = fetch, tenantApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.limit != null) query.append("limit", String(args.limit)) if (args['x-api-key'] != null) headers.set("x-api-key", String(args['x-api-key'])) if (args.authorization != null) headers.set("authorization", String(args.authorization)) if (tenantApiKey !== undefined) headers.set("x-api-key", tenantApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/offramp/orders${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(offrampOrderPage, await res.json()) };