import {z} from 'zod' export type GetApiInstructionsMmmSolFulfillBuyArgs = {pool: string, assetAmount: number, minPaymentAmount: number, seller: string, assetMint: string, assetTokenAccount: string, allowlistAuxAccount?: string | undefined, skipDelist?: boolean | undefined, priorityFee?: number | undefined}; export type GetApiInstructionsMmmSolFulfillBuyOptions = {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 have a pool fulfill a buy * * GET /instructions/mmm/sol-fulfill-buy */ export const getApiInstructionsMmmSolFulfillBuy = async (args: GetApiInstructionsMmmSolFulfillBuyArgs, options?: GetApiInstructionsMmmSolFulfillBuyOptions) => { 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.pool != null) query.append("pool", String(args.pool)) if (args.assetAmount != null) query.append("assetAmount", String(args.assetAmount)) if (args.minPaymentAmount != null) query.append("minPaymentAmount", String(args.minPaymentAmount)) if (args.seller != null) query.append("seller", String(args.seller)) if (args.assetMint != null) query.append("assetMint", String(args.assetMint)) if (args.assetTokenAccount != null) query.append("assetTokenAccount", String(args.assetTokenAccount)) if (args.allowlistAuxAccount != null) query.append("allowlistAuxAccount", String(args.allowlistAuxAccount)) if (args.skipDelist != null) query.append("skipDelist", String(args.skipDelist)) if (args.priorityFee != null) query.append("priorityFee", String(args.priorityFee)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/instructions/mmm/sol-fulfill-buy${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()) };