mexcdevelop / mexc-api-sdk

MIT License
173 stars 70 forks source link

Pls help me correct code to read balance #50

Open Dotijo opened 11 months ago

Dotijo commented 11 months ago

import time import requests import hashlib import hmac

def create_signed_request(api_key, secret_key, method, endpoint, params={}): base_url = 'https://www.mexc.com' request_url = f"{base_url}{endpoint}" timestamp = int(time.time() * 1000) params['api_key'] = api_key params['req_time'] = timestamp sorted_params = sorted(params.items()) query_string = "&".join([f"{k}={v}" for k, v in sorted_params])

to_sign = f"{method}{request_url}?{query_string}"

if method == 'GET' else f"{method}{request_url}" signature = hmac.new(secret_key.encode(), to_sign.encode(), hashlib.sha256).hexdigest() headers = { 'Content-Type': 'application/x-www-form-urlencoded', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3', 'Cookie': 'locale=en_US', 'Authorization': f"{signature}", }

if method == 'GET':
    response = requests.get(request_url, headers=headers)
else:
    response = requests.post(request_url, headers=headers, data=query_string)
    return response.json()

def main():

Read API key and secret key from the user

API_KEY = 'my api key here'
SECRET_KEY = 'my secret key here'

# Get account balance
account_balance = create_signed_request(API_KEY, SECRET_KEY, 'GET', '/account/v1/balance')
if account_balance:
    print("Account Balance:")
    for balance in account_balance:
        asset = balance['currency']
        free_balance = balance['available']
        locked_balance = balance['frozen']
        print(f"{asset}: Free={free_balance}, Locked={locked_balance}")
else:
    print("Failed to retrieve account balance.")

if name == "main": main()