Trong bối cảnh DeFi tiếp tục bùng nổ với khối lượng giao dịch trên Hyperliquid đạt $2.3 tỷ/chỉ trong tuần đầu tháng 6/2026, việc xây dựng hệ thống real-time data pipeline không còn là lựa chọn mà là yêu cầu bắt buộc cho các nhà phát triển, trader và dự án muốn tận dụng lợi thế cạnh tranh.

Bài viết này sẽ hướng dẫn chi tiết cách xây dựng Hyperliquid Trade Data Real-time Scraping System với chi phí tối ưu nhất, so sánh giữa các giải pháp và đặc biệt là cách HolySheep AI có thể giúp bạn tiết kiệm đến 85% chi phí vận hành.

So Sánh Chi Phí và Hiệu Suất: HolySheep vs Giải Pháp Khác

Tiêu chí HolySheep AI API Chính Thức Hyperliquid Dịch Vụ Relay (QuickNode, Alchemy) Tự xây Node
Chi phí hàng tháng $15-50 (tùy gói) Miễn phí nhưng rate limit nghiêm ngặt $100-500/tháng $200-800 (server + điện)
Độ trễ trung bình <50ms 100-300ms 80-150ms 30-80ms
Rate limit 10,000 req/phút 100 req/phút 5,000 req/phút Tự quản lý
Data persistence Tự động lưu trữ 90 ngày Không có Có (tối đa 30 ngày) Tùy cấu hình
Thanh toán CNY/USD, WeChat/Alipay, Visa Chỉ crypto Chỉ crypto/thẻ quốc tế Chỉ crypto
Setup time 5 phút 30 phút 2-4 giờ 1-3 ngày
Hỗ trợ WebSocket Có, real-time streaming Hạn chế Phải tự cấu hình

Hyperliquid Data Architecture Tổng Quan

Trước khi đi vào chi tiết code, chúng ta cần hiểu cấu trúc dữ liệu Hyperliquid cung cấp:

Hyperliquid Mainnet Contract: 0x...
Supported Assets: BTC, ETH, SOL, ARB, LINK, DOGE, AAVE, etc.

Data Endpoints:
├── /info - User info, balances
├── /funding - Funding rate history
├── /candles - OHLCV data (historical)
├── /trades - Real-time trade feeds
└── /l2Book - Order book snapshots

WebSocket Streams:
├── on-chain-data - Chain state updates
├── trades - Live trade executions
├── user-events - User-specific events
└── orderUpdates - Order status changes

Xây Dựng Real-Time Trade Scraper Với Node.js

Dưới đây là code hoàn chỉnh để xây dựng hệ thống thu thập dữ liệu giao dịch Hyperliquid theo thời gian thực:

// hyperliquid_trade_scraper.js
// Real-time Hyperliquid Trade Data Scraper với HolySheep AI Integration

const WebSocket = require('ws');
const axios = require('axios');
const { Pool } = require('pg');

// === CẤU HÌNH ===
const CONFIG = {
  // Hyperliquid WebSocket
  HYPERLIQUID_WS: 'wss://api.hyperliquid.xyz/ws',
  
  // HolySheep AI cho data processing
  HOLYSHEEP_BASE_URL: 'https://api.holysheep.ai/v1',
  HOLYSHEEP_API_KEY: process.env.HOLYSHEEP_API_KEY || 'YOUR_HOLYSHEEP_API_KEY',
  
  // Database PostgreSQL
  DB_CONFIG: {
    host: process.env.DB_HOST || 'localhost',
    port: 5432,
    database: 'hyperliquid_data',
    user: process.env.DB_USER,
    password: process.env.DB_PASSWORD
  },
  
  // Trading pairs cần theo dõi
  MONITORED_PAIRS: ['BTC', 'ETH', 'SOL', 'ARB', 'LINK'],
  
  // Batch size cho bulk insert
  BATCH_SIZE: 100,
  BATCH_INTERVAL_MS: 5000
};

