import {z} from 'zod' export type ApplyWithdrawRequest = {request_id?: string | undefined, chain: string, token_type: string, source_currency?: string | undefined, amount: string, wallet_address: string, note?: string | undefined}; export type CreateApiWithdrawArgs = {'X-API-Key-ID': string, 'X-API-Timestamp': string, 'X-API-Nonce': string, Digest: string, Authorization: string, body: ApplyWithdrawRequest}; export type CreateApiWithdrawOptions = {baseUrl?: string | undefined, Ed25519?: string | undefined, fetch?: typeof fetch | undefined}; export const applyWithdrawResponse = z.object({request_id: z.string(), status: z.enum(["pending", "processing", "completed", "failed"]), is_duplicate: z.boolean()}); export const applyWithdrawEnvelope = z.object({code: z.literal(0), message: z.string(), data: applyWithdrawResponse}); export const errorEnvelope = z.object({code: z.number().int().gte(1), message: z.string(), data: z.object({}).nullable()}); export const createApiWithdrawResponse = z.union([applyWithdrawEnvelope, errorEnvelope]); 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 }; /** * Withdraw organization funds * * POST /withdraw */ export const createApiWithdraw = async (args: CreateApiWithdrawArgs, options?: CreateApiWithdrawOptions) => { const {baseUrl = "https://openapi.infini.money/v2/funds", fetch: fetchFn = fetch, Ed25519} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-API-Key-ID'] != null) headers.set("X-API-Key-ID", String(args['X-API-Key-ID'])) if (args['X-API-Timestamp'] != null) headers.set("X-API-Timestamp", String(args['X-API-Timestamp'])) if (args['X-API-Nonce'] != null) headers.set("X-API-Nonce", String(args['X-API-Nonce'])) if (args.Digest != null) headers.set("Digest", String(args.Digest)) if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (Ed25519 !== undefined) headers.set("Authorization", Ed25519) const url = `${baseUrl.replace(/\/+$/, '')}/withdraw${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(createApiWithdrawResponse, await res.json()) };