import {z} from 'zod' export type GetApiTransfersArgs = {vaultId?: string | undefined, walletId?: string | undefined, transferIds?: Array | undefined, startDate?: string | undefined, endDate?: string | undefined, afterId?: string | undefined, limit?: number | undefined, statuses?: (Array<'IN_PROGRESS' | 'COMPLETED' | 'FAILED' | 'QUEUED'>) | undefined}; export type GetApiTransfersOptions = {baseUrl?: string | undefined, ApiAccessKey?: string | undefined, fetch?: typeof fetch | undefined}; export const amount = z.object({assetType: z.string(), currentPrice: z.string().optional(), currentUSDValue: z.string().optional(), quantity: z.string()}); export const resourceType = z.enum(["VAULT", "WALLET", "ADDRESS", "STANDING_INSTRUCTION"]); export const resourceDetails = z.object({id: z.string(), type: resourceType}); export const errorType = z.enum(["InternalError", "InvalidRequest", "Unauthenticated", "Forbidden", "NotFound", "Conflict", "UnprocessableEntity", "TooManyRequests", "ServiceUnavailable", "QuoteExpired", "InsufficientFunds", "NotImplemented"]); export const errorDetails = z.object({errorType: errorType, message: z.string()}); export const sourceResourceType = z.enum(["VAULT", "WALLET"]); export const sourceResourceDetails = z.object({id: z.string(), type: sourceResourceType}); export const transferStatus = z.enum(["IN_PROGRESS", "COMPLETED", "FAILED", "QUEUED"]); export const transferDetails = z.object({amount: amount, assetType: z.string(), blockchainTxId: z.string().optional(), createdAt: z.string(), destination: resourceDetails.optional(), endedAt: z.string().nullable().optional(), error: errorDetails.optional(), fee: amount.optional(), source: sourceResourceDetails.optional(), status: transferStatus, transferId: z.string(), transferMemo: z.string().nullable().optional()}); export const page = z.object({next: z.string().nullable().optional()}); export const transfersResponse = z.object({data: z.array(transferDetails), page: page}); 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 }; /** * List transfers * * GET /transfers */ export const getApiTransfers = async (args: GetApiTransfersArgs, options?: GetApiTransfersOptions) => { 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.vaultId != null) query.append("vaultId", String(args.vaultId)) if (args.walletId != null) query.append("walletId", String(args.walletId)) if (args.transferIds != null) query.append("transferIds", args.transferIds.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 (args.statuses != null) query.append("statuses", args.statuses.join(',')) if (ApiAccessKey !== undefined) headers.set("Api-Access-Key", ApiAccessKey) const url = `${baseUrl.replace(/\/+$/, '')}/transfers${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(transfersResponse, await res.json()) };