class HyperliquidTradeScraper {
  constructor() {
    this.ws = null;
    this.tradeBuffer = [];
    this.lastPingTime = Date.now();
    this.reconnectAttempts = 0;
    this.maxReconnectAttempts = 10;
    
    this.pool = new Pool(CONFIG.DB_CONFIG);
    this.initializeDatabase();
  }

  async initializeDatabase() {
    const createTableQuery = `
      CREATE TABLE IF NOT EXISTS hyperliquid_trades (
        id SERIAL PRIMARY KEY,
        trade_id VARCHAR(64) UNIQUE,
        pair VARCHAR(20) NOT NULL,
        side VARCHAR(4) NOT NULL,  -- 'B' or 'S'
        price DECIMAL(20, 8) NOT NULL,
        size DECIMAL(20, 8) NOT NULL,
        timestamp BIGINT NOT NULL,
        is_auction BOOLEAN DEFAULT FALSE,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        INDEX idx_pair_time (pair, timestamp DESC),
        INDEX idx_timestamp (timestamp DESC)
      );

      CREATE TABLE IF NOT EXISTS hyperliquid_aggregated (
        id SERIAL PRIMARY KEY,
        pair VARCHAR(20) NOT NULL,
        window_start TIMESTAMP NOT NULL,
        open DECIMAL(20, 8),
        high DECIMAL(20, 8),
        low DECIMAL(20, 8),
        close DECIMAL(20, 8),
        volume DECIMAL(20, 8),
        trade_count INTEGER,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        UNIQUE(pair, window_start)
      );
    `;
    
    try {
      await this.pool.query(createTableQuery);
      console.log('✅ Database initialized successfully');
    } catch (error) {
      console.error('❌ Database initialization failed:', error.message);
    }
  }

  connect() {
    this.ws = new WebSocket(CONFIG.HYPERLIQUID_WS);

    this.ws.on('open', () => {
      console.log('🔌 Connected to Hyperliquid WebSocket');
      
      // Subscribe to trades channel
      const subscribeMsg = {
        method: 'subscribe',
        subscription: { type: 'trades', coin: CONFIG.MONITORED_PAIRS }
      };
      
      this.ws.send(JSON.stringify(subscribeMsg));
      console.log(📡 Subscribed to trades for: ${CONFIG.MONITORED_PAIRS.join(', ')});
      
      // Start batch insert interval
      this.batchInsertInterval = setInterval(
        () => this.flushTradeBuffer(),
        CONFIG.BATCH_INTERVAL_MS
      );
    });

    this.ws.on('message', async (data) => {
      try {
        const message = JSON.parse(data);
        
        if (message.channel === 'trades' && message.data) {
          this.processTrades(message.data);
        }
        
        // Handle subscription confirmation
        if (message.method === 'subscribed') {
          console.log(✅ Subscription confirmed: ${JSON.stringify(message.subscription)});
        }
        
      } catch (error) {
        console.error('❌ Error parsing message:', error.message);
      }
    });

    this.ws.on('close', () => {
      console.log('⚠️ WebSocket disconnected, attempting reconnect...');
      this.handleReconnect();
    });

    this.ws.on('error', (error) => {
      console.error('❌ WebSocket error:', error.message);
    });
  }

  processTrades(trades) {
    if (!Array.isArray(trades)) {
      trades = [trades];
    }

    trades.forEach(trade => {
      const tradeRecord = {
        trade_id: ${trade.tid}_${trade.sz},
        pair: trade.coin,
        side: trade.side === 'B' ? 'BUY' : 'SELL',
        price: parseFloat(trade.px) / 1e8,  // Hyperliquid uses 8 decimal places
        size: parseFloat(trade.sz),
        timestamp: trade.time,
        is_auction: trade.closedAt !== undefined
      };

      this.tradeBuffer.push(tradeRecord);

      // Log large trades (> $10,000)
      const tradeValue = tradeRecord.price * tradeRecord.size;
      if (tradeValue > 10000) {
        console.log(🔴 LARGE TRADE: ${tradeRecord.side} ${tradeRecord.size} ${tradeRecord.pair} @ $${tradeValue.toLocaleString()});
      }
    });
  }

