134 lines
4.0 KiB
Python
134 lines
4.0 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
MEXC Order Fix V3 - Based on exact curl examples from MEXC documentation
|
|
"""
|
|
|
|
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 create_mexc_signature_v3(query_string: str, secret_key: str) -> str:
|
|
"""Create MEXC signature exactly as shown in curl examples"""
|
|
|
|
print(f"Signing string: {query_string}")
|
|
|
|
# MEXC uses HMAC SHA256 on the query string
|
|
signature = hmac.new(
|
|
secret_key.encode('utf-8'),
|
|
query_string.encode('utf-8'),
|
|
hashlib.sha256
|
|
).hexdigest()
|
|
|
|
print(f"Generated signature: {signature}")
|
|
return signature
|
|
|
|
def test_mexc_order_v3():
|
|
"""Test MEXC order placement with V3 method matching curl examples"""
|
|
print("Testing MEXC Order V3 - Exact curl examples...")
|
|
print("=" * 60)
|
|
|
|
# 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
|
|
|
|
# Order parameters exactly like the examples
|
|
timestamp = str(int(time.time() * 1000))
|
|
|
|
# Build the query string in alphabetical order (like the examples)
|
|
params = {
|
|
'price': '2900.0',
|
|
'quantity': '0.003',
|
|
'recvWindow': '5000',
|
|
'side': 'BUY',
|
|
'symbol': 'ETHUSDC',
|
|
'timeInForce': 'GTC',
|
|
'timestamp': timestamp,
|
|
'type': 'LIMIT'
|
|
}
|
|
|
|
# Create query string in alphabetical order
|
|
query_string = urlencode(sorted(params.items()))
|
|
|
|
print(f"Parameters: {params}")
|
|
print(f"Query string: {query_string}")
|
|
|
|
# Generate signature
|
|
signature = create_mexc_signature_v3(query_string, api_secret)
|
|
|
|
# Build the final URL and data exactly like the curl examples
|
|
base_url = "https://api.mexc.com/api/v3/order"
|
|
final_data = f"{query_string}&signature={signature}"
|
|
|
|
# Headers exactly like the curl examples
|
|
headers = {
|
|
'X-MEXC-APIKEY': api_key,
|
|
'Content-Type': 'application/x-www-form-urlencoded'
|
|
}
|
|
|
|
try:
|
|
print(f"\nMaking POST request to: {base_url}")
|
|
print(f"Headers: {headers}")
|
|
print(f"Data: {final_data}")
|
|
|
|
# POST with data in body (like curl -d option)
|
|
response = requests.post(base_url, headers=headers, data=final_data, timeout=10)
|
|
|
|
print(f"\nResponse Status: {response.status_code}")
|
|
print(f"Response: {response.text}")
|
|
|
|
if response.status_code == 200:
|
|
result = response.json()
|
|
print("✅ Order placed successfully!")
|
|
print(f"Order result: {result}")
|
|
|
|
# Cancel immediately if successful
|
|
if 'orderId' in result:
|
|
print(f"\n🔄 Canceling order {result['orderId']}...")
|
|
cancel_order_v3(api_key, api_secret, 'ETHUSDC', result['orderId'])
|
|
|
|
else:
|
|
print("❌ Order placement failed")
|
|
|
|
except Exception as e:
|
|
print(f"❌ Request error: {e}")
|
|
|
|
def cancel_order_v3(api_key: str, secret_key: str, symbol: str, order_id: str):
|
|
"""Cancel a MEXC order using V3 method"""
|
|
timestamp = str(int(time.time() * 1000))
|
|
|
|
params = {
|
|
'orderId': order_id,
|
|
'recvWindow': '5000',
|
|
'symbol': symbol,
|
|
'timestamp': timestamp
|
|
}
|
|
|
|
query_string = urlencode(sorted(params.items()))
|
|
signature = create_mexc_signature_v3(query_string, secret_key)
|
|
|
|
url = f"https://api.mexc.com/api/v3/order"
|
|
data = f"{query_string}&signature={signature}"
|
|
headers = {
|
|
'X-MEXC-APIKEY': api_key,
|
|
'Content-Type': 'application/x-www-form-urlencoded'
|
|
}
|
|
|
|
response = requests.delete(url, headers=headers, data=data, timeout=10)
|
|
print(f"Cancel response: {response.status_code} - {response.text}")
|
|
|
|
if __name__ == "__main__":
|
|
test_mexc_order_v3() |