import {z} from 'zod' export type GetApiApiV1TraderAddressPnlArgs = {'X-Query-Validation'?: 'strict' | undefined, address: string, from?: string | undefined, to?: string | undefined, sections?: (Array<'entries' | 'stats' | 'monthly' | 'year_totals' | 'drawdown'>) | undefined, 'sections[]'?: (Array<'entries' | 'stats' | 'monthly' | 'year_totals' | 'drawdown'>) | undefined}; export type GetApiApiV1TraderAddressPnlOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const traderPnl = z.object({id: z.string(), view: z.object({from: z.string().nullable(), to: z.string().nullable(), sections: z.array(z.enum(["entries", "stats", "monthly", "year_totals", "drawdown"])), entries_total: z.number().int(), entries_in_window: z.number().int(), anchor: z.object({date: z.string(), cumulative_profit: z.number().nullable(), total_pnl: z.number().nullable(), drawdown: z.number().nullable()}).nullable()}).optional(), freshness_at: z.string().nullable(), entries: z.array(z.object({date: z.string(), markets_traded: z.number().int().optional(), total_volume: z.number().optional(), cumulative_profit: z.number().optional(), total_pnl: z.number().optional(), daily_change: z.number()})).optional(), stats: z.object({all: z.object({current: z.number(), change: z.number(), change_pct: z.number(), best_day: z.number(), best_day_date: z.string().optional(), worst_day: z.number(), worst_day_date: z.string().optional(), win_days: z.number().int(), loss_days: z.number().int(), avg_day: z.number(), rebase_anchor: z.number()}), d90: z.object({current: z.number(), change: z.number(), change_pct: z.number(), best_day: z.number(), best_day_date: z.string().optional(), worst_day: z.number(), worst_day_date: z.string().optional(), win_days: z.number().int(), loss_days: z.number().int(), avg_day: z.number(), rebase_anchor: z.number()}), d30: z.object({current: z.number(), change: z.number(), change_pct: z.number(), best_day: z.number(), best_day_date: z.string().optional(), worst_day: z.number(), worst_day_date: z.string().optional(), win_days: z.number().int(), loss_days: z.number().int(), avg_day: z.number(), rebase_anchor: z.number()}), d7: z.object({current: z.number(), change: z.number(), change_pct: z.number(), best_day: z.number(), best_day_date: z.string().optional(), worst_day: z.number(), worst_day_date: z.string().optional(), win_days: z.number().int(), loss_days: z.number().int(), avg_day: z.number(), rebase_anchor: z.number()})}).optional(), monthly: z.array(z.object({year: z.number().int(), month: z.number().int(), pnl: z.number(), markets_traded: z.number().int()})).optional(), year_totals: z.array(z.object({year: z.number().int(), pnl: z.number()})).optional(), drawdown: z.array(z.object({date: z.string(), cumulative_profit: z.number(), drawdown: z.number()})).optional()}); export const responseMeta = z.object({request_id: z.string(), cached: z.boolean(), cache_age_s: z.number().int().optional(), cost: z.number().int(), ranking_generation: z.number().int().optional(), ranking_as_of: z.string().optional(), directional_source: z.enum(["live", "degraded"]).optional(), ranking_source: z.enum(["live", "db_only"]).optional(), category_skill_source: z.enum(["live", "partial", "degraded", "unavailable"]).optional(), category_skill_model_version: z.string().optional(), category_skill_taxonomy_version: z.string().optional(), category_skill_platform: z.literal("polymarket").optional(), category_skill_scope: z.literal("observed_goldsky_primary_taker_fill").optional(), category_skill_source_coverage: z.enum(["partial_whale_threshold_fills", "graded_wallet_fills"]).optional(), category_skill_observation_started_at: z.string().optional(), category_skill_model_operationally_degraded: z.boolean().optional(), category_skill_status_counts: z.object({live: z.number().int().gte(0), insufficient: z.number().int().gte(0), stale: z.number().int().gte(0), unknown: z.number().int().gte(0), degraded: z.number().int().gte(0)}).optional(), category_skill_base_payload_hash: z.string().optional(), category_skill_enriched_base_payload_hash: z.string().optional()}); export const getApiApiV1TraderAddressPnlResponse = z.object({object: z.literal("trader_pnl"), data: traderPnl, meta: responseMeta}); 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 trader P&L time series * * GET /api/v1/trader/{address}/pnl * * Also accepts auth: oauth2 */ export const getApiApiV1TraderAddressPnl = async (args: GetApiApiV1TraderAddressPnlArgs, options?: GetApiApiV1TraderAddressPnlOptions) => { const {baseUrl = "https://api.0xinsider.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['X-Query-Validation'] != null) headers.set("X-Query-Validation", String(args['X-Query-Validation'])) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) for (const item of args.sections ?? []) query.append("sections", String(item)) for (const item of args['sections[]'] ?? []) query.append("sections[]", String(item)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/trader/${encodeURIComponent(String(args.address))}/pnl${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(getApiApiV1TraderAddressPnlResponse, await res.json()) };