import {z} from 'zod' export type PaginationRequest = {page?: number | undefined, per_page?: number | undefined}; export type PnlByMarketSortField = 'total_pnl_usd'; export type SortDirection = 'ASC' | 'DESC'; export type SortOrderPnlByMarketSortField = {field: PnlByMarketSortField, direction: SortDirection}; export type PnlByMarketRequest = {market_id: string, pagination?: PaginationRequest | undefined, order_by?: Array | undefined}; export type CreateApiApiV1PredictionMarketPnlByMarketArgs = {body: PnlByMarketRequest}; export type CreateApiApiV1PredictionMarketPnlByMarketOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const paginationInfo = z.object({page: z.number().int().gte(1).optional(), per_page: z.number().int().gte(1).lte(1000).optional(), is_last_page: z.boolean().optional()}); export const pnlByMarketItem = z.object({address: z.string().optional(), owner_address: z.string().optional(), side_held: z.string().optional(), net_buy_cost_usd: z.number().optional(), net_sell_proceeds_usd: z.number().optional(), redemption_value_usd: z.number().optional(), unrealized_value_usd: z.number().optional(), total_pnl_usd: z.number().optional(), question: z.string().optional(), event_id: z.string().optional(), event_title: z.string().optional(), market_resolved: z.boolean().optional(), market_id: z.string()}); export const pnlByMarketResponse = z.object({pagination: paginationInfo.optional(), data: z.array(pnlByMarketItem)}); 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 Prediction Market PnL by Market * * POST /api/v1/prediction-market/pnl-by-market */ export const createApiApiV1PredictionMarketPnlByMarket = async (args: CreateApiApiV1PredictionMarketPnlByMarketArgs, options?: CreateApiApiV1PredictionMarketPnlByMarketOptions) => { const {baseUrl = "https://api.nansen.ai", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("apiKey", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/prediction-market/pnl-by-market${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(pnlByMarketResponse, await res.json()) };