|
@@ -11,32 +11,73 @@ logger = logging.getLogger(__name__)
|
|
|
class HyperliquidClient:
|
|
|
"""Wrapper class for Hyperliquid API client with enhanced functionality."""
|
|
|
|
|
|
- def __init__(self, use_testnet: bool = True):
|
|
|
+ def __init__(self, use_testnet: bool = None):
|
|
|
"""
|
|
|
- Initialize the Hyperliquid client.
|
|
|
+ Initialize the Hyperliquid client with CCXT-style configuration.
|
|
|
|
|
|
Args:
|
|
|
- use_testnet: Whether to use testnet (default: True for safety)
|
|
|
+ use_testnet: Whether to use testnet (default: from Config.HYPERLIQUID_TESTNET)
|
|
|
"""
|
|
|
+ # Use config value if not explicitly provided
|
|
|
+ if use_testnet is None:
|
|
|
+ use_testnet = Config.HYPERLIQUID_TESTNET
|
|
|
+
|
|
|
self.use_testnet = use_testnet
|
|
|
- self.config = {
|
|
|
- 'private_key': Config.HYPERLIQUID_PRIVATE_KEY,
|
|
|
- 'testnet': use_testnet
|
|
|
- }
|
|
|
|
|
|
- # Initialize sync client
|
|
|
+ # Get CCXT-style configuration
|
|
|
+ self.config = Config.get_hyperliquid_config()
|
|
|
+
|
|
|
+ # Override testnet setting if provided
|
|
|
+ if use_testnet is not None:
|
|
|
+ self.config['testnet'] = use_testnet
|
|
|
+ self.config['sandbox'] = use_testnet
|
|
|
+
|
|
|
+ # Initialize clients
|
|
|
self.sync_client = None
|
|
|
self.async_client = None
|
|
|
|
|
|
if Config.HYPERLIQUID_PRIVATE_KEY:
|
|
|
try:
|
|
|
+ # Initialize with CCXT-style parameters
|
|
|
+ logger.info(f"🔧 Initializing Hyperliquid client with config: {self._safe_config_log()}")
|
|
|
+
|
|
|
+ # Try different initialization patterns for CCXT compatibility
|
|
|
self.sync_client = HyperliquidSync(self.config)
|
|
|
- logger.info(f"✅ Hyperliquid client initialized (testnet: {use_testnet})")
|
|
|
+
|
|
|
+ logger.info(f"✅ Hyperliquid client initialized successfully")
|
|
|
+ logger.info(f"🌐 Network: {'Testnet' if use_testnet else '🚨 MAINNET 🚨'}")
|
|
|
+
|
|
|
+ # Test the connection
|
|
|
+ self._test_connection()
|
|
|
+
|
|
|
except Exception as e:
|
|
|
logger.error(f"❌ Failed to initialize Hyperliquid client: {e}")
|
|
|
+ logger.error(f"💡 Config used: {self._safe_config_log()}")
|
|
|
+ raise
|
|
|
else:
|
|
|
logger.warning("⚠️ No private key provided - client will have limited functionality")
|
|
|
|
|
|
+ def _safe_config_log(self) -> Dict[str, Any]:
|
|
|
+ """Return config with sensitive data masked for logging."""
|
|
|
+ safe_config = self.config.copy()
|
|
|
+ if 'apiKey' in safe_config and safe_config['apiKey']:
|
|
|
+ safe_config['apiKey'] = f"{safe_config['apiKey'][:8]}..."
|
|
|
+ if 'secret' in safe_config and safe_config['secret']:
|
|
|
+ safe_config['secret'] = f"{safe_config['secret'][:8]}..."
|
|
|
+ if 'private_key' in safe_config and safe_config['private_key']:
|
|
|
+ safe_config['private_key'] = f"{safe_config['private_key'][:8]}..."
|
|
|
+ return safe_config
|
|
|
+
|
|
|
+ def _test_connection(self):
|
|
|
+ """Test the connection to verify credentials."""
|
|
|
+ try:
|
|
|
+ # Try to fetch balance to test authentication
|
|
|
+ balance = self.sync_client.fetch_balance()
|
|
|
+ logger.info(f"🔗 Connection test successful")
|
|
|
+ except Exception as e:
|
|
|
+ logger.warning(f"⚠️ Connection test failed: {e}")
|
|
|
+ logger.warning("💡 This might be normal if you have no positions/balance")
|
|
|
+
|
|
|
def get_balance(self) -> Optional[Dict[str, Any]]:
|
|
|
"""Get account balance."""
|
|
|
try:
|
|
@@ -87,44 +128,56 @@ class HyperliquidClient:
|
|
|
logger.error(f"❌ Error fetching market data for {symbol}: {e}")
|
|
|
return None
|
|
|
|
|
|
- def place_limit_order(self, symbol: str, side: str, amount: float, price: float) -> Optional[Dict[str, Any]]:
|
|
|
+ def place_limit_order(self, symbol: str, side: str, amount: float, price: float, params: Optional[Dict] = None) -> Optional[Dict[str, Any]]:
|
|
|
"""
|
|
|
- Place a limit order.
|
|
|
+ Place a limit order with CCXT-style parameters.
|
|
|
|
|
|
Args:
|
|
|
symbol: Trading symbol (e.g., 'BTC/USDC:USDC')
|
|
|
side: 'buy' or 'sell'
|
|
|
amount: Order amount
|
|
|
price: Order price
|
|
|
+ params: Additional parameters for CCXT compatibility
|
|
|
"""
|
|
|
try:
|
|
|
if not self.sync_client:
|
|
|
logger.error("❌ Client not initialized")
|
|
|
return None
|
|
|
|
|
|
- order = self.sync_client.create_limit_order(symbol, side, amount, price)
|
|
|
+ # CCXT-style order creation
|
|
|
+ order_params = params or {}
|
|
|
+ order = self.sync_client.create_limit_order(symbol, side, amount, price, params=order_params)
|
|
|
+
|
|
|
logger.info(f"✅ Successfully placed {side} limit order for {amount} {symbol} at ${price}")
|
|
|
+ logger.debug(f"📄 Order details: {order}")
|
|
|
+
|
|
|
return order
|
|
|
except Exception as e:
|
|
|
logger.error(f"❌ Error placing limit order: {e}")
|
|
|
return None
|
|
|
|
|
|
- def place_market_order(self, symbol: str, side: str, amount: float) -> Optional[Dict[str, Any]]:
|
|
|
+ def place_market_order(self, symbol: str, side: str, amount: float, params: Optional[Dict] = None) -> Optional[Dict[str, Any]]:
|
|
|
"""
|
|
|
- Place a market order.
|
|
|
+ Place a market order with CCXT-style parameters.
|
|
|
|
|
|
Args:
|
|
|
symbol: Trading symbol (e.g., 'BTC/USDC:USDC')
|
|
|
side: 'buy' or 'sell'
|
|
|
amount: Order amount
|
|
|
+ params: Additional parameters for CCXT compatibility
|
|
|
"""
|
|
|
try:
|
|
|
if not self.sync_client:
|
|
|
logger.error("❌ Client not initialized")
|
|
|
return None
|
|
|
|
|
|
- order = self.sync_client.create_market_order(symbol, side, amount)
|
|
|
+ # CCXT-style order creation
|
|
|
+ order_params = params or {}
|
|
|
+ order = self.sync_client.create_market_order(symbol, side, amount, params=order_params)
|
|
|
+
|
|
|
logger.info(f"✅ Successfully placed {side} market order for {amount} {symbol}")
|
|
|
+ logger.debug(f"📄 Order details: {order}")
|
|
|
+
|
|
|
return order
|
|
|
except Exception as e:
|
|
|
logger.error(f"❌ Error placing market order: {e}")
|
|
@@ -144,14 +197,16 @@ class HyperliquidClient:
|
|
|
logger.error(f"❌ Error fetching open orders: {e}")
|
|
|
return None
|
|
|
|
|
|
- def cancel_order(self, order_id: str, symbol: str) -> bool:
|
|
|
- """Cancel an order."""
|
|
|
+ def cancel_order(self, order_id: str, symbol: str, params: Optional[Dict] = None) -> bool:
|
|
|
+ """Cancel an order with CCXT-style parameters."""
|
|
|
try:
|
|
|
if not self.sync_client:
|
|
|
logger.error("❌ Client not initialized")
|
|
|
return False
|
|
|
|
|
|
- result = self.sync_client.cancel_order(order_id, symbol)
|
|
|
+ cancel_params = params or {}
|
|
|
+ result = self.sync_client.cancel_order(order_id, symbol, params=cancel_params)
|
|
|
+
|
|
|
logger.info(f"✅ Successfully cancelled order {order_id}")
|
|
|
return True
|
|
|
except Exception as e:
|
|
@@ -184,4 +239,18 @@ class HyperliquidClient:
|
|
|
return fee
|
|
|
except Exception as e:
|
|
|
logger.error(f"❌ Error fetching trading fee for {symbol}: {e}")
|
|
|
+ return None
|
|
|
+
|
|
|
+ def get_markets(self) -> Optional[Dict[str, Any]]:
|
|
|
+ """Get available markets/symbols."""
|
|
|
+ try:
|
|
|
+ if not self.sync_client:
|
|
|
+ logger.error("❌ Client not initialized")
|
|
|
+ return None
|
|
|
+
|
|
|
+ markets = self.sync_client.load_markets()
|
|
|
+ logger.info(f"✅ Successfully loaded {len(markets)} markets")
|
|
|
+ return markets
|
|
|
+ except Exception as e:
|
|
|
+ logger.error(f"❌ Error loading markets: {e}")
|
|
|
return None
|