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 SourceResourceType = 'VAULT' | 'WALLET'; export type SourceResourceDetails = {id: string, type: SourceResourceType}; export type TransferAMLQuestionnaire = {destinationType?: ('FINANCIAL_INSTITUTION' | '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}; export type TransfersRequest = {amount: string, assetParametersATOM?: WithdrawalParametersForATOM | undefined, assetParametersCosmos?: WithdrawalParametersForCosmos | undefined, assetParametersExtra?: WithdrawalParametersExtra | undefined, assetParametersFiatTrustedDestination?: WithdrawalParametersForFiatTrustedDestination | undefined, assetParametersXLM?: WithdrawalParametersForXLM | undefined, assetParametersXRP?: WithdrawalParametersForXRP | undefined, assetType: string, deductFeeFromAmountIfSameType?: boolean | undefined, destination?: ResourceDetails | undefined, idempotentId?: string | undefined, source?: SourceResourceDetails | undefined, transferAmlQuestionnaire?: TransferAMLQuestionnaire | undefined, transferMemo?: (string | null) | undefined, useGasStation?: (boolean | null) | undefined}; export type CreateApiTransfersArgs = {'Api-Signature': string, 'Api-Timestamp': number, body: TransfersRequest}; export type CreateApiTransfersOptions = {baseUrl?: string | undefined, ApiAccessKey?: string | undefined, fetch?: typeof fetch | undefined}; export const transferStatus = z.enum(["IN_PROGRESS", "COMPLETED", "FAILED", "QUEUED"]); export const data3 = z.object({status: transferStatus, transferId: z.string()}); export const transfersResponse1 = z.object({data: data3}); 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 transfer request * * POST /transfers */ export const createApiTransfers = async (args: CreateApiTransfersArgs, options?: CreateApiTransfersOptions) => { 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(/\/+$/, '')}/transfers${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(transfersResponse1, await res.json()) };