import {z} from 'zod' export type CreateTransferRequest = {sourceWalletId: string, destinationAddr: string, assetId: string, amount: string, externalId?: string | undefined, note?: string | undefined, metadata?: {[key: string]: string} | undefined}; export type CreateApiTransactionsArgs = {'Idempotency-Key'?: string | undefined, body: CreateTransferRequest}; export type CreateApiTransactionsOptions = {baseUrl?: string | undefined, BearerAPIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const transaction = z.object({id: z.string(), tenantId: z.string(), externalId: z.string().optional(), sourceVaultId: z.string(), sourceWalletId: z.string(), destinationAddr: z.string(), assetId: z.string(), amount: z.string(), fee: z.string().optional(), gasLimit: z.number().int().optional(), nonce: z.number().int().optional(), status: z.enum(["SUBMITTED", "QUEUED", "PENDING_AML_SCREENING", "PENDING_AUTHORIZATION", "APPROVED", "REJECTED", "PENDING_SIGNATURE", "SIGNED", "BROADCASTING", "CONFIRMING", "COMPLETED", "FAILED", "CANCELLED", "BLOCKED"]), failureReason: z.string().optional(), chainTxHash: z.string().optional(), blockNumber: z.number().int().optional(), confirmations: z.number().int().optional(), note: z.string().optional(), metadata: z.record(z.string(), z.string()).optional(), createdAt: z.string(), updatedAt: z.string().optional(), broadcastAt: z.string().optional(), confirmedAt: z.string().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 }; /** * Create a new transfer * * POST /transactions */ export const createApiTransactions = async (args: CreateApiTransactionsArgs, options?: CreateApiTransactionsOptions) => { const {baseUrl = "https://api.qustody.io/v1", fetch: fetchFn = fetch, BearerAPIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (BearerAPIKey !== undefined) headers.set('authorization', `Bearer ${BearerAPIKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/transactions${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(transaction, await res.json()) };