import {z} from 'zod' export type GetApiPnlWalletArgs = {wallet: string, showHistoricPnL?: boolean | undefined, holdingCheck?: boolean | undefined, hideDetails?: boolean | undefined}; export type GetApiPnlWalletOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const tokenPnLResponse = z.object({holding: z.number().optional(), held: z.number().optional(), sold: z.number().optional(), sold_usd: z.number().optional(), realized: z.number().optional(), unrealized: z.number().optional(), total: z.number().optional(), total_sold: z.number().optional(), total_invested: z.number().optional(), average_buy_amount: z.number().optional(), current_value: z.number().optional(), cost_basis: z.number().optional(), first_buy_time: z.number().int().optional(), last_buy_time: z.number().int().optional(), last_sell_time: z.number().int().optional(), last_trade_time: z.number().int().optional(), buy_transactions: z.number().int().optional(), sell_transactions: z.number().int().optional(), total_transactions: z.number().int().optional()}); export const pnLSummary = z.object({realized: z.number().optional(), unrealized: z.number().optional(), total: z.number().optional(), totalInvested: z.number().optional(), averageBuyAmount: z.number().optional(), totalWins: z.number().int().optional(), totalLosses: z.number().int().optional(), winPercentage: z.number().optional(), lossPercentage: z.number().optional()}); export const walletPnLResponse = z.object({tokens: z.record(z.string(), tokenPnLResponse).optional(), summary: pnLSummary.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 }; /** * Deprecated: Get Wallet PnL * * GET /pnl/{wallet} */ export const getApiPnlWallet = async (args: GetApiPnlWalletArgs, options?: GetApiPnlWalletOptions) => { 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.showHistoricPnL != null) query.append("showHistoricPnL", String(args.showHistoricPnL)) if (args.holdingCheck != null) query.append("holdingCheck", String(args.holdingCheck)) if (args.hideDetails != null) query.append("hideDetails", String(args.hideDetails)) if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/pnl/${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(walletPnLResponse, await res.json()) };