import {z} from 'zod' export type GetExchangeOrdersExternalTransactionTypeEnum = 'ONRAMP' | 'OFFRAMP'; export type GetExchangeOrdersExternalStatusesEnum = 'INITIATED' | 'DEPOSITED' | 'CONFIRMED' | 'COMPLETED' | 'PENDING' | 'CANCELLED' | 'IN_DISPUTE' | 'AWAITING_ACCEPTANCE'; export type GetApiExchangeApiOrdersArgs = {from?: string | undefined, to?: string | undefined, size?: number | undefined, page?: number | undefined, transactionType?: GetExchangeOrdersExternalTransactionTypeEnum | undefined, statuses?: Array | undefined, token?: string | undefined, fiat?: string | undefined, network?: string | undefined}; export type GetApiExchangeApiOrdersOptions = {baseUrl?: string | undefined, HMACAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const orderOfferDTO = z.object({offerId: z.string().optional(), partnerId: z.string(), transactionType: z.enum(["ONRAMP", "OFFRAMP"]), rate: z.number(), networkId: z.string(), fiatSymbol: z.string().optional(), fiatAmount: z.number().optional(), token: z.string(), tokenAmount: z.number(), partnerDisplayName: z.string().optional()}); export const walletDTO = z.object({walletAddress: z.string().optional(), networkId: z.string().optional(), walletName: z.string().optional()}); export const exchangeOrderDTO = z.object({id: z.string().optional(), cexOrderId: z.number().int().gte(1).optional(), apiKey: z.string().optional(), lastWebhookSentAt: z.string().optional(), orderId: z.string().optional(), reference: z.string().optional(), customerId: z.string().optional(), customerName: z.string().optional(), offer: orderOfferDTO.optional(), escrowAddress: z.string().optional(), paymentChannelId: z.string().optional(), paymentMethod: z.object({}).optional(), buyerPaymentMethod: z.object({}).optional(), customerWallet: walletDTO.optional(), tokenAddress: z.string().optional(), status: z.enum(["INITIATED", "DEPOSITED", "CONFIRMED", "COMPLETED", "PENDING", "CANCELLED", "IN_DISPUTE", "AWAITING_ACCEPTANCE"]).optional(), createdAt: z.string().optional(), initiatedAt: z.string().optional(), depositedAt: z.string().optional(), confirmedAt: z.string().optional(), completedAt: z.string().optional(), cancelledAt: z.string().optional(), lockHash: z.string().optional(), confirmationHash: z.string().optional(), cancellationHash: z.string().optional()}); export const exchangeOrdersResponse = z.object({orders: z.array(exchangeOrderDTO).optional(), pageNumber: z.number().int().optional(), pageSize: z.number().int().optional(), totalPages: z.number().int().optional(), totalOrders: z.number().int().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 }; /** * Retrieve list of exchange orders * * GET /exchange/api/orders */ export const getApiExchangeApiOrders = async (args: GetApiExchangeApiOrdersArgs, options?: GetApiExchangeApiOrdersOptions) => { const {baseUrl = "https://external.dev.onboardpay.co", fetch: fetchFn = fetch, HMACAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.size != null) query.append("size", String(args.size)) if (args.page != null) query.append("page", String(args.page)) if (args.transactionType != null) query.append("transactionType", String(args.transactionType)) if (args.statuses != null) query.append("statuses", args.statuses.join(',')) if (args.token != null) query.append("token", String(args.token)) if (args.fiat != null) query.append("fiat", String(args.fiat)) if (args.network != null) query.append("network", String(args.network)) if (HMACAuth !== undefined) headers.set("x-api-key", HMACAuth) const url = `${baseUrl.replace(/\/+$/, '')}/exchange/api/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(exchangeOrdersResponse, await res.json()) };