import {z} from 'zod' export type CreateBatchCryptoTransfer = {requestId: string, currency: 'usdc', chain: 'POLYGON', source: {userId: string}, destination: {batch: Array<{userId?: string | undefined, walletAddress?: string | undefined, amount: string}>}, requireApproval?: boolean | undefined}; export type CreateApiV2WalletsTransfersBatchesArgs = {body: CreateBatchCryptoTransfer}; export type CreateApiV2WalletsTransfersBatchesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const batchCryptoTransferObject = z.object({transferType: z.string(), transferDetails: z.object({id: z.string(), requestId: z.string(), createdAt: z.string(), updatedAt: z.string(), chain: z.literal("POLYGON"), currency: z.literal("usdc"), contractAddress: z.string(), status: z.enum(["NOT_INITIATED", "CREATED", "INITIATED", "PENDING", "COMPLETED", "FAILED", "UNKNOWN"]), source: z.object({userId: z.string(), walletAddress: z.string(), walletType: z.enum(["INDIVIDUAL", "BUSINESS"])}), destination: z.object({batch: z.array(z.object({amount: z.string(), userId: z.string().optional()}))}), receipt: z.object({transactionHash: z.string(), userOpHash: z.string().optional(), approval: z.object({id: z.string().optional(), status: z.string().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), transferId: z.string().optional(), transferType: z.string().optional(), fromDashboard: z.boolean().optional(), initiatorProfileId: z.string().optional(), votes: z.array(z.object({approverId: z.string().optional(), vote: z.enum(["APPROVE", "REJECT"]).optional(), comment: z.string().optional(), createdAt: z.string().optional()})).optional()}).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 a batch crypto transfer * * POST /v2/wallets/transfers/batches */ export const createApiV2WalletsTransfersBatches = async (args: CreateApiV2WalletsTransfersBatchesArgs, options?: CreateApiV2WalletsTransfersBatchesOptions) => { const {baseUrl = "https://production.hifibridge.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/wallets/transfers/batches${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(batchCryptoTransferObject, await res.json()) };