import {z} from 'zod' export type SmartMoneyFilterType = 'Fund' | 'Smart Trader' | '30D Smart Trader' | '90D Smart Trader' | '180D Smart Trader' | 'Smart HL Perps Trader'; export type TradeType = 'Market' | 'Limit'; export type NumericRangeFilter = {min?: number | undefined, max?: number | undefined}; export type PositionSide = 'Long' | 'Short'; export type PerpActionType = 'Buy - Add Long' | 'Buy - Reduce Short' | 'Buy - Open Long' | 'Buy - Close Short' | 'Sell - Add Short' | 'Sell - Reduce Long' | 'Sell - Open Short' | 'Sell - Close Long'; export type SmartMoneyPerpTradesFilters = {include_smart_money_labels?: Array | undefined, exclude_smart_money_labels?: Array | undefined, trader_address?: (string | Array | null) | undefined, trader_address_label?: (string | Array | null) | undefined, token_symbol?: string | undefined, type?: TradeType | undefined, token_amount?: NumericRangeFilter | undefined, price_usd?: NumericRangeFilter | undefined, value_usd?: NumericRangeFilter | undefined, side?: (PositionSide | Array | null) | undefined, action?: (PerpActionType | Array | null) | undefined}; export type PaginationRequest = {page?: number | undefined, per_page?: number | undefined}; export type SmartMoneyPerpTradesSortField = 'block_timestamp' | 'token_amount' | 'price_usd' | 'value_usd'; export type SortDirection = 'ASC' | 'DESC'; export type SortOrderSmartMoneyPerpTradesSortField = {field: SmartMoneyPerpTradesSortField, direction: SortDirection}; export type SmartMoneyPerpTradesRequest = {filters?: SmartMoneyPerpTradesFilters | undefined, only_new_positions?: boolean | undefined, pagination?: PaginationRequest | undefined, order_by?: Array | undefined}; export type CreateApiApiV1SmartMoneyPerpTradesArgs = {body: SmartMoneyPerpTradesRequest}; export type CreateApiApiV1SmartMoneyPerpTradesOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const positionSide = z.enum(["Long", "Short"]); export const tradeType = z.enum(["Market", "Limit"]); export const smartMoneyPerpTrade = z.object({trader_address_label: z.string(), trader_address: z.string(), token_symbol: z.string(), side: positionSide.optional(), action: z.string(), token_amount: z.number().optional(), price_usd: z.number().optional(), value_usd: z.number().optional(), type: tradeType, block_timestamp: z.string(), transaction_hash: z.string()}); 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 smartMoneyPerpTradesResponse = z.object({data: z.array(smartMoneyPerpTrade), pagination: paginationInfo}); 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 Smart Money Perpetual Trades Data * * POST /api/v1/smart-money/perp-trades */ export const createApiApiV1SmartMoneyPerpTrades = async (args: CreateApiApiV1SmartMoneyPerpTradesArgs, options?: CreateApiApiV1SmartMoneyPerpTradesOptions) => { 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/smart-money/perp-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(smartMoneyPerpTradesResponse, await res.json()) };