import {z} from 'zod' export type FieldOptions = '0' | '1' | '2' | '5'; export type DirectionOptions = '0' | '1' | '2'; export type GetApiMmmPoolsArgs = {collectionSymbol?: string | undefined, pools?: Array | undefined, owner?: string | undefined, offset?: number | undefined, limit?: number | undefined, field?: FieldOptions | undefined, direction?: DirectionOptions | undefined}; export type GetApiMmmPoolsOptions = {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 AMM pools corresponding to an owner or collection symbol. At least one of required collectionSymbol or owner is required! * * GET /mmm/pools */ export const getApiMmmPools = async (args: GetApiMmmPoolsArgs, options?: GetApiMmmPoolsOptions) => { 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.collectionSymbol != null) query.append("collectionSymbol", String(args.collectionSymbol)) for (const item of args.pools ?? []) query.append("pools", String(item)) if (args.owner != null) query.append("owner", String(args.owner)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.field != null) query.append("field", String(args.field)) if (args.direction != null) query.append("direction", String(args.direction)) const url = `${baseUrl.replace(/\/+$/, '')}/mmm/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()) };