fairdesk / fairdesk-api-docs

API docs for fairdesk exchange
5 stars 8 forks source link

Unable to execute market order #7

Open bachebo opened 1 year ago

bachebo commented 1 year ago

Hey all!

I want to execute a simple market order, and I keep getting error 401 Unauthorised. Below is the script:

from Configurations.Api_keys import (FDESK_API_KEY, FDESK_SECRET) import requests import hmac import hashlib import time import base64

API_URL = 'https://api.fairdesk.com/api/v1/private/trade/place-order' API_KEY = FDESK_API_KEY API_SECRET = FDESK_SECRET

def generate_signature(endpoint, body, api_secret): expiry = str(int(time.time() * 1000) + 60000)
signature_payload = endpoint + expiry + body api_secret_decoded = base64.urlsafe_b64decode(api_secret) signature = hmac.new(api_secret_decoded, signature_payload.encode('utf-8'), hashlib.sha256).hexdigest() return signature, expiry

def place_market_order():

order_data = {
    "quantity": "0.01",
    "side": "BUY",
    "positionSide": "LONG",
    "copyTradeFlag": "false",
    "isolated": True,
    "type": "MARKET",
    "timeInForce": "GTC",
    "symbol": "btcusdt",
    "clientOrderId": "WEB9C6IB7h8absN",
    "orderRespType": "ACK"
}

body = str(order_data).replace(" ", "")
signature, expiry = generate_signature('/api/v1/private/trade/place-order', body, API_SECRET)

headers = {
    'x-fairdesk-access-key': API_KEY,
    'x-fairdesk-request-expiry': expiry,
    'x-fairdesk-request-signature': signature
}

response = requests.post(API_URL, json=order_data, headers=headers)
print("Response Text:", response.text)
try:
    return response.json()
except requests.exceptions.JSONDecodeError:
    return None

response = place_market_order()

if response is not None: print(response)

I am following all the requirements for the signature but it still doesn't work. Can someone advise on how we can fix this or an alternative way to execute the orders (through the API or other library is also fine)? Thanks in advance!