  async flushTradeBuffer() {
    if (this.tradeBuffer.length === 0) return;

    const tradesToInsert = [...this.tradeBuffer];
    this.tradeBuffer = [];

    const values = tradesToInsert.map(t => 
      ('${t.trade_id}', '${t.pair}', '${t.side}', ${t.price}, ${t.size}, ${t.timestamp}, ${t.is_auction})
    ).join(',');

    const insertQuery = `
      INSERT INTO hyperliquid_trades (trade_id, pair, side, price, size, timestamp, is_auction)
      VALUES ${values}
      ON CONFLICT (trade_id) DO NOTHING
      RETURNING id
    `;

    try {
      const result = await this.pool.query(insertQuery);
      console.log(✅ Inserted ${result.rowCount}/${tradesToInsert.length} trades);
    } catch (error) {
      console.error('❌ Batch insert failed:', error.message);
      // Re-add to buffer for retry
      this.tradeBuffer = [...tradesToInsert, ...this.tradeBuffer];
    }
  }

  handleReconnect() {
    if (this.reconnectAttempts >= this.maxReconnectAttempts) {
      console.error('❌ Max reconnection attempts reached');
      return;
    }

    const delay = Math.min(1000 * Math.pow(2, this.reconnectAttempts), 30000);
    console.log(🔄 Reconnecting in ${delay}ms (attempt ${this.reconnectAttempts + 1}/${this.maxReconnectAttempts}));
    
    this.reconnectAttempts++;
    setTimeout(() => this.connect(), delay);
  }

  // === HOLYSHEEP AI INTEGRATION ===
  async analyzeTradesWithAI(trades) {
    try {
      const response = await axios.post(
        ${CONFIG.HOLYSHEEP_BASE_URL}/chat/completions,
        {
          model: 'gpt-4.1',
          messages: [
            {
              role: 'system',
              content: 'You are a crypto trading analyst. Analyze these Hyperliquid trades and provide insights.'
            },
            {
              role: 'user',
              content: Analyze these recent trades and identify patterns:\n${JSON.stringify(trades.slice(0, 20), null, 2)}
            }
          ],
          max_tokens: 500
        },
        {
          headers: {
            'Authorization': Bearer ${CONFIG.HOLYSHEEP_API_KEY},
            'Content-Type': 'application/json'
          }
        }
      );

      return response.data.choices[0].message.content;
    } catch (error) {
      console.error('❌ HolySheep AI analysis failed:', error.message);
      return null;
    }
  }

  async getMarketSentiment(pair) {
    try {
      const recentTrades = await this.pool.query(
        `SELECT side, SUM(size * price) as volume 
         FROM hyperliquid_trades 
         WHERE pair = $1 AND timestamp > $2 
         GROUP BY side`,
        [pair, Date.now() - 3600000] // Last 1 hour
      );

      const buyVolume = recentTrades.rows
        .filter(r => r.side === 'BUY')
        .reduce((sum, r) => sum + parseFloat(r.volume), 0);
      
      const sellVolume = recentTrades.rows
        .filter(r => r.side === 'SELL')
        .reduce((sum, r) => sum + parseFloat(r.volume), 0);

      return {
        pair,
        buyVolume,
        sellVolume,
        sentiment: buyVolume > sellVolume ? 'BULLISH' : 'BEARISH',
        ratio: buyVolume / (sellVolume || 1)
      };
    } catch (error) {
      console.error('❌ Sentiment analysis failed:', error.message);
      return null;
    }
  }

  async shutdown() {
    console.log('🛑 Shutting down gracefully...');
    await this.flushTradeBuffer();
    clearInterval(this.batchInsertInterval);
    await this.pool.end();
    this.ws?.close();
  }
}

