import {z} from 'zod' export type InputForWithdraw = {all?: (boolean | null) | undefined, amount?: (number | null) | undefined, base_asset: string, create_wsol_ata?: (boolean | null) | undefined, detect_wsol_ata?: (boolean | null) | undefined, exclude_fees?: (boolean | null) | undefined, fund_id?: (string | null) | undefined, payer_key?: (string | null) | undefined, strategy_id: string, unwrap_wsol_ata?: (boolean | null) | undefined, user_key: string, user_token_account?: (string | null) | undefined}; export type StructuredInputForWithdraw = {params: InputForWithdraw}; export type CreateApiWithdrawIxArgs = {body: StructuredInputForWithdraw}; export type CreateApiWithdrawIxOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const solanaAccountMeta = z.object({is_signer: z.boolean(), is_writable: z.boolean(), pubkey: z.array(z.number().int().gte(0))}); export const solanaInstruction = z.object({accounts: z.array(solanaAccountMeta), data: z.array(z.number().int().gte(0)), program_id: z.array(z.number().int().gte(0))}); export const withdrawInstructions = z.object({lookup_table: z.string(), withdraw_instructions: z.array(solanaInstruction)}); 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 }; /** * Generate withdraw instructions * * POST /withdraw/ix */ export const createApiWithdrawIx = async (args: CreateApiWithdrawIxArgs, options?: CreateApiWithdrawIxOptions) => { const {baseUrl = "https://api.breeze.baby", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/withdraw/ix${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(withdrawInstructions, await res.json()) };