#!/usr/bin/env python3 """ Final MEXC Order Test - Exact match to working examples """ import os import sys import time import hmac import hashlib import requests import json from urllib.parse import urlencode from pathlib import Path # Add project root to path project_root = Path(__file__).parent sys.path.insert(0, str(project_root)) def test_final_mexc_order(): """Test MEXC order with the working method""" print("Final MEXC Order Test - Working Method") print("=" * 50) # Get API credentials api_key = os.getenv('MEXC_API_KEY', '') api_secret = os.getenv('MEXC_SECRET_KEY', '') if not api_key or not api_secret: print("❌ No MEXC API credentials found") return # Parameters timestamp = str(int(time.time() * 1000)) # Create the exact parameter string like the working example params = f"symbol=ETHUSDC&side=BUY&type=LIMIT&quantity=0.003&price=2900&recvWindow=5000×tamp={timestamp}" print(f"Parameter string: {params}") # Create signature exactly like the working example signature = hmac.new( api_secret.encode('utf-8'), params.encode('utf-8'), hashlib.sha256 ).hexdigest() print(f"Signature: {signature}") # Make the request exactly like the curl example url = f"https://api.mexc.com/api/v3/order" headers = { 'X-MEXC-APIKEY': api_key, 'Content-Type': 'application/x-www-form-urlencoded' } data = f"{params}&signature={signature}" try: print(f"\nPOST to: {url}") print(f"Headers: {headers}") print(f"Data: {data}") response = requests.post(url, headers=headers, data=data) print(f"\nStatus: {response.status_code}") print(f"Response: {response.text}") if response.status_code == 200: print("✅ SUCCESS!") else: print("❌ FAILED") # Try alternative method - sending as query params print("\n--- Trying alternative method ---") test_alternative_method(api_key, api_secret) except Exception as e: print(f"Error: {e}") def test_alternative_method(api_key: str, api_secret: str): """Try sending as query parameters instead""" timestamp = str(int(time.time() * 1000)) params = { 'symbol': 'ETHUSDC', 'side': 'BUY', 'type': 'LIMIT', 'quantity': '0.003', 'price': '2900', 'timestamp': timestamp, 'recvWindow': '5000' } # Create query string query_string = '&'.join([f"{k}={v}" for k, v in sorted(params.items())]) # Create signature signature = hmac.new( api_secret.encode('utf-8'), query_string.encode('utf-8'), hashlib.sha256 ).hexdigest() # Add signature to params params['signature'] = signature headers = { 'X-MEXC-APIKEY': api_key } print(f"Alternative query params: {params}") response = requests.post('https://api.mexc.com/api/v3/order', params=params, headers=headers) print(f"Alternative response: {response.status_code} - {response.text}") if __name__ == "__main__": test_final_mexc_order()