import {z} from 'zod' export type GetApiV2PnlWalletsWalletArgs = {wallet: string, pnlMode?: ('strict' | 'adjusted' | 'raw') | undefined, currency?: ('usd' | 'sol' | 'eur') | undefined}; export type GetApiV2PnlWalletsWalletOptions = {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 summary = z.object({pnl: pnlBlock.optional(), invested: 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(), trades: z.number().int().optional(), tokensTraded: z.number().int().optional(), tokensHeldEver: z.number().int().optional()}).optional(), averages: z.object({buy: z.number().nullable().optional(), sell: z.number().nullable().optional()}).optional(), roi: z.number().nullable().optional(), timing: z.object({firstTrade: z.number().int().nullable().optional(), lastTrade: z.number().int().nullable().optional(), avgHoldTimeSecs: z.number().int().nullable().optional()}).optional()}); export const getApiV2PnlWalletsWalletResponse = z.object({wallet: z.string().optional(), pnlMode: z.enum(["strict", "adjusted", "raw"]).optional(), currency: z.enum(["sol", "eur"]).optional(), identity: identity.optional(), summary: summary.optional(), analysis: z.object({winRate: z.number().nullable().optional(), avgPnlPerAsset: z.number().nullable().optional(), avgBuyValue: z.number().nullable().optional(), tokens: z.object({closed: z.number().int().optional(), winning: z.number().int().optional(), losing: z.number().int().optional()}).optional(), distribution: z.array(z.object({range: z.string().optional(), count: z.number().int().optional(), rate: z.number().nullable().optional()})).optional()}).optional(), stats: z.object({total: z.number().int().optional(), holding: z.number().int().optional(), sold: z.number().int().optional(), profitable: z.number().int().optional(), losing: z.number().int().optional()}).optional(), tags: z.object({isArbitrage: z.boolean().optional(), platforms: z.array(z.string()).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 Summary * * GET /v2/pnl/wallets/{wallet} */ export const getApiV2PnlWalletsWallet = async (args: GetApiV2PnlWalletsWalletArgs, options?: GetApiV2PnlWalletsWalletOptions) => { 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.pnlMode != null) query.append("pnlMode", String(args.pnlMode)) if (args.currency != null) query.append("currency", String(args.currency)) if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/pnl/wallets/${encodeURIComponent(String(args.wallet))}${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(getApiV2PnlWalletsWalletResponse, await res.json()) };