// === KHỞI CHẠY ===
const scraper = new HyperliquidTradeScraper();

process.on('SIGINT', async () => {
  await scraper.shutdown();
  process.exit(0);
});

scraper.connect();

// Export for testing
module.exports = { HyperliquidTradeScraper, CONFIG };

Python Implementation cho Data Processing Pipeline

Phiên bản Python này sử dụng async/await cho hiệu suất cao hơn và tích hợp trực tiếp với HolySheep AI để xử lý ngôn ngữ tự nhiên:

# hyperliquid_processor.py

Python async pipeline cho Hyperliquid data processing với HolySheep AI

import asyncio import json import time from datetime import datetime, timedelta from typing import List, Dict, Optional from dataclasses import dataclass, asdict import asyncpg import aiohttp import websockets from websockets.exceptions import ConnectionClosed import logging

Configure logging

logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s' ) logger = logging.getLogger(__name__) @dataclass class Trade: trade_id: str pair: str side: str price: float size: float timestamp: int is_auction: bool = False @dataclass class Candle: pair: str open: float high: float low: float close: float volume: float trade_count: int timestamp: int class HyperliquidProcessor: """ Real-time Hyperliquid data processor với HolySheep AI integration Chi phí: ~$0.02/giờ với HolySheep thay vì $0.50+ với AWS Lambda """ def __init__( self, holysheep_api_key: str, db_url: str, monitored_pairs: List[str] = None ): self.holysheep_api_key = holysheep_api_key self.holysheep_base_url = "https://api.holysheep.ai/v1" self.db_url = db_url self.monitored_pairs = monitored_pairs or ["BTC", "ETH", "SOL", "ARB"] self.ws_url = "wss://api.hyperliquid.xyz/ws" self.websocket = None self.db_pool: Optional[asyncpg.Pool] = None # Buffering configuration self.trade_buffer: List[Trade] = [] self.buffer_size = 500 self.buffer_timeout = 5.0 # seconds self.last_flush = time.time() # Metrics self.metrics = { "trades_processed": 0, "trades_inserted": 0, "errors": 0, "last_ai_analysis": None } async def initialize_db(self): """Khởi tạo PostgreSQL connection pool""" self.db_pool = await asyncpg.create_pool( self.db_url, min_size=5, max_size=20 ) # Create tables async with self.db_pool.acquire() as conn: await conn.execute(""" CREATE TABLE IF NOT EXISTS hyperliquid_trades ( id BIGSERIAL PRIMARY KEY, trade_id VARCHAR(64) UNIQUE, pair VARCHAR(20) NOT NULL, side VARCHAR(4) NOT NULL, price DECIMAL(20, 8) NOT NULL, size DECIMAL(20, 8) NOT NULL, timestamp BIGINT NOT NULL, is_auction BOOLEAN DEFAULT FALSE, created_at TIMESTAMPTZ DEFAULT NOW() ); CREATE TABLE IF NOT EXISTS hyperliquid_candles_1m ( id BIGSERIAL PRIMARY KEY, pair VARCHAR(20) NOT NULL, timestamp TIMESTAMPTZ NOT NULL, open DECIMAL(20, 8) NOT NULL, high DECIMAL(20, 8) NOT NULL, low DECIMAL(20, 8) NOT NULL, close DECIMAL(20, 8) NOT NULL, volume DECIMAL(20, 8) NOT NULL, trade_count INTEGER NOT NULL, created_at TIMESTAMPTZ DEFAULT NOW(), UNIQUE(pair, timestamp) ); CREATE INDEX IF NOT EXISTS idx_trades_pair_time ON hyperliquid_trades(pair, timestamp DESC); CREATE INDEX IF NOT EXISTS idx_candles_pair_time ON hyperliquid_candles_1m(pair, timestamp DESC); """) logger.info("✅ Database initialized") async def connect_websocket(self): """Kết nối WebSocket đến Hyperliquid""" while True: try: self.websocket = await websockets.connect( self.ws_url, ping_interval=20, ping_timeout=10 ) # Subscribe to trades subscribe_msg = { "method": "subscribe", "subscription": { "type": "trades", "coin": self.monitored_pairs } } await self.websocket.send(json.dumps(subscribe_msg)) logger.info(f"📡 Subscribed to: {self.monitored_pairs}") return except Exception as e: logger.error(f"❌ WebSocket connection failed: {e}") await asyncio.sleep(5) async def process_websocket_messages(self): """Xử lý messages từ WebSocket""" buffer_task = asyncio.create_task(self._buffer_flusher()) try: async for message in self.websocket: try: data = json.loads(message) await self._handle_message(data) except json.JSONDecodeError as e: logger.error(f"❌ JSON decode error: {e}") self.metrics["errors"] += 1 except ConnectionClosed as e: logger.warning(f"⚠️ WebSocket closed: {e}") finally: buffer_task.cancel() await self.flush_buffer() async def _handle_message(self, data: dict): """Xử lý incoming message""" if data.get("channel") == "trades": trades = data.get("data", []) if not isinstance(trades, list): trades = [trades] for trade_data in trades: trade = self._parse_trade(trade_data) if trade: self.trade_buffer.append(trade) self.metrics["trades_processed"] += 1 # Check for whale trades (> $50k) trade_value = trade.price * trade.size if trade_value > 50000: logger.warning( f"🐋 WHALE: {trade.side} {trade.size} {trade.pair} " f"@ ${trade_value:,.0f}" ) def _parse_trade(self, data: dict) -> Optional[Trade]: """Parse trade data từ Hyperliquid""" try: # Convert price from 8 decimal places price = int(data["px"]) / 1e8 size = float(data["sz"]) return Trade( trade_id=f"{data['tid']}_{data['sz']}", pair=data["coin"], side="BUY" if data["side"] == "B" else "SELL", price=price, size=size, timestamp=data["time"], is_auction=data.get("closedAt") is not None ) except (KeyError, ValueError) as e: logger.error(f"❌ Trade parse error: {e}") return None async def _buffer_flusher(self): """Background task để flush buffer theo timeout""" while True: await asyncio.sleep(1) if (time.time() - self.last_flush >= self.buffer_timeout and len(self.trade_buffer) > 0): await self.flush_buffer() async def flush_buffer(self): """Flush trade buffer vào database""" if not self.trade_buffer or not self.db_pool: return trades = self.trade_buffer[:] self.trade_buffer = [] self.last_flush = time.time() try: async with self.db_pool.acquire() as conn: values = [ ( t.trade_id, t.pair, t.side, str(t.price), str(t.size), t.timestamp, t.is_auction ) for t in trades ] await conn.executemany(""" INSERT INTO hyperliquid_trades (trade_id, pair, side, price, size, timestamp, is_auction) VALUES ($1, $2, $3, $4::decimal, $5::decimal, $6, $7) ON CONFLICT (trade_id) DO NOTHING """, values) self.metrics["trades_inserted"] += len(values) logger.info(f"✅ Flushed {len(values)} trades to DB") except Exception as e: logger.error(f"❌ Buffer flush failed: {e}") # Re-add to buffer self.trade_buffer = trades + self.trade_buffer self.metrics["errors"] += 1 # === HOLYSHEEP AI INTEGRATION === async def analyze_market_with_ai(self, pair: str) -> Optional[str]: """ Sử dụng HolySheep AI để phân tích thị trường Chi phí: ~$0.001 cho mỗi analysis với DeepSeek V3.2 """ try: # Get recent trades async with self.db_pool.acquire() as conn: recent_trades = await conn.fetch(""" SELECT * FROM hyperliquid_trades WHERE pair = $1 AND timestamp > $2 ORDER BY timestamp DESC LIMIT 50 """, pair, int((time.time() - 300) * 1000)) # Last 5 minutes if len(recent_trades) < 10: return None # Calculate stats buy_volume = sum( float(t['price']) * float(t['size']) for t in recent_trades if t['side'] == 'BUY' ) sell_volume = sum( float(t['price']) * float(t['size']) for t in recent_trades if t['side'] == 'SELL' ) prompt = f"""Analyze this {pair} trading data from Hyperliquid: Recent Activity (last 5 minutes): - Total trades: {len(recent_trades)} - Buy volume: ${buy_volume:,.2f} - Sell volume: ${sell_volume:,.2f} - Buy/Sell ratio: {buy_volume/(sell_volume or 1):.2f} Provide a brief market sentiment analysis (3-5 sentences).""" async with aiohttp.ClientSession() as session: async with session.post( f"{self.holysheep_base_url}/chat/completions", headers={ "Authorization": f"Bearer {self.holysheep_api_key}", "Content-Type": "application/json" }, json={ "model": "deepseek-v3.2", # $0.42/M token - siêu tiết kiệm! "messages": [ {"role": "system", "content": "You are a crypto trading analyst."}, {"role": "user", "content": prompt} ], "max_tokens": 200 } ) as response: if response.status == 200: result = await response.json() analysis = result['choices'][0]['message']['content'] self.metrics["last_ai_analysis"] = time.time() return analysis else: logger.error(f"❌ AI API error: {response.status}") return None except Exception as e: logger.error(f"❌ AI analysis failed: {e}") return None async def run(self): """Main entry point""" await self.initialize_db() await self.connect_websocket() logger.info("🚀 Hyperliquid Processor started") # Run WebSocket listener and periodic AI analysis concurrently await asyncio.gather( self.process_websocket_messages(), self._run_periodic_analysis() ) async def _run_periodic_analysis(self): """Chạy AI analysis định kỳ mỗi 5 phút""" while True: await asyncio.sleep(300) # 5 minutes for pair in self.monitored_pairs: analysis = await self.analyze_market_with_ai(pair) if analysis: logger.info(f"📊 AI Analysis for {pair}: {analysis[:100]}...") async def main(): processor = HyperliquidProcessor( holysheep_api_key="YOUR_HOLYSHEEP_API_KEY", db_url="postgresql://user:pass@localhost:5432/hyperliquid", monitored_pairs=["BTC", "ETH", "SOL", "ARB", "LINK"] ) try: await processor.run() except KeyboardInterrupt: logger.info("🛑 Shutting down...") if processor.db_pool: await processor.db_pool.close() if __name__ == "__main__": asyncio.run(main())

