import {z} from 'zod' export type GetApiInstructionsSellChangePriceArgs = {seller: string, auctionHouseAddress?: string | undefined, tokenMint: string, tokenAccount: string, price: number, newPrice: number, sellerReferral?: string | undefined, expiry: number, priorityFee?: number | undefined}; export type GetApiInstructionsSellChangePriceOptions = {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 change a sell price * * GET /instructions/sell_change_price */ export const getApiInstructionsSellChangePrice = async (args: GetApiInstructionsSellChangePriceArgs, options?: GetApiInstructionsSellChangePriceOptions) => { 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.seller != null) query.append("seller", String(args.seller)) if (args.auctionHouseAddress != null) query.append("auctionHouseAddress", String(args.auctionHouseAddress)) if (args.tokenMint != null) query.append("tokenMint", String(args.tokenMint)) if (args.tokenAccount != null) query.append("tokenAccount", String(args.tokenAccount)) if (args.price != null) query.append("price", String(args.price)) if (args.newPrice != null) query.append("newPrice", String(args.newPrice)) if (args.sellerReferral != null) query.append("sellerReferral", String(args.sellerReferral)) if (args.expiry != null) query.append("expiry", String(args.expiry)) if (args.priorityFee != null) query.append("priorityFee", String(args.priorityFee)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/instructions/sell_change_price${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()) };