import {z} from 'zod' export type GetApiApiPublicV1PulseTraderAddressLifecyclesArgs = {address: string, coin?: string | undefined, status?: ('open' | 'closed' | 'all') | undefined, includeSpot?: boolean | undefined, includeCensored?: boolean | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiApiPublicV1PulseTraderAddressLifecyclesOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const lifecycleCoverage = z.object({reason: z.string().nullable(), tracked: z.boolean(), windowEnd: z.string(), windowStart: z.string()}); export const lifecycleRow = z.object({builderFee: z.number(), closedAt: z.string().optional(), closedAtRaw: z.number().int().optional(), coin: z.string(), durationMs: z.number().int().optional(), entryVwap: z.number(), exitVwap: z.number().optional(), fees: z.number(), fillCount: z.number().int(), id: z.number().int(), isClosed: z.boolean(), lifecycleNum: z.number().int(), liquidationClosedPnl: z.number().optional(), liquidationMethod: z.string().optional(), liquidatorAddress: z.string().optional(), openedAt: z.string(), openedAtRaw: z.number().int(), openedBeforeWindow: z.boolean(), peakSize: z.number(), penaltyFeePaid: z.number().optional(), realizedPnl: z.number(), side: z.string(), status: z.string(), wasLiquidated: z.boolean()}); export const walletLifecyclesBody = z.object({$schema: z.string().optional(), address: z.string(), coverage: lifecycleCoverage, lifecycles: z.array(lifecycleRow).nullable(), limit: z.number().int(), offset: z.number().int(), total: z.number().int()}); 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 API public v1 pulse trader by address lifecycles * * GET /api/public/v1/pulse/trader/{address}/lifecycles */ export const getApiApiPublicV1PulseTraderAddressLifecycles = async (args: GetApiApiPublicV1PulseTraderAddressLifecyclesArgs, options?: GetApiApiPublicV1PulseTraderAddressLifecyclesOptions) => { const {baseUrl = "https://api.coinversa.ai", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.coin != null) query.append("coin", String(args.coin)) if (args.status != null) query.append("status", String(args.status)) if (args.includeSpot != null) query.append("includeSpot", String(args.includeSpot)) if (args.includeCensored != null) query.append("includeCensored", String(args.includeCensored)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (apiKey !== undefined) headers.set("X-API-Key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/public/v1/pulse/trader/${encodeURIComponent(String(args.address))}/lifecycles${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(walletLifecyclesBody, await res.json()) };