import {z} from 'zod' export type SortDirection = 'asc' | 'desc'; export type GetOpenPortfolioSort = 'current_balances' | 'unclaimed_fee' | 'fee_per_tvl24h'; export type GetApiPortfolioOpenArgs = {user: string, page?: number | undefined, page_size?: number | undefined, sort_direction?: SortDirection | undefined, sort_by?: GetOpenPortfolioSort | undefined}; export type GetApiPortfolioOpenOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const poolOpenPortfolioItem = z.object({balances: z.string(), balancesSol: z.unknown(), baseFee: z.number(), binStep: z.number().int().gte(0), collectFeeMode: z.number().int().gte(0), feePerTvl24h: z.string(), listPositions: z.array(z.string()), openPositionCount: z.number().int().gte(0), outOfRange: z.unknown(), pnl: z.string(), pnlPctChange: z.string(), pnlSol: z.unknown(), pnlSolPctChange: z.unknown(), poolAddress: z.string(), poolPrice: z.unknown(), poolStateUpdatedAtBlockTime: z.unknown(), poolStateUpdatedAtSlot: z.unknown(), positionsOutOfRange: z.array(z.string()), rewardX: z.string(), rewardY: z.string(), tokenX: z.string(), tokenXIcon: z.string(), tokenXMint: z.string(), tokenY: z.string(), tokenYIcon: z.string(), tokenYMint: z.string(), totalDeposit: z.string(), totalDepositSol: z.unknown(), unclaimedFees: z.string(), unclaimedFeesSol: z.unknown()}); export const totalMetrics = z.object({balances: z.string(), balancesSol: z.unknown(), pnl: z.string(), pnlPctChange: z.string(), pnlSol: z.unknown(), pnlSolPctChange: z.unknown(), totalPositions: z.number().int().gte(0), unclaimedFees: z.string(), unclaimedFeesSol: z.unknown()}); export const getOpenPortfolioResponse = z.object({hasNext: z.boolean(), page: z.number().int().gte(0), pageSize: z.number().int().gte(0), pools: z.array(poolOpenPortfolioItem), solPrice: z.unknown(), total: totalMetrics.optional(), totalCount: z.number().int().gte(0), totalPositions: z.number().int().gte(0)}); 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 user portfolio with all pools containing open positions * * GET /portfolio/open */ export const getApiPortfolioOpen = async (args: GetApiPortfolioOpenArgs, options?: GetApiPortfolioOpenOptions) => { const {baseUrl = "https://damm-api.meteora.ag", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.user != null) query.append("user", String(args.user)) if (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.sort_direction != null) query.append("sort_direction", String(args.sort_direction)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) const url = `${baseUrl.replace(/\/+$/, '')}/portfolio/open${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(getOpenPortfolioResponse, await res.json()) };