#!/usr/bin/env python3 """ Test MEXC Futures Web Client This script demonstrates how to use the MEXC Futures Web Client for futures trading that isn't supported by their official API. IMPORTANT: This requires extracting cookies from your browser session. """ import logging import sys import os import time import json import uuid # Add the project root to path sys.path.append(os.path.dirname(os.path.abspath(__file__))) from core.mexc_webclient import MEXCFuturesWebClient from core.mexc_webclient.session_manager import MEXCSessionManager # Setup logging logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' ) logger = logging.getLogger(__name__) def test_basic_connection(): """Test basic connection and authentication""" logger.info("Testing MEXC Futures Web Client") # Initialize session manager session_manager = MEXCSessionManager() # Try to load saved session first cookies = session_manager.load_session() if not cookies: # Explicitly load the cookies from the file we have cookies_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'mexc_cookies_20250703_003625.json') if os.path.exists(cookies_file): try: with open(cookies_file, 'r') as f: cookies = json.load(f) logger.info(f"Loaded cookies from {cookies_file}") except Exception as e: logger.error(f"Failed to load cookies from {cookies_file}: {e}") cookies = None else: logger.error(f"Cookies file not found at {cookies_file}") cookies = None if not cookies: print("\nNo saved session found. You need to extract cookies from your browser.") session_manager.print_cookie_extraction_guide() print("\nPaste your cookie header or cURL command (or press Enter to exit):") user_input = input().strip() if not user_input: print("No input provided. Exiting.") return False # Extract cookies from user input if user_input.startswith('curl'): cookies = session_manager.extract_from_curl_command(user_input) else: cookies = session_manager.extract_cookies_from_network_tab(user_input) if not cookies: logger.error("Failed to extract cookies from input") return False # Validate and save session if session_manager.validate_session_cookies(cookies): session_manager.save_session(cookies) logger.info("Session saved for future use") else: logger.warning("Extracted cookies may be incomplete") # Initialize the web client client = MEXCFuturesWebClient(api_key='', api_secret='', user_id='', base_url='https://www.mexc.com', headless=True) # Load cookies into the client's session for name, value in cookies.items(): client.session.cookies.set(name, value) # Update headers to include additional parameters from captured requests client.session.headers.update({ 'trochilus-trace-id': f"{uuid.uuid4()}-{int(time.time() * 1000) % 10000:04d}", 'trochilus-uid': cookies.get('u_id', ''), 'Referer': 'https://www.mexc.com/en-GB/futures/ETH_USDT?type=linear_swap', 'Language': 'English', 'X-Language': 'en-GB' }) if not client.is_authenticated: logger.error("Failed to authenticate with extracted cookies") return False logger.info("Successfully authenticated with MEXC") logger.info(f"User ID: {client.user_id}") logger.info(f"Auth Token: {client.auth_token[:20]}..." if client.auth_token else "No auth token") return True def test_captcha_verification(client: MEXCFuturesWebClient): """Test captcha verification system""" logger.info("Testing captcha verification...") # Test captcha for ETH_USDT long position with 200x leverage success = client.verify_captcha('ETH_USDT', 'openlong', '200X') if success: logger.info("Captcha verification successful") else: logger.warning("Captcha verification failed - this may be normal if no position is being opened") return success def test_position_opening(client: MEXCFuturesWebClient, dry_run: bool = True): """Test opening a position (dry run by default)""" if dry_run: logger.info("DRY RUN: Testing position opening (no actual trade)") else: logger.warning("LIVE TRADING: Opening actual position!") symbol = 'ETH_USDT' volume = 1 # Small test position leverage = 200 logger.info(f"Attempting to open long position: {symbol}, Volume: {volume}, Leverage: {leverage}x") if not dry_run: result = client.open_long_position(symbol, volume, leverage) if result['success']: logger.info(f"Position opened successfully!") logger.info(f"Order ID: {result['order_id']}") logger.info(f"Timestamp: {result['timestamp']}") return True else: logger.error(f"Failed to open position: {result['error']}") return False else: logger.info("DRY RUN: Would attempt to open position here") # Test just the captcha verification part return client.verify_captcha(symbol, 'openlong', f'{leverage}X') def test_position_opening_live(client): symbol = "ETH_USDT" volume = 1 # Small volume for testing leverage = 200 logger.info(f"LIVE TRADING: Opening actual position!") logger.info(f"Attempting to open long position: {symbol}, Volume: {volume}, Leverage: {leverage}x") result = client.open_long_position(symbol, volume, leverage) if result.get('success'): logger.info(f"Successfully opened position: {result}") else: logger.error(f"Failed to open position: {result.get('error', 'Unknown error')}") def interactive_menu(client: MEXCFuturesWebClient): """Interactive menu for testing different functions""" while True: print("\n" + "="*50) print("MEXC Futures Web Client Test Menu") print("="*50) print("1. Test captcha verification") print("2. Test position opening (DRY RUN)") print("3. Test position opening (LIVE - BE CAREFUL!)") print("4. Test position closing (DRY RUN)") print("5. Show session info") print("6. Refresh session") print("0. Exit") choice = input("\nEnter choice (0-6): ").strip() if choice == "1": test_captcha_verification(client) elif choice == "2": test_position_opening(client, dry_run=True) elif choice == "3": test_position_opening_live(client) elif choice == "4": logger.info("DRY RUN: Position closing test") success = client.verify_captcha('ETH_USDT', 'closelong', '200X') if success: logger.info("DRY RUN: Would close position here") else: logger.warning("Captcha verification failed for position closing") elif choice == "5": print(f"\nSession Information:") print(f"Authenticated: {client.is_authenticated}") print(f"User ID: {client.user_id}") print(f"Auth Token: {client.auth_token[:20]}..." if client.auth_token else "None") print(f"Fingerprint: {client.fingerprint}") print(f"Visitor ID: {client.visitor_id}") elif choice == "6": session_manager = MEXCSessionManager() session_manager.print_cookie_extraction_guide() elif choice == "0": print("Goodbye!") break else: print("Invalid choice. Please try again.") def main(): """Main test function""" print("MEXC Futures Web Client Test") print("WARNING: This is experimental software for futures trading") print("Use at your own risk and test with small amounts first!") # Test basic connection if not test_basic_connection(): logger.error("Failed to establish connection. Exiting.") return # Create client with loaded session session_manager = MEXCSessionManager() cookies = session_manager.load_session() # If no session from manager, load explicitly if not cookies: cookies_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'mexc_cookies_20250703_003625.json') if os.path.exists(cookies_file): try: with open(cookies_file, 'r') as f: cookies = json.load(f) logger.info(f"Loaded cookies from {cookies_file}") except Exception as e: logger.error(f"Failed to load cookies from {cookies_file}: {e}") cookies = None else: logger.error(f"Cookies file not found at {cookies_file}") cookies = None if not cookies: logger.error("No valid session available") return client = MEXCFuturesWebClient(api_key='', api_secret='', user_id='', base_url='https://www.mexc.com', headless=True) # Load cookies into the client's session for name, value in cookies.items(): client.session.cookies.set(name, value) if not client.is_authenticated: logger.error("Failed to authenticate with MEXC") return # Enter interactive menu for further testing interactive_menu(client) if __name__ == "__main__": main()