import {z} from 'zod' export type GetApiTradesTokenAddressByWalletOwnerArgs = {tokenAddress: string, owner: string, cursor?: string | undefined, showMeta?: boolean | undefined, parseJupiter?: boolean | undefined, hideArb?: boolean | undefined, sortDirection?: ('DESC' | 'ASC') | undefined}; export type GetApiTradesTokenAddressByWalletOwnerOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const tokenTrade = z.object({tx: z.string().optional(), amount: z.number().optional(), priceUsd: z.number().optional(), volume: z.number().optional(), volumeSol: z.number().optional(), type: z.enum(["buy", "sell"]).optional(), wallet: z.string().optional(), time: z.number().int().optional(), program: z.string().optional(), pools: z.array(z.string()).optional()}); export const tokenTradesResponse = z.object({trades: z.array(tokenTrade).optional(), nextCursor: z.number().int().optional(), hasNextPage: z.boolean().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 User-Specific Token Trades * * GET /trades/{tokenAddress}/by-wallet/{owner} */ export const getApiTradesTokenAddressByWalletOwner = async (args: GetApiTradesTokenAddressByWalletOwnerArgs, options?: GetApiTradesTokenAddressByWalletOwnerOptions) => { 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.cursor != null) query.append("cursor", String(args.cursor)) if (args.showMeta != null) query.append("showMeta", String(args.showMeta)) if (args.parseJupiter != null) query.append("parseJupiter", String(args.parseJupiter)) if (args.hideArb != null) query.append("hideArb", String(args.hideArb)) if (args.sortDirection != null) query.append("sortDirection", String(args.sortDirection)) if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/trades/${encodeURIComponent(String(args.tokenAddress))}/by-wallet/${encodeURIComponent(String(args.owner))}${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(tokenTradesResponse, await res.json()) };