208 lines
7.1 KiB
Python
208 lines
7.1 KiB
Python
#!/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
|
|
|
|
# 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:
|
|
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(cookies)
|
|
|
|
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 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":
|
|
confirm = input("Are you sure you want to open a LIVE position? (type 'YES' to confirm): ")
|
|
if confirm == "YES":
|
|
test_position_opening(client, dry_run=False)
|
|
else:
|
|
print("Cancelled live trading")
|
|
|
|
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 not cookies:
|
|
logger.error("No valid session available")
|
|
return
|
|
|
|
client = MEXCFuturesWebClient(cookies)
|
|
|
|
if not client.is_authenticated:
|
|
logger.error("Authentication failed")
|
|
return
|
|
|
|
# Show interactive menu
|
|
interactive_menu(client)
|
|
|
|
if __name__ == "__main__":
|
|
main() |