import {z} from 'zod' export type GetApiTokensTrendingOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const token = z.object({name: z.string().optional(), symbol: z.string().optional(), mint: z.string().optional(), uri: z.string().optional(), decimals: z.number().int().optional(), description: z.string().optional(), image: z.string().optional(), hasFileMetaData: z.boolean().optional(), strictSocials: z.object({}).optional(), creation: z.object({creator: z.string().optional(), created_tx: z.string().optional(), created_time: z.number().int().optional()}).optional()}); export const pool = z.object({poolId: z.string().optional(), liquidity: z.object({quote: z.number().optional(), usd: z.number().optional()}).optional(), price: z.object({quote: z.number().optional(), usd: z.number().optional()}).optional(), tokenSupply: z.number().optional(), lpBurn: z.number().int().optional(), tokenAddress: z.string().optional(), marketCap: z.object({quote: z.number().optional(), usd: z.number().optional()}).optional(), market: z.string().optional(), quoteToken: z.string().optional(), decimals: z.number().int().optional(), security: z.object({freezeAuthority: z.string().nullable().optional(), mintAuthority: z.string().nullable().optional()}).optional(), lastUpdated: z.number().int().optional(), deployer: z.string().optional(), txns: z.object({buys: z.number().int().optional(), total: z.number().int().optional(), volume: z.number().optional(), volume24h: z.number().optional(), sells: z.number().int().optional()}).optional(), bundleId: z.string().optional()}); export const events = z.record(z.string(), z.object({priceChangePercentage: z.number().optional()})); export const risk = z.object({snipers: z.object({count: z.number().int().optional(), totalBalance: z.number().optional(), totalPercentage: z.number().optional(), wallets: z.array(z.string()).optional()}).optional(), bundlers: z.object({count: z.number().int().optional(), totalBalance: z.number().optional(), totalPercentage: z.number().optional(), totalInitialBalance: z.number().optional(), totalInitialPercentage: z.number().optional(), wallets: z.array(z.object({wallet: z.string().optional(), initialBalance: z.number().optional(), initialPercentage: z.number().optional(), balance: z.number().optional(), percentage: z.number().optional(), bundleTime: z.number().int().optional()})).optional()}).optional(), insiders: z.object({count: z.number().int().optional(), totalBalance: z.number().optional(), totalPercentage: z.number().optional(), wallets: z.array(z.string()).optional()}).optional(), top10: z.number().optional(), dev: z.object({percentage: z.number().optional(), amount: z.number().optional()}).optional(), fees: z.record(z.string(), z.number()).optional(), rugged: z.boolean().optional(), risks: z.array(z.string()).optional(), score: z.number().int().optional(), jupiterVerified: z.boolean().optional()}); export const tokenInfo = z.object({token: token.optional(), pools: z.array(pool).optional(), events: events.optional(), risk: risk.optional(), buys: z.number().int().optional(), sells: z.number().int().optional(), txns: z.number().int().optional(), holders: z.number().int().optional()}); export const getApiTokensTrendingResponse = z.array(tokenInfo); 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 Trending Tokens * * GET /tokens/trending */ export const getApiTokensTrending = async (options?: GetApiTokensTrendingOptions) => { 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 (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/tokens/trending${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(getApiTokensTrendingResponse, await res.json()) };