UNPKG

@iqai/mcp-near-agent

Version:
122 lines (121 loc) 3.28 kB
import { EventEmitter } from "node:events"; import * as cron from "node-cron"; import type { AgentEvent } from "../types.js"; import type { EventSubscription } from "./subscription-manager.js"; export interface BlockProcessingState { lastBlockHeight: number; isProcessing: boolean; processedTransactionIds: Set<string>; } export interface EventListenerConfig { networkId?: string; nodeUrl?: string; gasLimit?: string; } export interface EventListenerEvents { "event:found": [data: { event: AgentEvent; subscription: EventSubscription; }]; "event:error": [data: { subscription: EventSubscription; error: unknown; }]; "block:processed": [ data: { blockHeight: number; subscriptionId: string; eventsFound: number; } ]; "block:error": [ data: { blockHeight: number; subscriptionId: string; error: unknown; } ]; "polling:started": [subscriptionId: string]; "polling:completed": [ data: { subscriptionId: string; blocksProcessed: number; eventsFound: number; } ]; "state:initialized": [subscriptionId: string]; } export declare class EventListener extends EventEmitter<EventListenerEvents> { private static readonly BATCH_SIZE; private static readonly POLL_DELAY; private authManager; private processingStates; constructor(config?: EventListenerConfig); /** * Initialize the NEAR connection using AuthManager */ initialize(): Promise<void>; /** * Get the NEAR account from AuthManager */ private getAccount; /** * Start listening for events for a specific subscription */ startListening(subscription: EventSubscription): cron.ScheduledTask; /** * Stop listening for a specific subscription */ stopListening(subscriptionId: string): void; /** * Poll for events for a specific subscription */ private pollEventsForSubscription; /** * Process a single block for events */ private processBlock; /** * Get the current block and initialize lastBlockHeight if needed */ private getCurrentBlock; /** * Get relevant receipts for a contract from a block */ private getRelevantReceipts; /** * Process receipts and extract events */ private processReceipts; /** * Extract events from a receipt using NearBlocks API */ private extractEventsFromReceipt; /** * Parse an event log and return an AgentEvent if it matches */ private parseEventLog; /** * Get processing statistics */ getStats(): { isInitialized: boolean; activeSubscriptions: number; authManagerStatus: { isInitialized: boolean; hasAccount: boolean; accountId: string | null; networkId: string; }; processingStates: { subscriptionId: string; lastBlockHeight: number; isProcessing: boolean; processedTransactionCount: number; }[]; }; /** * Cleanup resources */ cleanup(): void; }