import {z} from 'zod' export type CryptoNetwork = 'BITCOIN' | 'ETHEREUM' | 'BITCOIN_CASH' | 'LITECOIN' | 'SOLANA' | 'POLYGON_POS' | 'BASE' | 'ARBITRUM_ONE' | 'STELLAR' | 'INK' | 'XLAYER' | 'AVALANCHE' | 'DOGECOIN' | 'SUI' | 'ROBINHOOD' | 'BNB'; export type CryptoWithdrawalBeneficiaryPerson = {first_name: string, last_name: string}; export type CryptoWithdrawalBeneficiaryInstitution = {name: string}; export type CryptoWithdrawalBeneficiary = {person_details?: CryptoWithdrawalBeneficiaryPerson | undefined, institution_details?: CryptoWithdrawalBeneficiaryInstitution | undefined}; export type CreateCryptoWithdrawalRequest = {ref_id?: string | undefined, profile_id: string, identity_id?: string | undefined, destination_address: string, asset: string, balance_asset?: string | undefined, metadata?: {[key: string]: string} | undefined, account_id?: string | undefined, fee_id?: string | undefined, crypto_network: CryptoNetwork, amount?: string | undefined, total?: string | undefined, beneficiary?: CryptoWithdrawalBeneficiary | undefined, memo?: string | undefined}; export type CreateApiTransferCryptoWithdrawalsArgs = {body: CreateCryptoWithdrawalRequest}; export type CreateApiTransferCryptoWithdrawalsOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const transferDirection = z.enum(["CREDIT", "DEBIT"]); export const transferType = z.enum(["INTERNAL_TRANSFER_DEBIT", "INTERNAL_TRANSFER_CREDIT", "CRYPTO_DEPOSIT", "CRYPTO_WITHDRAWAL", "WIRE_DEPOSIT", "WIRE_WITHDRAWAL", "SEN_DEPOSIT", "SEN_WITHDRAWAL", "BANK_DEPOSIT", "BANK_WITHDRAWAL", "PAXOS_TRANSFER_DEBIT", "PAXOS_TRANSFER_CREDIT", "SIGNET_DEPOSIT", "SIGNET_WITHDRAWAL", "CBIT_WITHDRAWAL", "CBIT_DEPOSIT", "CUBIX_DEPOSIT", "CUBIX_WITHDRAWAL", "RTP_DEPOSIT"]); export const transferStatus = z.enum(["PENDING", "COMPLETED", "FAILED"]); export const secondaryStatusName = z.enum(["TRAVEL_RULE_INFO_REQUESTED", "TRAVEL_RULE_REJECTED", "CUSTOMER_REJECTED", "PENDING_CUSTOMER_APPROVAL"]); export const secondaryStatus = z.object({name: secondaryStatusName.optional(), detail: z.string().optional()}); export const missingTrustline = z.object({message: z.string().optional(), destination_address: z.string().optional(), asset: z.string().optional()}); export const maxRentExceeded = z.object({message: z.string().optional(), destination_address: z.string().optional()}); export const awaitingActionFrom = z.enum(["AWAITING_ACTION_FROM_UNSPECIFIED", "AWAITING_ACTION_FROM_CLIENT", "AWAITING_ACTION_FROM_PAXOS"]); export const pendingCustomerApproval = z.object({message: z.string().optional(), awaiting_action_from: awaitingActionFrom.optional()}); export const staleFeeQuote = z.object({message: z.string().optional()}); export const invalidDestinationAddress = z.object({message: z.string().optional(), destination_address: z.string().optional()}); export const transferStatusDetails = z.object({missing_trustline: missingTrustline.optional(), max_rent_exceeded: maxRentExceeded.optional(), pending_customer_approval: pendingCustomerApproval.optional(), stale_fee_quote: staleFeeQuote.optional(), invalid_destination_address: invalidDestinationAddress.optional()}); export const cryptoNetwork = z.enum(["BITCOIN", "ETHEREUM", "BITCOIN_CASH", "LITECOIN", "SOLANA", "POLYGON_POS", "BASE", "ARBITRUM_ONE", "STELLAR", "INK", "XLAYER", "AVALANCHE", "DOGECOIN", "SUI", "ROBINHOOD", "BNB"]); export const autoConversion = z.object({from_transfer_id: z.string().optional(), to_transfer_id: z.string().optional()}); export const transfer = z.object({id: z.string(), customer_id: z.string(), profile_id: z.string(), identity_id: z.string().optional(), ref_id: z.string().optional(), amount: z.string(), total: z.string(), fee: z.string(), asset: z.string(), balance_asset: z.string().optional(), direction: transferDirection, type: transferType, status: transferStatus, secondary_status: secondaryStatus.optional(), status_details: transferStatusDetails.optional(), created_at: z.string(), updated_at: z.string(), metadata: z.record(z.string(), z.string()).optional(), destination_address: z.string().optional(), crypto_network: cryptoNetwork.optional(), crypto_tx_hash: z.string().optional(), crypto_tx_index: z.string().optional(), account_id: z.string().optional(), auto_conversion: autoConversion.optional(), group_id: z.string().optional(), fiat_account_id: z.string().optional(), notional_value: z.string().optional(), memo: z.string().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 Crypto Withdrawal * * POST /transfer/crypto-withdrawals */ export const createApiTransferCryptoWithdrawals = async (args: CreateApiTransferCryptoWithdrawalsArgs, options?: CreateApiTransferCryptoWithdrawalsOptions) => { const {baseUrl = "https://api.paxos.com/v2", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/transfer/crypto-withdrawals${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(transfer, await res.json()) };