import {z} from 'zod' export type BridgingRequestSource = {userId: string, chain: 'POLYGON' | 'ETHEREUM' | 'SOLANA', currency: 'usdc' | 'usdt'}; export type BridgingRequestDestination = {userId?: string | undefined, chain: 'POLYGON' | 'ETHEREUM' | 'BASE' | 'OPTIMISM' | 'ARBITRUM' | 'SOLANA', currency: 'usdc' | 'usdt', walletAddress?: string | undefined}; export type CreateBridgingRequest = {requestId: string, amount: number, source: BridgingRequestSource, destination: BridgingRequestDestination, requireApproval?: boolean | undefined}; export type CreateApiV2WalletsBridgesArgs = {body: CreateBridgingRequest}; export type CreateApiV2WalletsBridgesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const bridgeAssetTransferObject = 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(), amount: z.number().optional(), status: z.enum(["NOT_INITIATED", "OPEN_QUOTE", "CREATED", "SOURCE_INITIATED", "SOURCE_PENDING", "SOURCE_PROCESSED", "SOURCE_FAILED", "DESTINATION_INITIATED", "DESTINATION_PENDING", "COMPLETED", "DESTINATION_FAILED", "QUOTE_FAILED", "UNKNOWN"]).optional(), source: z.object({userId: z.string().optional(), walletAddress: z.string().optional(), chain: z.string().optional(), currency: z.string().optional()}).optional(), destination: z.object({userId: z.string().optional(), walletAddress: z.string().optional(), chain: z.string().optional(), currency: z.string().optional()}).optional(), receipt: z.object({transactionHash: z.string().optional(), operations: z.object({mint: z.object({transactionHash: z.string().nullable().optional(), userOpHash: z.string().nullable().optional(), chain: z.string().nullable().optional()}).optional(), burn: z.object({transactionHash: z.string().nullable().optional(), userOpHash: z.string().nullable().optional(), chain: z.string().nullable().optional()}).optional(), approve: z.object({transactionHash: z.string().nullable().optional(), userOpHash: z.string().nullable().optional(), chain: z.string().nullable().optional()}).optional()}).optional()}).optional(), failedReason: z.string().nullable().optional(), fee: z.number().nullable().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(), railFee: 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()}).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 bridging request * * POST /v2/wallets/bridges */ export const createApiV2WalletsBridges = async (args: CreateApiV2WalletsBridgesArgs, options?: CreateApiV2WalletsBridgesOptions) => { 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/bridges${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(bridgeAssetTransferObject, await res.json()) };