import {z} from 'zod' export type GetApiPoolsInfoListV2Args = {poolType?: ('Concentrated' | 'Standard') | undefined, mintFilter?: string | undefined, hasReward?: boolean | undefined, sortField?: ('liquidity' | 'volume24h' | 'fee24h' | 'apr24h' | 'volume7d' | 'fee7d' | 'apr7d' | 'volume30d' | 'fee30d' | 'apr30d') | undefined, sortType?: ('asc' | 'desc') | undefined, size: number, nextPageId?: string | undefined, mint1?: string | undefined, mint2?: string | undefined}; export type GetApiPoolsInfoListV2Options = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const poolInfoItem = z.object({id: z.string().optional(), type: z.string().optional(), programId: z.string().optional(), lpMint: z.string().optional(), mint1: z.object({}).optional(), mint2: z.object({}).optional(), tvl: z.number().optional(), lpPrice: z.number().optional(), farmOngoingCount: z.number().optional(), day: z.object({}).optional(), week: z.object({}).optional(), month: z.object({}).optional()}); export const poolListV2Response = z.object({data: z.array(poolInfoItem).optional(), nextPageId: z.string().nullable().optional()}); export const getApiPoolsInfoListV2Response = z.object({id: z.string(), success: z.boolean(), msg: z.string().optional(), data: poolListV2Response.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 }; /** * List pools with advanced filters (v2) * * GET /pools/info/list-v2 */ export const getApiPoolsInfoListV2 = async (args: GetApiPoolsInfoListV2Args, options?: GetApiPoolsInfoListV2Options) => { const {baseUrl = "https://api.raydium.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.poolType != null) query.append("poolType", String(args.poolType)) if (args.mintFilter != null) query.append("mintFilter", String(args.mintFilter)) if (args.hasReward != null) query.append("hasReward", String(args.hasReward)) if (args.sortField != null) query.append("sortField", String(args.sortField)) if (args.sortType != null) query.append("sortType", String(args.sortType)) if (args.size != null) query.append("size", String(args.size)) if (args.nextPageId != null) query.append("nextPageId", String(args.nextPageId)) if (args.mint1 != null) query.append("mint1", String(args.mint1)) if (args.mint2 != null) query.append("mint2", String(args.mint2)) const url = `${baseUrl.replace(/\/+$/, '')}/pools/info/list-v2${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(getApiPoolsInfoListV2Response, await res.json()) };