Эх сурвалжийг харах

Refactor Hyperliquid API configuration for wallet address and API key - Updated the configuration to correctly assign the wallet address to HYPERLIQUID_PRIVATE_KEY and the API generator key to HYPERLIQUID_SECRET_KEY. Adjusted documentation and logging to reflect these changes, ensuring clarity and compatibility with CCXT implementation.

Carles Sentis 5 өдөр өмнө
parent
commit
d5588ed5be
5 өөрчлөгдсөн 32 нэмэгдсэн , 66 устгасан
  1. 2 3
      README.md
  2. 5 10
      config/env.example
  3. 4 4
      docs/setup.md
  4. 15 22
      src/config.py
  5. 6 27
      src/hyperliquid_client.py

+ 2 - 3
README.md

@@ -179,9 +179,8 @@ Following [CCXT standards](https://github.com/ccxt/ccxt) for exchange compatibil
 
 ```env
 # Hyperliquid API (CCXT Style)
-HYPERLIQUID_PRIVATE_KEY=your_api_generator_key_here  # ← Use API Generator Key from app.hyperliquid.xyz/API
-HYPERLIQUID_WALLET_ADDRESS=your_wallet_address_here  # ← Your wallet address (0x...)
-HYPERLIQUID_SECRET_KEY=your_secret_key_here
+HYPERLIQUID_PRIVATE_KEY=your_wallet_address_here     # ← Your wallet address (0x...)
+HYPERLIQUID_SECRET_KEY=your_api_generator_key_here   # ← Use API Generator Key from app.hyperliquid.xyz/API
 HYPERLIQUID_TESTNET=true
 
 # Telegram Bot

+ 5 - 10
config/env.example

@@ -1,17 +1,12 @@
 # ========================================
 # Hyperliquid API Configuration (CCXT Style)
 # ========================================
-# Your Hyperliquid API Generator Key (SECURE - from https://app.hyperliquid.xyz/API)
-# NEVER use your wallet's private key directly!
-HYPERLIQUID_PRIVATE_KEY=your_api_generator_key_here
+# Your wallet address (required - this goes to CCXT as 'secret')
+HYPERLIQUID_PRIVATE_KEY=your_wallet_address_here
 
-# Your wallet address (required by CCXT Hyperliquid implementation)
-# Get this from your wallet interface (0x...)
-HYPERLIQUID_WALLET_ADDRESS=your_wallet_address_here
-
-# Your Hyperliquid secret key (may be optional depending on implementation)
-# Some CCXT exchanges require both apiKey and secret, others only one
-HYPERLIQUID_SECRET_KEY=your_secret_key_here
+# Your Hyperliquid API Generator Key (from https://app.hyperliquid.xyz/API)
+# This goes to CCXT as 'apiKey' - NEVER use your wallet's private key directly!
+HYPERLIQUID_SECRET_KEY=your_api_generator_key_here
 
 # Network selection: true for testnet (safe), false for mainnet (real money!)
 HYPERLIQUID_TESTNET=true

+ 4 - 4
docs/setup.md

@@ -44,9 +44,8 @@ nano .env  # Or use your preferred editor
 **Required settings:**
 ```env
 # Use API Generator Key from https://app.hyperliquid.xyz/API (NOT your wallet private key)
-HYPERLIQUID_PRIVATE_KEY=your_api_generator_key_here
-HYPERLIQUID_WALLET_ADDRESS=your_wallet_address_here
-HYPERLIQUID_SECRET_KEY=your_secret_key_here
+HYPERLIQUID_PRIVATE_KEY=your_wallet_address_here
+HYPERLIQUID_SECRET_KEY=your_api_generator_key_here
 HYPERLIQUID_TESTNET=true
 
 # From steps above
@@ -69,7 +68,8 @@ python trading_bot.py
 2. Generate a dedicated API key for trading
 3. **NEVER use your wallet's private key directly**
 4. Copy your wallet address (0x...) from your wallet interface
-5. Use both the generated API key and wallet address in your `.env` file
+5. Set HYPERLIQUID_PRIVATE_KEY to your wallet address
+6. Set HYPERLIQUID_SECRET_KEY to your API generator key
 
 ### Testnet (Recommended First)
 1. Go to [Hyperliquid Testnet](https://app.hyperliquid-testnet.xyz/)

+ 15 - 22
src/config.py

@@ -12,9 +12,8 @@ class Config:
     """Configuration class for the Hyperliquid trading bot."""
     
     # Hyperliquid API Configuration
-    HYPERLIQUID_PRIVATE_KEY: Optional[str] = os.getenv('HYPERLIQUID_PRIVATE_KEY')
-    HYPERLIQUID_SECRET_KEY: Optional[str] = os.getenv('HYPERLIQUID_SECRET_KEY')
-    HYPERLIQUID_WALLET_ADDRESS: Optional[str] = os.getenv('HYPERLIQUID_WALLET_ADDRESS')
+    HYPERLIQUID_PRIVATE_KEY: Optional[str] = os.getenv('HYPERLIQUID_PRIVATE_KEY')  # Wallet address
+    HYPERLIQUID_SECRET_KEY: Optional[str] = os.getenv('HYPERLIQUID_SECRET_KEY')    # API generator key
     HYPERLIQUID_TESTNET: bool = os.getenv('HYPERLIQUID_TESTNET', 'true').lower() == 'true'
     
     # Trading Bot Configuration
@@ -48,11 +47,15 @@ class Config:
         
         # Validate Hyperliquid settings
         if not cls.HYPERLIQUID_PRIVATE_KEY:
-            logger.error("❌ HYPERLIQUID_PRIVATE_KEY is required")
+            logger.error("❌ HYPERLIQUID_PRIVATE_KEY (wallet address) is required")
+            is_valid = False
+        
+        if not cls.HYPERLIQUID_SECRET_KEY:
+            logger.error("❌ HYPERLIQUID_SECRET_KEY (API generator key) is required")
             is_valid = False
         
         # Validate wallet address or private key format
-        if not cls.HYPERLIQUID_WALLET_ADDRESS and cls.HYPERLIQUID_PRIVATE_KEY:
+        if not cls.HYPERLIQUID_PRIVATE_KEY and cls.HYPERLIQUID_SECRET_KEY:
             # Check if private key looks like an address
             if not cls.HYPERLIQUID_PRIVATE_KEY.startswith('0x') and len(cls.HYPERLIQUID_PRIVATE_KEY) < 40:
                 logger.warning("⚠️ HYPERLIQUID_WALLET_ADDRESS not set - will attempt to use private key as address")
@@ -105,23 +108,14 @@ class Config:
         }
         
         # Add authentication if available
-        # Hyperliquid CCXT implementation expects 'privateKey' and 'walletAddress' credentials
-        if cls.HYPERLIQUID_PRIVATE_KEY:
-            config['privateKey'] = cls.HYPERLIQUID_PRIVATE_KEY
-            config['apiKey'] = cls.HYPERLIQUID_PRIVATE_KEY  # Keep for backward compatibility
-            
-        if cls.HYPERLIQUID_WALLET_ADDRESS:
-            config['walletAddress'] = cls.HYPERLIQUID_WALLET_ADDRESS
-        elif cls.HYPERLIQUID_PRIVATE_KEY:
-            # If no explicit wallet address, try to use the private key as address
-            # (This might work if the private key is actually the wallet address)
-            wallet_addr = cls.HYPERLIQUID_PRIVATE_KEY
-            if not wallet_addr.startswith('0x'):
-                wallet_addr = f"0x{wallet_addr}"
-            config['walletAddress'] = wallet_addr
-            
+        # Hyperliquid CCXT implementation expects:
+        # - apiKey: API generator key (SECRET_KEY)  
+        # - secret: wallet address (PRIVATE_KEY)
         if cls.HYPERLIQUID_SECRET_KEY:
-            config['secret'] = cls.HYPERLIQUID_SECRET_KEY
+            config['apiKey'] = cls.HYPERLIQUID_SECRET_KEY  # API generator key
+            
+        if cls.HYPERLIQUID_PRIVATE_KEY:
+            config['secret'] = cls.HYPERLIQUID_PRIVATE_KEY  # Wallet address
             
         return config
     
@@ -131,7 +125,6 @@ class Config:
         logger.info("🔧 Configuration:")
         logger.info(f"  📡 Hyperliquid Testnet: {cls.HYPERLIQUID_TESTNET}")
         logger.info(f"  🔑 Private Key: {'✅ Set' if cls.HYPERLIQUID_PRIVATE_KEY else '❌ Missing'}")
-        logger.info(f"  🏠 Wallet Address: {'✅ Set' if cls.HYPERLIQUID_WALLET_ADDRESS else '⚠️ Will derive from private key'}")
         logger.info(f"  🔐 Secret Key: {'✅ Set' if cls.HYPERLIQUID_SECRET_KEY else '⚠️ Missing (may be optional)'}")
         logger.info(f"  📱 Telegram: {'✅ Enabled' if cls.TELEGRAM_ENABLED else '❌ Disabled'}")
         logger.info(f"  🔍 Log Level: {cls.LOG_LEVEL}")

+ 6 - 27
src/hyperliquid_client.py

@@ -32,25 +32,12 @@ class HyperliquidClient:
             self.config['sandbox'] = use_testnet
         
         # Ensure proper CCXT format
-        if not self.config.get('privateKey') and Config.HYPERLIQUID_PRIVATE_KEY:
-            self.config['privateKey'] = Config.HYPERLIQUID_PRIVATE_KEY
-            
-        if not self.config.get('apiKey') and Config.HYPERLIQUID_PRIVATE_KEY:
-            self.config['apiKey'] = Config.HYPERLIQUID_PRIVATE_KEY
-            
-        if not self.config.get('walletAddress'):
-            if Config.HYPERLIQUID_WALLET_ADDRESS:
-                self.config['walletAddress'] = Config.HYPERLIQUID_WALLET_ADDRESS
-            elif Config.HYPERLIQUID_PRIVATE_KEY:
-                # Fallback: try to use private key as wallet address
-                wallet_addr = Config.HYPERLIQUID_PRIVATE_KEY
-                if not wallet_addr.startswith('0x'):
-                    wallet_addr = f"0x{wallet_addr}"
-                self.config['walletAddress'] = wallet_addr
-                logger.warning(f"⚠️ Using private key as wallet address: {wallet_addr[:8]}...")
-            
-        if not self.config.get('secret') and Config.HYPERLIQUID_SECRET_KEY:
-            self.config['secret'] = Config.HYPERLIQUID_SECRET_KEY
+        # Hyperliquid CCXT expects: apiKey=API_generator_key, secret=wallet_address
+        if not self.config.get('apiKey') and Config.HYPERLIQUID_SECRET_KEY:
+            self.config['apiKey'] = Config.HYPERLIQUID_SECRET_KEY  # API generator key
+            
+        if not self.config.get('secret') and Config.HYPERLIQUID_PRIVATE_KEY:
+            self.config['secret'] = Config.HYPERLIQUID_PRIVATE_KEY  # Wallet address
         
         # Initialize clients
         self.sync_client = None
@@ -95,10 +82,6 @@ class HyperliquidClient:
         safe_config = self.config.copy()
         if 'apiKey' in safe_config and safe_config['apiKey']:
             safe_config['apiKey'] = f"{safe_config['apiKey'][:8]}..."
-        if 'privateKey' in safe_config and safe_config['privateKey']:
-            safe_config['privateKey'] = f"{safe_config['privateKey'][:8]}..."
-        if 'walletAddress' in safe_config and safe_config['walletAddress']:
-            safe_config['walletAddress'] = f"{safe_config['walletAddress'][:8]}..."
         if 'secret' in safe_config and safe_config['secret']:
             safe_config['secret'] = f"{safe_config['secret'][:8]}..."
         return safe_config
@@ -108,10 +91,6 @@ class HyperliquidClient:
         safe_config = config.copy()
         if 'apiKey' in safe_config and safe_config['apiKey']:
             safe_config['apiKey'] = f"{safe_config['apiKey'][:8]}..."
-        if 'privateKey' in safe_config and safe_config['privateKey']:
-            safe_config['privateKey'] = f"{safe_config['privateKey'][:8]}..."
-        if 'walletAddress' in safe_config and safe_config['walletAddress']:
-            safe_config['walletAddress'] = f"{safe_config['walletAddress'][:8]}..."
         if 'secret' in safe_config and safe_config['secret']:
             safe_config['secret'] = f"{safe_config['secret'][:8]}..."
         return safe_config