import {z} from 'zod' export type GetApiAlphaVaultArgs = {vault_address?: (Array | null) | undefined, pool_address?: (Array | null) | undefined, base_mint?: (Array | null) | undefined}; export type GetApiAlphaVaultOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const alphaVaultResponse = z.object({base: z.string(), base_mint: z.string(), owner: z.string(), pool_address: z.string(), pool_type: z.number().int(), quote_mint: z.string(), token_out_vault: z.string(), token_vault: z.string(), vault_address: z.string()}); export const getApiAlphaVaultResponse = z.array(alphaVaultResponse); 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 }; /** * Alpha Vaults * * GET /alpha-vault */ export const getApiAlphaVault = async (args: GetApiAlphaVaultArgs, options?: GetApiAlphaVaultOptions) => { const {baseUrl = "https://damm-api.meteora.ag", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.vault_address ?? []) query.append("vault_address", String(item)) for (const item of args.pool_address ?? []) query.append("pool_address", String(item)) for (const item of args.base_mint ?? []) query.append("base_mint", String(item)) const url = `${baseUrl.replace(/\/+$/, '')}/alpha-vault${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(getApiAlphaVaultResponse, await res.json()) };