import {z} from 'zod' export type WithdrawalParametersForATOM = {memo: string}; export type WithdrawalParametersForCosmos = {memo: string}; export type WithdrawalParametersExtra = {value: string}; export type WithdrawalParametersForFiatTrustedDestination = {memo?: string | undefined, purpose: 'TRADING_SETTLEMENT' | 'LENDING' | 'INVESTMENT' | 'EXPENSES' | 'FEE_PAYMENT' | 'GRANT_PAYMENT' | 'PRIZE_AWARD' | 'GIFT' | 'REIMBURSEMENT' | 'REBALANCE' | 'INTRA_COMPANY'}; export type WithdrawalParametersForXLM = {memo: string}; export type WithdrawalParametersForXRP = {destinationTag: number}; export type ResourceType = 'VAULT' | 'WALLET' | 'ADDRESS' | 'STANDING_INSTRUCTION'; export type ResourceDetails = {id: string, type: ResourceType}; export type SourceResourceTypeRequest = 'VAULT' | 'WALLET' | 'SUBACCOUNT'; export type SourceResourceDetailsRequest = {customerId?: string | undefined, id: string, type: SourceResourceTypeRequest}; export type AMLQuestionnaire = {destinationType?: ('FINANCIAL_INSTITUTION' | 'UNHOSTED_WALLET' | 'SELFHOSTED_WALLET') | undefined, institutionCountry?: string | undefined, institutionName?: string | undefined, originatorCity?: string | undefined, originatorCountry?: string | undefined, originatorName?: string | undefined, originatorPostalCode?: string | undefined, originatorStateProvince?: string | undefined, originatorStreetAddress?: string | undefined, originatorType?: ('MY_ORGANIZATION' | 'MY_CLIENT') | undefined, purpose: 'TRADING_SETTLEMENT' | 'LENDING' | 'INVESTMENT' | 'EXPENSES' | 'FEE_PAYMENT' | 'GRANT_PAYMENT' | 'PRIZE_AWARD' | 'GIFT' | 'REIMBURSEMENT' | 'REBALANCE' | 'INTRA_COMPANY' | 'REWARDS_DISTRIBUTION', recipientCity?: string | undefined, recipientCountry?: string | undefined, recipientFirstName?: string | undefined, recipientFullName?: string | undefined, recipientLastName?: string | undefined, recipientPostalCode?: string | undefined, recipientStateProvince?: string | undefined, recipientStreetAddress?: string | undefined, recipientType?: ('DESTINATION_INSTITUTION' | 'MY_ORGANIZATION' | 'PERSON' | 'ENTITY') | undefined, selfhostedDescription?: string | undefined, useTrustedDestinationAml?: (boolean | null) | undefined}; export type WithdrawalRequest = {amount: string, assetParametersATOM?: WithdrawalParametersForATOM | undefined, assetParametersCosmos?: WithdrawalParametersForCosmos | undefined, assetParametersExtra?: WithdrawalParametersExtra | undefined, assetParametersFiatTrustedDestination?: WithdrawalParametersForFiatTrustedDestination | undefined, assetParametersXLM?: WithdrawalParametersForXLM | undefined, assetParametersXRP?: WithdrawalParametersForXRP | undefined, assetType: string, description?: (string | null) | undefined, destination?: ResourceDetails | undefined, idempotentId?: (string | null) | undefined, source?: SourceResourceDetailsRequest | undefined, useGasStation?: (boolean | null) | undefined, withdrawalAmlQuestionnaire?: AMLQuestionnaire | undefined}; export type CreateApiTransactionsWithdrawalArgs = {'Api-Signature': string, 'Api-Timestamp': number, body: WithdrawalRequest}; export type CreateApiTransactionsWithdrawalOptions = {baseUrl?: string | undefined, ApiAccessKey?: string | undefined, fetch?: typeof fetch | undefined}; export const withdrawalDetails = z.object({withdrawalId: z.string()}); export const withdrawalResponse = z.object({data: withdrawalDetails}); 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 withdrawal request * * POST /transactions/withdrawal */ export const createApiTransactionsWithdrawal = async (args: CreateApiTransactionsWithdrawalArgs, options?: CreateApiTransactionsWithdrawalOptions) => { const {baseUrl = "https://api.anchorage-staging.com/v2", fetch: fetchFn = fetch, ApiAccessKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Api-Signature'] != null) headers.set("Api-Signature", String(args['Api-Signature'])) if (args['Api-Timestamp'] != null) headers.set("Api-Timestamp", String(args['Api-Timestamp'])) if (ApiAccessKey !== undefined) headers.set("Api-Access-Key", ApiAccessKey) const url = `${baseUrl.replace(/\/+$/, '')}/transactions/withdrawal${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(withdrawalResponse, await res.json()) };