import {z} from 'zod' export type GetApiV2PnlWalletsWalletPerformanceArgs = {wallet: string, currency?: ('usd' | 'sol' | 'eur') | undefined, period?: ('1d' | '7d' | '14d' | '30d' | '90d' | 'all') | undefined, days?: number | undefined}; export type GetApiV2PnlWalletsWalletPerformanceOptions = {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 getApiV2PnlWalletsWalletPerformanceResponse = z.object({wallet: z.string().optional(), currency: z.enum(["sol", "eur"]).optional(), identity: identity.optional(), window: z.number().int().optional(), totals: z.object({realizedPnl: z.number().nullable().optional(), volume: z.number().nullable().optional(), trades: z.number().int().optional()}).optional(), bestDay: z.object({date: z.string().optional(), realizedPnl: z.number().nullable().optional(), volume: z.number().nullable().optional(), totalPnl: z.number().nullable().optional(), trades: z.number().int().optional()}).nullable().optional(), worstDay: z.object({date: z.string().optional(), realizedPnl: z.number().nullable().optional(), volume: z.number().nullable().optional(), totalPnl: z.number().nullable().optional(), trades: z.number().int().optional()}).nullable().optional(), streaks: z.object({positive: z.number().int().nullable().optional(), negative: z.number().int().nullable().optional(), currentPositive: z.number().int().nullable().optional(), currentNegative: z.number().int().nullable().optional()}).optional(), drawdown: z.object({amount: z.number().nullable().optional(), percent: z.number().nullable().optional()}).optional(), days: z.array(z.object({date: z.string().optional(), realizedPnl: z.number().nullable().optional(), unrealizedPnl: z.number().nullable().optional(), totalPnl: z.number().nullable().optional(), volume: z.number().nullable().optional(), trades: z.number().int().optional()})).optional(), updatedAt: z.string().nullable().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 Performance * * GET /v2/pnl/wallets/{wallet}/performance */ export const getApiV2PnlWalletsWalletPerformance = async (args: GetApiV2PnlWalletsWalletPerformanceArgs, options?: GetApiV2PnlWalletsWalletPerformanceOptions) => { 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.days != null) query.append("days", String(args.days)) if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/pnl/wallets/${encodeURIComponent(String(args.wallet))}/performance${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(getApiV2PnlWalletsWalletPerformanceResponse, await res.json()) };