import {z} from 'zod' export type MMMCurveKindString = 'linear' | 'exp' | 'xyk'; export type GetApiInstructionsMmmCreatePoolArgs = {spotPrice: number, curveType: MMMCurveKindString, curveDelta: number, reinvestBuy: boolean, reinvestSell: boolean, expiry?: number | undefined, lpFeeBp: number, buysideCreatorRoyaltyBp: number, paymentMint: string, collectionSymbol: string, owner: string, solDeposit?: number | undefined, priorityFee?: number | undefined, sharedEscrowCount?: number | undefined}; export type GetApiInstructionsMmmCreatePoolOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const iBuffer = z.object({type: z.literal("Buffer"), data: z.array(z.number())}); export const transaction = z.object({tx: iBuffer, txSigned: iBuffer.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 }; /** * Get instruction to create a pool * * GET /instructions/mmm/create-pool */ export const getApiInstructionsMmmCreatePool = async (args: GetApiInstructionsMmmCreatePoolArgs, options?: GetApiInstructionsMmmCreatePoolOptions) => { const {baseUrl = "https://api-mainnet.magiceden.dev/v2", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.spotPrice != null) query.append("spotPrice", String(args.spotPrice)) if (args.curveType != null) query.append("curveType", String(args.curveType)) if (args.curveDelta != null) query.append("curveDelta", String(args.curveDelta)) if (args.reinvestBuy != null) query.append("reinvestBuy", String(args.reinvestBuy)) if (args.reinvestSell != null) query.append("reinvestSell", String(args.reinvestSell)) if (args.expiry != null) query.append("expiry", String(args.expiry)) if (args.lpFeeBp != null) query.append("lpFeeBp", String(args.lpFeeBp)) if (args.buysideCreatorRoyaltyBp != null) query.append("buysideCreatorRoyaltyBp", String(args.buysideCreatorRoyaltyBp)) if (args.paymentMint != null) query.append("paymentMint", String(args.paymentMint)) if (args.collectionSymbol != null) query.append("collectionSymbol", String(args.collectionSymbol)) if (args.owner != null) query.append("owner", String(args.owner)) if (args.solDeposit != null) query.append("solDeposit", String(args.solDeposit)) if (args.priorityFee != null) query.append("priorityFee", String(args.priorityFee)) if (args.sharedEscrowCount != null) query.append("sharedEscrowCount", String(args.sharedEscrowCount)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/instructions/mmm/create-pool${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) 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()) };