import {z} from 'zod' export type BuildTransactionRequest = {wallet: string, swapResponse: {id?: string | undefined, success?: boolean | undefined, version?: string | undefined, data?: Record | undefined}, txVersion: 'V0' | 'LEGACY', computeUnitPriceMicroLamports: string, wrapSol?: boolean | undefined, unwrapSol?: boolean | undefined, inputAccount?: string | undefined, outputAccount?: string | undefined, jitoInfo?: ({address?: string | undefined, amount?: string | undefined}) | undefined, referrerWallet?: string | undefined}; export type CreateApiTransactionSwapBaseInArgs = {body: BuildTransactionRequest}; export type CreateApiTransactionSwapBaseInOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const transactionResponse = z.object({id: z.string().optional(), success: z.boolean().optional(), version: z.string().optional(), data: z.object({transaction: z.string().optional(), addressLookupTableAddresses: z.array(z.string()).optional()}).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 }; /** * Build swap transaction (base input) * * POST /transaction/swap-base-in */ export const createApiTransactionSwapBaseIn = async (args: CreateApiTransactionSwapBaseInArgs, options?: CreateApiTransactionSwapBaseInOptions) => { const {baseUrl = "https://api.raydium.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/transaction/swap-base-in${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(transactionResponse, await res.json()) };