import {z} from 'zod' export type TGMPerpPositionsLabel = 'smart_money' | 'all_traders' | 'whale' | 'public_figure'; export type PaginationRequest = {page?: number | undefined, per_page?: number | undefined}; export type LabelType = '30D Smart Trader' | '90D Smart Trader' | '180D Smart Trader' | 'Fund' | 'Smart Trader' | 'Public Figure' | 'Exchange' | 'Whale' | 'BananaGun Bot User' | 'Top Maestro Bot User' | 'Top BananaGun Bot User' | 'Maestro Bot User' | 'Early MAGIC Miner' | 'First Mover LP' | 'First Mover Staking' | 'Profitable LP' | 'Smart HL Perps Trader'; export type PositionSide = 'Long' | 'Short'; export type NumericRangeFilter = {min?: number | undefined, max?: number | undefined}; export type TGMPerpPositionsFilters = {include_smart_money_labels?: Array | undefined, exclude_smart_money_labels?: Array | undefined, address?: (string | Array | null) | undefined, address_label?: (string | Array | null) | undefined, side?: (PositionSide | Array | null) | undefined, position_value_usd?: NumericRangeFilter | undefined, position_size?: NumericRangeFilter | undefined, entry_price?: NumericRangeFilter | undefined, upnl_usd?: NumericRangeFilter | undefined, funding_usd?: NumericRangeFilter | undefined}; export type TGMPerpPositionsSortField = 'address' | 'address_label' | 'side' | 'position_value_usd' | 'position_size' | 'leverage' | 'entry_price' | 'mark_price' | 'liquidation_price' | 'funding_usd' | 'upnl_usd'; export type SortDirection = 'ASC' | 'DESC'; export type SortOrderTGMPerpPositionsSortField = {field: TGMPerpPositionsSortField, direction: SortDirection}; export type TGMPerpPositionsRequest = {token_symbol: string, label_type?: TGMPerpPositionsLabel | undefined, pagination?: PaginationRequest | undefined, filters?: TGMPerpPositionsFilters | undefined, order_by?: Array | undefined}; export type CreateApiApiV1TgmPerpPositionsArgs = {body: TGMPerpPositionsRequest}; export type CreateApiApiV1TgmPerpPositionsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const positionSide = z.enum(["Long", "Short"]); export const tGMPerpPosition = z.object({address: z.string().optional(), address_label: z.string().optional(), side: positionSide.optional(), position_value_usd: z.number().optional(), position_size: z.number().optional(), leverage: z.string(), leverage_type: z.string().optional(), entry_price: z.number().optional(), mark_price: z.number().optional(), liquidation_price: z.number().optional(), funding_usd: z.number().optional(), upnl_usd: z.number().optional()}); 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 tGMPerpPositionsResponse = z.object({data: z.array(tGMPerpPosition), 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 "Token God Mode" (TGM) perp positions data * * POST /api/v1/tgm/perp-positions */ export const createApiApiV1TgmPerpPositions = async (args: CreateApiApiV1TgmPerpPositionsArgs, options?: CreateApiApiV1TgmPerpPositionsOptions) => { 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/perp-positions${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(tGMPerpPositionsResponse, await res.json()) };