import {z} from 'zod' export type GetApiOnchainNetworksNetworkTokensAddressArgs = {network: string, address: string, include?: 'top_pools' | undefined, include_composition?: boolean | undefined, include_inactive_source?: boolean | undefined}; export type GetApiOnchainNetworksNetworkTokensAddressOptions = {baseUrl?: string | undefined, headerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const tokenItem = z.object({id: z.string(), type: z.string(), attributes: z.object({address: z.string(), name: z.string(), symbol: z.string(), decimals: z.number().int(), image_url: z.string().nullable(), coingecko_coin_id: z.string().nullable(), total_supply: z.string(), normalized_total_supply: z.string(), price_usd: z.string().nullable(), fdv_usd: z.string().nullable(), total_reserve_in_usd: z.string(), volume_usd: z.object({h24: z.string().optional()}), market_cap_usd: z.string().nullable(), last_trade_timestamp: z.string().optional(), launchpad_details: z.object({graduation_percentage: z.number().optional(), completed: z.boolean().optional(), completed_at: z.string().nullable().optional(), migrated_destination_pool_address: z.string().nullable().optional()}).optional()}), relationships: z.object({top_pools: z.object({data: z.array(z.object({id: z.string().optional(), type: z.string().optional()})).optional()}).optional()})}); export const tokenData = z.object({data: tokenItem, included: z.array(z.object({id: z.string().optional(), type: z.string().optional(), attributes: z.object({base_token_price_usd: z.string().optional(), base_token_price_native_currency: z.string().optional(), quote_token_price_usd: z.string().optional(), quote_token_price_native_currency: z.string().optional(), base_token_price_quote_token: z.string().optional(), quote_token_price_base_token: z.string().optional(), address: z.string().optional(), name: z.string().optional(), pool_created_at: z.string().optional(), token_price_usd: z.string().optional(), fdv_usd: z.string().nullable().optional(), market_cap_usd: z.string().nullable().optional(), price_change_percentage: z.object({m5: z.string().optional(), m15: z.string().optional(), m30: z.string().optional(), h1: z.string().optional(), h6: z.string().optional(), h24: z.string().optional()}).optional(), transactions: z.object({m5: z.object({buys: z.number().int().optional(), sells: z.number().int().optional(), buyers: z.number().int().optional(), sellers: z.number().int().optional()}).optional(), m15: z.object({buys: z.number().int().optional(), sells: z.number().int().optional(), buyers: z.number().int().optional(), sellers: z.number().int().optional()}).optional(), m30: z.object({buys: z.number().int().optional(), sells: z.number().int().optional(), buyers: z.number().int().optional(), sellers: z.number().int().optional()}).optional(), h1: z.object({buys: z.number().int().optional(), sells: z.number().int().optional(), buyers: z.number().int().optional(), sellers: z.number().int().optional()}).optional(), h6: z.object({buys: z.number().int().optional(), sells: z.number().int().optional(), buyers: z.number().int().optional(), sellers: z.number().int().optional()}).optional(), h24: z.object({buys: z.number().int().optional(), sells: z.number().int().optional(), buyers: z.number().int().optional(), sellers: z.number().int().optional()}).optional()}).optional(), volume_usd: z.object({m5: z.string().optional(), m15: z.string().optional(), m30: z.string().optional(), h1: z.string().optional(), h6: z.string().optional(), h24: z.string().optional()}).optional(), reserve_in_usd: z.string().optional(), last_trade_timestamp: z.string().optional()}).optional(), relationships: z.object({base_token: z.object({data: z.object({id: z.string().optional(), type: z.string().optional()}).optional()}).optional(), quote_token: z.object({data: z.object({id: z.string().optional(), type: z.string().optional()}).optional()}).optional(), dex: z.object({data: z.object({id: z.string().optional(), type: z.string().optional()}).optional()}).optional()}).optional()})).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 }; /** * Token Data by Token Address * * GET /onchain/networks/{network}/tokens/{address} * * Also accepts auth: queryAuth */ export const getApiOnchainNetworksNetworkTokensAddress = async (args: GetApiOnchainNetworksNetworkTokensAddressArgs, options?: GetApiOnchainNetworksNetworkTokensAddressOptions) => { const {baseUrl = "https://api.coingecko.com/api/v3", fetch: fetchFn = fetch, headerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.include != null) query.append("include", String(args.include)) if (args.include_composition != null) query.append("include_composition", String(args.include_composition)) if (args.include_inactive_source != null) query.append("include_inactive_source", String(args.include_inactive_source)) if (headerAuth !== undefined) headers.set("x-cg-pro-api-key", headerAuth) const url = `${baseUrl.replace(/\/+$/, '')}/onchain/networks/${encodeURIComponent(String(args.network))}/tokens/${encodeURIComponent(String(args.address))}${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(tokenData, await res.json()) };