#!/usr/bin/env python3 """ Test script for MEXC API with new credentials """ import os import sys from dotenv import load_dotenv # Load environment variables load_dotenv() def test_api_credentials(): """Test MEXC API credentials step by step""" print("="*60) print("MEXC API CREDENTIALS TEST") print("="*60) # Check environment variables api_key = os.getenv('MEXC_API_KEY') api_secret = os.getenv('MEXC_SECRET_KEY') print(f"1. Environment Variables:") print(f" API Key: {api_key[:5]}...{api_key[-5:] if api_key else 'None'}") print(f" API Secret: {api_secret[:5]}...{api_secret[-5:] if api_secret else 'None'}") print(f" API Key Length: {len(api_key) if api_key else 0}") print(f" API Secret Length: {len(api_secret) if api_secret else 0}") if not api_key or not api_secret: print("❌ API credentials not found in environment") return False # Test public API first print(f"\n2. Testing Public API (no authentication):") try: from NN.exchanges.mexc_interface import MEXCInterface api = MEXCInterface('dummy', 'dummy', test_mode=False) ticker = api.get_ticker('ETHUSDT') if ticker: print(f" ✅ Public API works: ETH/USDT = ${ticker.get('last', 'N/A')}") else: print(f" ❌ Public API failed") return False except Exception as e: print(f" ❌ Public API error: {e}") return False # Test private API with actual credentials print(f"\n3. Testing Private API (with authentication):") try: api_auth = MEXCInterface(api_key, api_secret, test_mode=False) # Try to get account info account_info = api_auth.get_account_info() if account_info: print(f" ✅ Private API works: Account info retrieved") print(f" 📊 Account Type: {account_info.get('accountType', 'N/A')}") # Try to get USDT balance usdt_balance = api_auth.get_balance('USDT') print(f" 💰 USDT Balance: {usdt_balance}") return True else: print(f" ❌ Private API failed: Could not get account info") return False except Exception as e: print(f" ❌ Private API error: {e}") return False def test_api_permissions(): """Test specific API permissions""" print(f"\n4. Testing API Permissions:") api_key = os.getenv('MEXC_API_KEY') api_secret = os.getenv('MEXC_SECRET_KEY') try: from NN.exchanges.mexc_interface import MEXCInterface api = MEXCInterface(api_key, api_secret, test_mode=False) # Test spot trading permissions print(" Testing spot trading permissions...") # Try to get open orders (requires spot trading permission) try: orders = api.get_open_orders('ETHUSDT') print(" ✅ Spot trading permission: OK") except Exception as e: print(f" ❌ Spot trading permission: {e}") return False return True except Exception as e: print(f" ❌ Permission test error: {e}") return False def main(): """Main test function""" success = test_api_credentials() if success: test_api_permissions() print(f"\n✅ MEXC API SETUP COMPLETE") print("The trading system should now work with live MEXC spot trading") else: print(f"\n❌ MEXC API SETUP FAILED") print("Possible issues:") print("1. API key or secret incorrect") print("2. API key not activated yet") print("3. Insufficient permissions (need spot trading)") print("4. IP address not whitelisted") print("5. Account verification incomplete") if __name__ == "__main__": main()