import {z} from 'zod' export type GetApiV2PnlWalletsWalletHistoryArgs = {wallet: string, currency?: ('usd' | 'sol' | 'eur') | undefined, period?: ('1d' | '7d' | '14d' | '30d' | '90d' | 'all') | undefined, start?: string | undefined, end?: string | undefined, limit?: number | undefined}; export type GetApiV2PnlWalletsWalletHistoryOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const identity = z.object({name: z.string().nullable().optional(), twitter: z.string().nullable().optional(), avatar: z.string().nullable().optional(), type: z.string().nullable().optional(), tags: z.array(z.string()).optional(), platforms: z.array(z.string()).optional(), bot: z.object({name: z.string().nullable().optional(), avatar: z.string().nullable().optional()}).optional(), pool: z.object({program: z.string().nullable().optional(), poolAddress: z.string().nullable().optional()}).optional(), developer: z.object({token: z.string().nullable().optional(), via: z.array(z.string()).optional(), pools: z.array(z.string()).optional(), creationTx: z.string().nullable().optional(), createdAt: z.number().int().nullable().optional()}).optional(), hacker: z.object({label: z.string().nullable().optional()}).optional(), spamDusting: z.object({label: z.string().nullable().optional()}).optional(), exchange: z.object({name: z.string().nullable().optional()}).optional()}); export const pnlBlock = z.object({realized: z.number().nullable().optional(), realizedRaw: z.number().nullable().optional(), unrealized: z.number().nullable().optional(), total: z.number().nullable().optional()}); export const snapshot = z.object({date: z.string().optional(), cumulative: z.object({pnl: pnlBlock.optional(), cost: z.number().nullable().optional(), proceeds: z.number().nullable().optional(), openPositions: z.object({cost: z.number().nullable().optional(), value: z.number().nullable().optional()}).optional(), counts: z.object({buys: z.number().int().optional(), sells: z.number().int().optional(), tokensTraded: z.number().int().optional()}).optional()}).optional(), activity: z.object({pnl: z.object({realized: z.number().nullable().optional()}).optional(), counts: z.object({buys: z.number().int().optional(), sells: z.number().int().optional()}).optional(), volume: z.object({costUsd: z.number().nullable().optional(), total: z.number().nullable().optional()}).optional(), averages: z.object({buy: z.number().nullable().optional(), sell: z.number().nullable().optional(), realizedPnl: z.number().nullable().optional(), holdTimeSecs: z.number().int().nullable().optional()}).optional()}).optional()}); export const getApiV2PnlWalletsWalletHistoryResponse = z.object({wallet: z.string().optional(), currency: z.enum(["sol", "eur"]).optional(), identity: identity.optional(), days: z.array(snapshot).optional(), summary: z.object({days: z.object({trading: z.number().int().optional(), positive: z.number().int().optional(), negative: z.number().int().optional(), breakEven: z.number().int().optional()}).optional(), totals: z.object({realizedPnl: z.number().nullable().optional(), volume: z.number().nullable().optional()}).optional(), winRate: z.number().nullable().optional(), totalDays: z.number().int().optional(), deltas: z.object({realized: z.number().nullable().optional(), total: z.number().nullable().optional()}).nullable().optional()}).optional()}); 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 Wallet PnL Calendar * * GET /v2/pnl/wallets/{wallet}/history */ export const getApiV2PnlWalletsWalletHistory = async (args: GetApiV2PnlWalletsWalletHistoryArgs, options?: GetApiV2PnlWalletsWalletHistoryOptions) => { const {baseUrl = "https://data.solanatracker.io", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.currency != null) query.append("currency", String(args.currency)) if (args.period != null) query.append("period", String(args.period)) if (args.start != null) query.append("start", String(args.start)) if (args.end != null) query.append("end", String(args.end)) if (args.limit != null) query.append("limit", String(args.limit)) if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/pnl/wallets/${encodeURIComponent(String(args.wallet))}/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(getApiV2PnlWalletsWalletHistoryResponse, await res.json()) };