import {z} from 'zod' export type GetApiPortfolioHistoryArgs = {cursor?: string | undefined, limit?: number | undefined}; export type GetApiPortfolioHistoryOptions = {baseUrl?: string | undefined, HmacAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const historyMarketGroupDto = z.object({id: z.number(), slug: z.string(), title: z.string(), status: z.string(), deadline: z.string(), hidden: z.boolean(), txHash: z.string().optional(), resolutionTxHash: z.string().optional(), priorityIndex: z.number(), metadata: z.object({}), negRiskMarketId: z.string(), createdAt: z.string(), updatedAt: z.string()}); export const historyMarketDto = z.object({closed: z.boolean(), collateral: z.object({}), group: historyMarketGroupDto, condition_id: z.string(), funding: z.number(), id: z.number(), slug: z.string(), title: z.string(), deadline: z.string()}); export const historyEntryDto = z.object({blockTimestamp: z.number(), collateralAmount: z.string(), market: historyMarketDto, outcomeTokenAmount: z.string(), outcomeTokenAmounts: z.array(z.string()), outcomeIndex: z.number(), outcomeTokenPrice: z.number(), strategy: z.enum(["Buy", "Sell", "Limit Buy", "Limit Sell", "Market Buy", "Market Sell", "Split", "Merge", "Convert", "Claim"]), transactionHash: z.string().optional()}); export const historyResponseDto = z.object({data: z.array(historyEntryDto), nextCursor: z.string().nullable().optional(), totalCount: z.number().nullable()}); 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 History * * GET /portfolio/history */ export const getApiPortfolioHistory = async (args: GetApiPortfolioHistoryArgs, options?: GetApiPortfolioHistoryOptions) => { const {baseUrl = "https://api.limitless.exchange", fetch: fetchFn = fetch, HmacAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.limit != null) query.append("limit", String(args.limit)) if (HmacAuth !== undefined) headers.set("lmts-api-key", HmacAuth) const url = `${baseUrl.replace(/\/+$/, '')}/portfolio/history${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(historyResponseDto, await res.json()) };