import {z} from 'zod' export type GetApiMmmTokenMintAddressPoolsArgs = {mint_address: string, limit?: number | undefined}; export type GetApiMmmTokenMintAddressPoolsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const iAttributeV2 = z.object({trait_type: z.string(), value: z.unknown()}); export const mMMCurveKindString = z.enum(["linear", "exp", "xyk"]); export const mMMPoolTypeString = z.enum(["buy_sided", "sell_sided", "two_sided", "invalid"]); export const iSharedEscrowInfo = z.object({sharedEscrowAddress: z.string(), sharedEscrowCount: z.number(), sharedEscrowBuysidePaymentAmount: z.number()}); export const iMmmPoolV2Response = z.object({results: z.array(z.object({collectionName: z.string(), attributes: z.array(z.array(iAttributeV2)).optional(), expiry: z.number(), collectionSymbol: z.string(), spotPrice: z.number(), curveType: mMMCurveKindString, curveDelta: z.number(), reinvestFulfillBuy: z.boolean(), reinvestFulfillSell: z.boolean(), lpFeeBp: z.number(), buysideCreatorRoyaltyBp: z.number(), poolOwner: z.string(), sellsideAssetAmount: z.number(), buysidePaymentAmount: z.number(), buyOrdersAmount: z.number(), poolType: mMMPoolTypeString, uuid: z.string(), poolKey: z.string(), cosigner: z.string(), blockedAt: z.string().optional(), mints: z.array(z.string()).optional(), collectionSellerFeeBasisPoints: z.number(), lpFeeEarned: z.number(), buyPriceTaker: z.number().optional(), isMIP1: z.boolean().optional(), isOCP: z.boolean().optional(), sharedEscrowInfo: iSharedEscrowInfo.optional(), updatedAt: z.string()}))}); 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 best offers for an NFT * * GET /mmm/token/{mint_address}/pools */ export const getApiMmmTokenMintAddressPools = async (args: GetApiMmmTokenMintAddressPoolsArgs, options?: GetApiMmmTokenMintAddressPoolsOptions) => { const {baseUrl = "https://api-mainnet.magiceden.dev/v2", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) const url = `${baseUrl.replace(/\/+$/, '')}/mmm/token/${encodeURIComponent(String(args.mint_address))}/pools${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(iMmmPoolV2Response, await res.json()) };