import {z} from 'zod' export type GetApiV2PnlWalletsWalletChartArgs = {wallet: string, time_from?: number | undefined, time_to?: number | undefined}; export type GetApiV2PnlWalletsWalletChartOptions = {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 chartPoint = z.object({date: z.string().optional(), time: z.number().int().optional(), pnl: z.object({realized: z.number().nullable().optional(), total: z.number().nullable().optional()}).optional(), invested: z.number().nullable().optional(), proceeds: z.number().nullable().optional(), activity: z.object({realizedPnl: z.number().nullable().optional(), buys: z.number().int().optional(), sells: z.number().int().optional(), volume: z.number().nullable().optional(), avgHoldTimeSecs: z.number().int().nullable().optional()}).optional(), counts: z.object({buys: z.number().int().optional(), sells: z.number().int().optional(), tokensTraded: z.number().int().optional()}).optional()}); export const getApiV2PnlWalletsWalletChartResponse = z.object({wallet: z.string().optional(), identity: identity.optional(), points: z.array(chartPoint).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(), bestDay: z.object({date: z.string().optional(), realizedPnl: z.number().nullable().optional()}).nullable().optional(), worstDay: z.object({date: z.string().optional(), realizedPnl: z.number().nullable().optional()}).nullable().optional(), streaks: z.object({positive: z.number().int().optional(), negative: z.number().int().optional(), currentPositive: z.number().int().optional(), currentNegative: z.number().int().optional()}).optional(), drawdown: z.object({amount: z.number().nullable().optional(), percent: z.number().nullable().optional()}).optional(), averages: z.object({dailyVolume: z.number().nullable().optional(), dailyRealizedPnl: z.number().nullable().optional(), holdTimeSecs: z.number().int().nullable().optional()}).optional()}).optional(), pagination: z.object({count: z.number().int().optional(), hasMore: z.boolean().optional(), nextTimeTo: z.number().int().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 Chart * * GET /v2/pnl/wallets/{wallet}/chart */ export const getApiV2PnlWalletsWalletChart = async (args: GetApiV2PnlWalletsWalletChartArgs, options?: GetApiV2PnlWalletsWalletChartOptions) => { 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.time_from != null) query.append("time_from", String(args.time_from)) if (args.time_to != null) query.append("time_to", String(args.time_to)) if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/pnl/wallets/${encodeURIComponent(String(args.wallet))}/chart${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(getApiV2PnlWalletsWalletChartResponse, await res.json()) };