import {z} from 'zod' export type ApiJettonVerification = 'BLACKLISTED' | 'UNKNOWN' | 'COMMUNITY' | 'WHITELISTED'; export type ApiHybridSearchKind = 'ALL' | 'DEXES' | 'MEMES_ALL' | 'MEMES_DEXES' | 'MEMES_MEMEPADS'; export type ApiHybridSearchSort = 'FDMC' | 'TVL' | 'MCAP' | 'VOLUME_24H' | 'PRICE_CHANGE_24H'; export type ApiSortDirection = 'ASC' | 'DESC'; export type GetApiApiV3HybridSearchArgs = {search?: string | undefined, verification?: Array | undefined, size?: number | undefined, page?: number | undefined, kind?: ApiHybridSearchKind | undefined, sort?: ApiHybridSearchSort | undefined, direction?: ApiSortDirection | undefined}; export type GetApiApiV3HybridSearchOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const apiJettonVerification = z.enum(["BLACKLISTED", "UNKNOWN", "COMMUNITY", "WHITELISTED"]); export const apiJettonMarketStats = z.object({holders_count: z.number().int(), price_usd: z.number(), price_change_7d: z.number(), price_change_5m: z.number(), price_change_1h: z.number(), price_change_6h: z.number(), price_change_24h: z.number(), volume_usd_24h: z.number(), tvl_usd: z.number(), fdmc: z.number(), mcap: z.number(), trust_score: z.number().int().optional(), volume_buy_usd_24h: z.number().optional(), volume_sell_usd_24h: z.number().optional(), unique_traders_24h: z.number().int().optional(), lp_locked_percent: z.number().optional()}); export const apiJettonLabelRelation = z.object({label: z.string(), label_id: z.number().int(), created_at: z.string(), expires_at: z.string().optional()}); export const apiJettonScaledUiMultiplier = z.object({numerator: z.string(), denominator: z.string()}); export const apiJetton = z.object({created_at: z.string().optional(), address: z.string(), total_supply: z.string().optional(), name: z.string().optional(), symbol: z.string().optional(), decimals: z.number().int(), mintable: z.boolean().optional(), buy_fee: z.number().int().optional(), sell_fee: z.number().int().optional(), verification: apiJettonVerification.optional(), contract_interface: z.string().optional(), image_url: z.string().optional(), market_stats: apiJettonMarketStats.optional(), labels: z.array(apiJettonLabelRelation).optional(), scaled_ui_multiplier: apiJettonScaledUiMultiplier.optional()}); export const apiMemepadJettonMarketStats = z.object({price_usd: z.number(), tvl_usd: z.number(), fdmc_usd: z.number(), max_ton: z.number().optional(), collected_ton: z.number(), progress: z.number().optional()}); export const apiMemepadJetton = z.object({created_at: z.string(), address: z.string(), protocol: z.string(), name: z.string().optional(), symbol: z.string().optional(), decimals: z.number().int(), image_url: z.string().optional(), market_stats: apiMemepadJettonMarketStats.optional()}); export const apiPolyJetton = z.discriminatedUnion("type", [apiJetton, apiMemepadJetton]); export const getApiApiV3HybridSearchResponse = z.array(apiPolyJetton); 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 }; /** * Jetton hybrid search * * GET /api/v3/hybrid-search */ export const getApiApiV3HybridSearch = async (args: GetApiApiV3HybridSearchArgs, options?: GetApiApiV3HybridSearchOptions) => { const {baseUrl = "https://tokens.swap.coffee", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.search != null) query.append("search", String(args.search)) if (args.verification != null) query.append("verification", args.verification.join(',')) if (args.size != null) query.append("size", String(args.size)) if (args.page != null) query.append("page", String(args.page)) if (args.kind != null) query.append("kind", String(args.kind)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.direction != null) query.append("direction", String(args.direction)) if (ApiKey !== undefined) headers.set("X-Api-Key", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v3/hybrid-search${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(getApiApiV3HybridSearchResponse, await res.json()) };