Lỗi Thường Gặp và Cách Khắc Phục

1. Lỗi WebSocket Reconnection Liên Tục

// ❌ VẤN ĐỀ: WebSocket bị disconnect liên tục, không reconnect được
// Nguyên nhân: Rate limit, network instability, hoặc subscription format sai

// ✅ GIẢI PHÁP: Implement exponential backoff với proper error handling

class WebSocketManager {
  constructor() {
    this.maxRetries = 5;
    this.baseDelay = 1000;
    this.maxDelay = 30000;
    this.retryCount = 0;
  }

  async connect() {
    try {
      this.ws = new WebSocket(CONFIG.HYPERLIQUID_WS);
      
      // Add heartbeat để detect dead connections
      this.ws.on('pong', () => {
        this.lastPong = Date.now();
      });
      
      // Heartbeat every 30 seconds
      setInterval(() => {
        if (this.ws.readyState === WebSocket.OPEN) {
          if (Date.now() - this.lastPong > 60000) {
            console.log('⚠️ Heartbeat timeout, reconnecting...');
            this.ws.terminate();
          } else {
            this.ws.ping();
          }
        }
      }, 30000);
      
    } catch (error) {
      await this.handleReconnect(error);
    }
  }

  async handleReconnect(error) {
    if (this.retryCount >= this.maxRetries) {
      console.error('❌ Max retries reached, escalating...');
      await this.escalateIssue(error);
      return;
    }

    // Exponential backoff: 1s, 2s, 4s, 8s, 16s (max 30s)
    const delay = Math.min(
      this.baseDelay * Math.pow(2, this.retryCount),
      this.maxDelay
    );
    
    console.log(🔄 Retry ${this.retryCount + 1}/${this.maxRetries} in ${delay}ms);
    this.retryCount++;
    
    await new Promise(resolve => setTimeout(resolve, delay));
    await this.connect();
  }
}

