import {z} from 'zod' export type CryptoTransferSource = {userId: string}; export type CryptoTransferDestination = {userId: string, walletAddress?: string | undefined}; export type CryptoTransferFee = {type: 'FIX' | 'PERCENT', value: number}; export type CreateCryptoTransfer = {requestId: string, currency?: ('usdc' | 'usdt' | 'usdHifi') | undefined, amount?: number | undefined, chain?: ('POLYGON' | 'ETHEREUM' | 'BASE' | 'SOLANA') | undefined, source: CryptoTransferSource, destination: CryptoTransferDestination, fee?: CryptoTransferFee | undefined, requireApproval?: boolean | undefined}; export type CreateApiV2WalletsTransfersArgs = {body: CreateCryptoTransfer}; export type CreateApiV2WalletsTransfersOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const cryptoToCryptoTransferObject = 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(), chain: z.string().optional(), currency: z.string().optional(), contractAddress: z.string().optional(), status: z.string().optional(), failedReason: z.string().optional(), source: z.object({userId: z.string().optional(), walletAddress: z.string().optional(), walletType: z.string().optional(), user: z.object({email: z.string().optional(), lastName: z.string().optional(), firstName: z.string().optional(), businessName: z.string().optional()}).optional()}).optional(), destination: z.object({userId: z.string().optional(), walletType: z.string().optional(), walletAddress: z.string().optional(), user: z.object({email: z.string().optional(), lastName: z.string().optional(), firstName: z.string().optional(), businessName: z.string().optional()}).optional()}).optional(), amount: z.number().optional(), amountIncludeDeveloperFee: z.number().optional(), receipt: z.object({transactionHash: z.string().optional(), userOpHash: z.string().optional()}).optional(), developerFee: z.object({feeId: z.string().optional(), feeType: z.string().optional(), feeAmount: z.number().optional(), feePercent: z.number().optional(), status: z.string().optional(), transactionHash: z.string().optional(), failedReason: z.string().optional()}).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()}).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 crypto transfer * * POST /v2/wallets/transfers */ export const createApiV2WalletsTransfers = async (args: CreateApiV2WalletsTransfersArgs, options?: CreateApiV2WalletsTransfersOptions) => { 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${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(cryptoToCryptoTransferObject, await res.json()) };