import {z} from 'zod' export type CreateWalletSwap = {requestId: string, source: {userId: string, chain: 'POLYGON' | 'ETHEREUM' | 'SOLANA', currency: 'usdc' | 'usdt', amount?: string | undefined}, destination: {userId?: string | undefined, externalWalletId?: string | undefined, walletAddress?: string | undefined, chain: 'POLYGON' | 'ETHEREUM' | 'SOLANA', currency: 'usdc' | 'usdt' | 'usdg', amount?: string | undefined}}; export type CreateApiV2WalletsSwapsArgs = {body: CreateWalletSwap}; export type CreateApiV2WalletsSwapsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const walletSwapSourceObject = z.object({currency: z.string().optional(), amount: z.number().optional(), chain: z.string().optional(), userId: z.string().optional()}); export const walletSwapDestinationObject = z.object({currency: z.string().optional(), userId: z.string().optional(), externalWalletId: z.string().nullable().optional(), amount: z.number().nullable().optional(), chain: z.string().optional(), walletAddress: z.string().optional()}); export const walletSwapAmountObject = z.object({amount: z.string().optional(), currency: z.string().optional()}); export const walletSwapQuoteInformationObject = z.object({sendGross: walletSwapAmountObject.optional(), sendNet: walletSwapAmountObject.optional(), receiveGross: walletSwapAmountObject.optional(), receiveNet: walletSwapAmountObject.optional(), rate: z.string().optional(), expiresAt: z.string().optional()}); export const walletSwapFeeObject = z.object({id: z.string().optional(), amount: z.number().optional(), currency: z.string().optional(), type: z.enum(["EXCHANGE_FEE", "GAS_FEE"]).optional(), timing: z.enum(["ON_INVOICE", "ON_TRANSACTION"]).optional()}); export const walletSwapBillingInformationObject = z.object({fees: z.array(walletSwapFeeObject).optional()}); export const walletSwapDetailsObject = z.object({id: z.string().optional(), requestId: z.string().optional(), status: z.enum(["PENDING", "COMPLETED", "FAILED", "CANCELLED"]).optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), source: walletSwapSourceObject.optional(), destination: walletSwapDestinationObject.optional(), error: z.string().nullable().optional(), errorDetails: z.string().nullable().optional(), quoteInformation: walletSwapQuoteInformationObject.optional(), billingInformation: walletSwapBillingInformationObject.optional()}); export const walletSwapObject = z.object({transferType: z.string().optional(), transferDetails: walletSwapDetailsObject.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 swap * * POST /v2/wallets/swaps */ export const createApiV2WalletsSwaps = async (args: CreateApiV2WalletsSwapsArgs, options?: CreateApiV2WalletsSwapsOptions) => { 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/swaps${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(walletSwapObject, await res.json()) };