import {z} from 'zod' export type GetApiV2OfframpsArgs = {userId?: string | undefined, limit?: string | undefined, createdBefore?: string | undefined, createdAfter?: string | undefined}; export type GetApiV2OfframpsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const offrampStatusEnum = z.enum(["NOT_INITIATED", "CREATED", "OPEN_QUOTE", "CRYPTO_INITIATED", "AWAITING_FUNDS", "CRYPTO_PENDING", "CRYPTO_PROCESSED", "FIAT_INITIATED", "FIAT_PENDING", "COMPLETED", "FIAT_FAILED", "CRYPTO_RETURNED", "CRYPTO_FAILED", "QUOTE_FAILED", "IN_REVIEW"]); export const offrampPaymentTrackingObject = z.object({fedBatchId: z.string().optional(), imad: z.string().optional(), omad: z.string().optional(), traceNumber: z.string().optional(), paymentRail: z.enum(["wire", "ach", "rtp", "swift"]).optional()}); export const offrampDepositInstructionsList = z.array(z.object({id: z.string().optional(), fromAddress: z.string().optional(), toAddress: z.string().optional(), currency: z.string().optional(), contractAddress: z.string().optional(), rawAmount: z.string().optional(), amount: z.number().optional(), chain: z.string().optional(), collected: z.boolean().optional(), collectable: z.boolean().optional(), expiresAt: z.string().optional(), transactionHash: z.string().nullable().optional()})); export const offrampRecordObject = z.object({transferType: z.string().optional(), transferDetails: z.object({id: z.string().optional(), requestId: z.string().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), status: offrampStatusEnum.optional(), failedReason: z.string().optional(), source: z.object({userId: z.string().optional(), chain: z.string().optional(), currency: z.string().optional(), amount: z.number().optional(), walletAddress: z.string().optional(), user: z.object({email: z.string().optional(), lastName: z.string().nullable().optional(), firstName: z.string().nullable().optional(), businessName: z.string().nullable().optional()}).optional()}).optional(), destination: z.object({userId: z.string().optional(), amount: z.number().optional(), currency: z.string().optional(), user: z.object({email: z.string().optional(), lastName: z.string().nullable().optional(), firstName: z.string().nullable().optional(), businessName: z.string().nullable().optional()}).optional(), accountId: z.string().optional(), sameDayAch: z.boolean().optional()}).optional(), receipt: z.object({transactionHash: z.string().nullable().optional(), paymentTracking: offrampPaymentTrackingObject.optional()}).optional(), developerFee: z.object({id: z.string().optional(), transactionHash: z.string().nullable().optional(), fees: z.array(z.object({type: z.enum(["PERCENTAGE", "FLAT"]), value: z.string(), amount: z.string(), walletAddress: z.string()})).optional()}).optional(), quoteInformation: z.object({sendGross: z.object({amount: z.string().optional(), currency: z.string().optional()}).optional(), sendNet: z.object({amount: z.string().optional(), currency: z.string().optional()}).optional(), receiveGross: z.object({amount: z.string().optional(), currency: z.string().optional()}).optional(), receiveNet: z.object({amount: z.string().optional(), currency: z.string().optional()}).optional(), rate: z.string().optional(), expiresAt: z.string().optional()}).optional(), depositInformation: offrampDepositInstructionsList.optional()}).optional()}); export const offrampAllRecordsObject = z.object({count: z.number().int().optional(), records: z.array(offrampRecordObject).optional(), nextCursor: 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 }; /** * List all offramps * * GET /v2/offramps */ export const getApiV2Offramps = async (args: GetApiV2OfframpsArgs, options?: GetApiV2OfframpsOptions) => { 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 (args.userId != null) query.append("userId", String(args.userId)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.createdBefore != null) query.append("createdBefore", String(args.createdBefore)) if (args.createdAfter != null) query.append("createdAfter", String(args.createdAfter)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/offramps${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(offrampAllRecordsObject, await res.json()) };