import {z} from 'zod' export type PositionEventType = 'add' | 'remove' | 'claim_fee' | 'claim_reward'; export type PositionEventOrderDirection = 'asc' | 'desc'; export type GetApiPositionsAddressHistoricalArgs = {address: string, event_type?: PositionEventType | undefined, order_direction?: PositionEventOrderDirection | undefined}; export type GetApiPositionsAddressHistoricalOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const positionEvent = z.object({amountX: z.string(), amountXUsd: z.string(), amountY: z.string(), amountYUsd: z.string(), blockTime: z.number().int().gte(0), createdAt: z.string(), eventType: z.string(), ixIndex: z.number().int().gte(0), poolAddress: z.string(), positionAddress: z.string(), signature: z.string(), slot: z.number().int().gte(0), tokenX: z.string(), tokenY: z.string(), totalUsd: z.string(), userAddress: z.string()}); export const getPositionHistoricalEventsResponse = z.object({events: z.array(positionEvent)}); 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 the historical events for a position * * GET /positions/{address}/historical */ export const getApiPositionsAddressHistorical = async (args: GetApiPositionsAddressHistoricalArgs, options?: GetApiPositionsAddressHistoricalOptions) => { 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.event_type != null) query.append("event_type", String(args.event_type)) if (args.order_direction != null) query.append("order_direction", String(args.order_direction)) const url = `${baseUrl.replace(/\/+$/, '')}/positions/${encodeURIComponent(String(args.address))}/historical${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(getPositionHistoricalEventsResponse, await res.json()) };