import {z} from 'zod' export type GetApiSubaccountsSubaccountIdFiatWithdrawalsArgs = {subaccountId: string, withdrawalStatuses?: (Array<'INITIATED' | 'FAILED' | 'PENDING_ENDORSEMENT' | 'PENDING_REVIEW' | 'BROADCASTING' | 'COMPLETED' | 'CANCELED'>) | undefined, startDate?: string | undefined, endDate?: string | undefined, afterId?: string | undefined, limit?: number | undefined}; export type GetApiSubaccountsSubaccountIdFiatWithdrawalsOptions = {baseUrl?: string | undefined, ApiAccessKey?: string | undefined, fetch?: typeof fetch | undefined}; export const assetAmount = z.object({amount: z.string(), assetType: z.string()}); export const destinationAddress = z.object({address: z.string().max(35), address2: z.string().max(35).optional(), city: z.string(), country: z.string().optional(), state: z.string(), zipcode: z.string()}); export const routingDetails = z.object({address: destinationAddress.optional(), code: z.enum(["ABA", "BIC", "DDA"]), id: z.string(), name: z.string().optional()}); export const withdrawalBankInfo = z.object({beneficiary: z.object({accountNumber: z.string().optional(), address: destinationAddress, name: z.string()}), financialInstitution: routingDetails.optional(), intermediateFinancialInstitution: routingDetails.optional(), memo: z.string().optional()}); export const listSubaccountWithdrawalsObject = z.object({asset: assetAmount.optional(), createdAt: z.string().optional(), customerDetails: withdrawalBankInfo.optional(), endedAt: z.string().nullable().optional(), transactionId: z.string().optional(), updatedAt: z.string().optional(), withdrawalStatus: z.enum(["INITIATED", "PENDING_ENDORSEMENT", "PENDING_REVIEW", "BROADCASTING", "COMPLETED", "FAILED", "CANCELED"]).optional()}); export const page = z.object({next: z.string().nullable().optional()}); export const listSubaccountWithdrawalsResponse = z.object({data: z.array(listSubaccountWithdrawalsObject), page: page.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 }; /** * Fetches a set of Withdrawals Requests for a subaccount * * GET /subaccounts/{subaccountId}/fiat/withdrawals */ export const getApiSubaccountsSubaccountIdFiatWithdrawals = async (args: GetApiSubaccountsSubaccountIdFiatWithdrawalsArgs, options?: GetApiSubaccountsSubaccountIdFiatWithdrawalsOptions) => { const {baseUrl = "https://api.anchorage-staging.com/v2", fetch: fetchFn = fetch, ApiAccessKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.withdrawalStatuses != null) query.append("withdrawalStatuses", args.withdrawalStatuses.join(',')) if (args.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) if (args.afterId != null) query.append("afterId", String(args.afterId)) if (args.limit != null) query.append("limit", String(args.limit)) if (ApiAccessKey !== undefined) headers.set("Api-Access-Key", ApiAccessKey) const url = `${baseUrl.replace(/\/+$/, '')}/subaccounts/${encodeURIComponent(String(args.subaccountId))}/fiat/withdrawals${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(listSubaccountWithdrawalsResponse, await res.json()) };