import {z} from 'zod' export type ProfilerDexTradesChain = 'arbitrum' | 'avalanche' | 'base' | 'bnb' | 'ethereum' | 'hyperevm' | 'injective' | 'iotaevm' | 'linea' | 'mantle' | 'mantra' | 'monad' | 'near' | 'optimism' | 'plasma' | 'polygon' | 'robinhood' | 'sei' | 'solana' | 'sonic' | 'starknet' | 'sui' | 'ton' | 'tron'; export type DateRange = {from?: string | undefined, to?: string | undefined}; export type NumericRangeFilter = {min?: number | undefined, max?: number | undefined}; export type IntegerRangeFilter = {min?: number | undefined, max?: number | undefined}; export type ProfilerDexTradeFilters = {token_bought_address?: string | undefined, token_sold_address?: string | undefined, token_bought_symbol?: string | undefined, token_sold_symbol?: string | undefined, token_bought_amount?: NumericRangeFilter | undefined, token_sold_amount?: NumericRangeFilter | undefined, token_bought_age_days?: IntegerRangeFilter | undefined, token_sold_age_days?: IntegerRangeFilter | undefined, token_bought_market_cap?: NumericRangeFilter | undefined, token_sold_market_cap?: NumericRangeFilter | undefined, token_bought_fdv?: NumericRangeFilter | undefined, token_sold_fdv?: NumericRangeFilter | undefined, trade_value_usd?: NumericRangeFilter | undefined}; export type PaginationRequest = {page?: number | undefined, per_page?: number | undefined}; export type ProfilerDexTradeSortField = 'chain' | 'block_timestamp' | 'transaction_hash' | 'token_bought_amount' | 'token_sold_amount' | 'token_bought_symbol' | 'token_sold_symbol' | 'token_bought_age_days' | 'token_sold_age_days' | 'token_bought_market_cap' | 'token_sold_market_cap' | 'token_bought_fdv' | 'token_sold_fdv' | 'trade_value_usd'; export type SortDirection = 'ASC' | 'DESC'; export type SortOrderProfilerDexTradeSortField = {field: ProfilerDexTradeSortField, direction: SortDirection}; export type ProfilerDexTradeRequest = {address: string, chain: ProfilerDexTradesChain, date: DateRange, filters?: ProfilerDexTradeFilters | undefined, pagination?: PaginationRequest | undefined, order_by?: Array | undefined}; export type CreateApiApiV1ProfilerDexTradesArgs = {body: ProfilerDexTradeRequest}; export type CreateApiApiV1ProfilerDexTradesOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const paginationInfo = z.object({page: z.number().int().gte(1).optional(), per_page: z.number().int().gte(1).lte(1000).optional(), is_last_page: z.boolean().optional()}); export const profilerDexTrade = z.object({chain: z.string(), block_timestamp: z.string(), transaction_hash: z.string(), trader_address: z.string(), trader_address_label: z.string().optional(), token_bought_address: z.string(), token_sold_address: z.string(), token_bought_amount: z.number().optional(), token_sold_amount: z.number().optional(), token_bought_symbol: z.string().optional(), token_sold_symbol: z.string().optional(), token_bought_age_days: z.number().int().optional(), token_sold_age_days: z.number().int().optional(), token_bought_market_cap: z.number().optional(), token_sold_market_cap: z.number().optional(), token_bought_fdv: z.number().optional(), token_sold_fdv: z.number().optional(), trade_value_usd: z.number().optional()}); export const profilerDexTradeResponse = z.object({pagination: paginationInfo, data: z.array(profilerDexTrade)}); 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 Wallet DEX Trades * * POST /api/v1/profiler/dex-trades */ export const createApiApiV1ProfilerDexTrades = async (args: CreateApiApiV1ProfilerDexTradesArgs, options?: CreateApiApiV1ProfilerDexTradesOptions) => { 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/profiler/dex-trades${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(profilerDexTradeResponse, await res.json()) };