2. Lỗi Database Connection Pool Exhaustion

-- ❌ VẤN ĐỀ: PostgreSQL connection pool bị exhaustion
-- Nguyên nhân: Quá nhiều concurrent queries hoặc connections không được release

-- ✅ GIẢI PHÁP: Sử dụng connection pool với proper lifecycle management

-- PostgreSQL configuration cho high-throughput workload:
ALTER SYSTEM SET max_connections = 200;
ALTER SYSTEM SET shared_buffers = '2GB';
ALTER SYSTEM SET effective_cache_size = '6GB';
ALTER SYSTEM SET work_mem = '50MB';
ALTER SYSTEM SET maintenance_work_mem = '512MB';

-- JavaScript: Enhanced connection pool với retry logic
const { Pool } = require('pg');

class DatabaseManager {
  constructor() {
    this.pool = new Pool({
      host: process.env.DB_HOST,
      port: 5432,
      database: 'hyperliquid_data',
      user: process.env.DB_USER,
      password: process.env.DB_PASSWORD,
      
      // Pool configuration
      max: 50,                    // Maximum connections
      idleTimeoutMillis: 30000,   // Close idle after 30s
      connectionTimeoutMillis: 5000, // Timeout after 5s
      
      // Retry configuration
      maxRetries: 3,
      retryDelay: 1000
    });

    // Handle pool errors
    this.pool.on('error', (err) => {
      console.error('❌ Unexpected pool error:', err.message);
      this.scheduleReconnect();
    });

    // Monitor pool health
    this.startHealthCheck();
  }

