import {z} from 'zod' export type TGMChain = 'arbitrum' | 'avalanche' | 'base' | 'bnb' | 'ethereum' | 'hyperevm' | 'hyperliquid' | 'injective' | 'iotaevm' | 'linea' | 'mantle' | 'mantra' | 'monad' | 'near' | 'optimism' | 'plasma' | 'polygon' | 'robinhood' | 'sei' | 'solana' | 'sonic' | 'starknet' | 'sui' | 'ton' | 'tron'; export type TGMIndicatorsRequest = {chain: TGMChain, token_address: string}; export type CreateApiApiV1TgmIndicatorsArgs = {body: TGMIndicatorsRequest}; export type CreateApiApiV1TgmIndicatorsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const tGMIndicatorTokenInfo = z.object({market_cap_usd: z.number().optional(), market_cap_group: z.string().optional(), is_stablecoin: z.boolean().optional()}); export const tGMIndicator = z.object({indicator_type: z.string(), score: z.string().optional(), signal: z.number().optional(), signal_percentile: z.number().optional(), last_trigger_on: z.string().optional()}); export const tGMIndicatorsResponse = z.object({token_address: z.string(), chain: z.string(), token_info: tGMIndicatorTokenInfo, risk_indicators: z.array(tGMIndicator), reward_indicators: z.array(tGMIndicator)}); 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 Nansen Score Indicators * * POST /api/v1/tgm/indicators */ export const createApiApiV1TgmIndicators = async (args: CreateApiApiV1TgmIndicatorsArgs, options?: CreateApiApiV1TgmIndicatorsOptions) => { const {baseUrl = "https://api.nansen.ai", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("apiKey", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/tgm/indicators${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(tGMIndicatorsResponse, await res.json()) };