import {z} from 'zod' export type WithdrawRequest = {currency: string, amount: string, channel_type: 'crypto' | 'fiat', idempotency_key?: string | undefined, address?: string | undefined, address_book_id?: string | undefined, network?: string | undefined, address_tag?: string | undefined, memo?: string | undefined, recipient_id?: string | undefined, instant_pay?: boolean | undefined, account_holder_name?: string | undefined, bank_name?: string | undefined, bank_code?: string | undefined, branch_code?: string | undefined, account_number?: string | undefined, account_type?: ('Savings' | 'Current' | 'Cheque') | undefined, bank_reference?: string | undefined}; export type CreateApiWalletWithdrawArgs = {body: WithdrawRequest}; export type CreateApiWalletWithdrawOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, TimestampAuth?: string | undefined, SignatureAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const withdrawal = z.object({id: z.string().optional(), currency: z.string().optional(), amount: z.string().optional(), fee_amount: z.string().optional(), amount_after_fees: z.string().optional(), status: z.string().optional(), channel_type: z.enum(["crypto", "fiat"]).optional(), network_code: z.string().nullable().optional(), address: z.string().nullable().optional(), address_tag: z.string().nullable().optional(), tx_hash: z.string().nullable().optional(), requested_at: z.number().int().nullable().optional(), completed_at: z.number().int().nullable().optional(), created_at: z.number().int().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 }; /** * Submit withdrawal * * POST /wallet/withdraw */ export const createApiWalletWithdraw = async (args: CreateApiWalletWithdrawArgs, options?: CreateApiWalletWithdrawOptions) => { const {baseUrl = "https://api.altcointrader.co.za", fetch: fetchFn = fetch, ApiKeyAuth, TimestampAuth, SignatureAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("X-API-KEY", ApiKeyAuth) if (TimestampAuth !== undefined) headers.set("X-TIMESTAMP", TimestampAuth) if (SignatureAuth !== undefined) headers.set("X-SIGNATURE", SignatureAuth) const url = `${baseUrl.replace(/\/+$/, '')}/wallet/withdraw${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(withdrawal, await res.json()) };