  async queryWithRetry(sql, params, retries = 3) {
    for (let i = 0; i < retries; i++) {
      try {
        const start = Date.now();
        const result = await this.pool.query(sql, params);
        const duration = Date.now() - start;
        
        if (duration > 1000) {
          console.warn(⚠️ Slow query (${duration}ms): ${sql.substring(0, 50)}...);
        }
        
        return result;
        
      } catch (error) {
        if (i === retries - 1) throw error;
        
        console.log(🔄 Query retry ${i + 1}/${retries}: ${error.message});
        await new Promise(r => setTimeout(r, 1000 * Math.pow(2, i)));
      }
    }
  }

  async bulkInsert(trades) {
    // Use UNNEST for efficient bulk insert
    const query = `
      INSERT INTO hyperliquid_trades (trade_id, pair, side, price, size, timestamp)
      SELECT * FROM UNNEST(
        $1::varchar[], $2::varchar[], $3::varchar[],
        $4::decimal[], $5::decimal[], $6::bigint[]
      )
      ON CONFLICT (trade_id) DO NOTHING
      RETURNING id
    `;

    const [
      ids, pairs, sides, 
      prices, sizes, timestamps
    ] = this.separateTradeArrays(trades);

    return this.queryWithRetry(query, [ids, pairs, sides, prices, sizes, timestamps]);
  }
}

3. Lỗi HolySheep AI API Timeout và Rate Limit

// ❌ VẤN ĐỀ: HolySheep API returns 429 hoặc timeout
// Nguyên nhân: Quá nhiều concurrent requests

// ✅ GIẢI PHÁP: Implement queue system với rate limiting

const Bottleneck = require('bottleneck');

// Configure rate limiter: 50 requests/minute = safe limit
const limiter = new Bottleneck({
  minTime: 1200,           // 50 req/min = 1200