Advertisement
den4ik2003

Untitled

Nov 1st, 2024
7
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. import requests
  2. import json
  3. import copy
  4. import hashlib
  5. import hmac
  6.  
  7. api_key = '7bf3c1fd-f3a3-4412-b7fb-148f6bbcd0fd'
  8. secret_key = 'e11f47c3-3237-426c-9d9f-dbae226cae5d'
  9. base_url = 'https://api.biconomy.com'
  10.  
  11. # X-SITE-ID = 127 add
  12.  
  13. def get_best_book(symbol):
  14. response = requests.get(f'https://api.biconomy.com/api/v1/depth?symbol={symbol}')
  15. if response.status_code != 200:
  16. return (0, 0, returnresponse.status_code)
  17. else:
  18. content = json.loads(response.content)
  19. if len(content['asks']) > 0 and len(content['bids']) > 0:
  20. return (content['bids'][0][0], content['asks'][0][0], 200)
  21. elif len(content['asks']) > 0:
  22. return (0, content['asks'][0][0], 200)
  23. elif len(content['bids']) > 0:
  24. return (content['bids'][0][0], 0, 200)
  25. else:
  26. return (0, 0, 200)
  27.  
  28.  
  29. def get_info(symbol):
  30. base = symbol[0:symbol.find('_')]
  31. quote = symbol[0:symbol.find('_') + 1:]
  32. response = requests.get('https://api.biconomy.com/api/v1/exchangeInfo')
  33. if response.status_code == 200:
  34. for element in json.loads(response.content):
  35. if element['baseAsset'] == base and element['quoteAsset']:
  36. return element
  37.  
  38.  
  39. def form_signed_url(url, params):
  40. print(params)
  41. signature_dict = copy.deepcopy(params)
  42. print(signature_dict)
  43. signature_dict['api_key'] = api_key
  44. sorted_signature_dict = {key: signature_dict[key] for key in sorted(signature_dict.keys())}
  45. signature_str = ''
  46. for key, val in sorted_signature_dict.items():
  47. signature_str += (key + '=' + val + '&')
  48. signature_str += ('secret_key=' + secret_key)
  49.  
  50. print(signature_str)
  51.  
  52. signature_str = hmac.new(
  53. secret_key.encode('utf-8'),
  54. msg=signature_str[:-1].encode('utf-8'),
  55. digestmod=hashlib.sha256
  56. ).hexdigest().capitalize()
  57.  
  58.  
  59. body = copy.deepcopy(params)
  60. body['api_key'] = api_key
  61. sorted_body = {key: body[key] for key in sorted(body.keys())}
  62.  
  63. url += '?'
  64. for key, value in sorted_body.items():
  65. url += (key + '=' + value + '&')
  66. url += ('sign=' + signature_str)
  67.  
  68. print(url)
  69.  
  70. return url
  71.  
  72.  
  73. def get_balance(params, token):
  74. url = form_signed_url('https://api.biconomy.com/api/v2/private/user', params)
  75.  
  76. content = json.loads(requests.post(url).content)
  77. if content['code'] == 0:
  78. for coin in content['result'].keys():
  79. if coin == token:
  80. return content['result'][coin]
  81. else:
  82. print(content['message'])
  83.  
  84.  
  85. def new_limit(symbol, side, amount, price):
  86. if side == 'SELL':
  87. side = '1'
  88. elif side == 'BUY':
  89. side = '2'
  90.  
  91. params = {'market': symbol, 'side': side, 'amount': amount, 'price': price}
  92. url = form_signed_url(base_url + '/api/v2/private/trade/limit', params)
  93.  
  94. content = json.loads(requests.post(url).content)
  95. if content['code'] == 0:
  96. return content['result']
  97. else:
  98. print(content['message'])
  99.  
  100. def cancel(symbol, order_id):
  101. params = {'market': symbol, 'order_id': order_id}
  102. url = form_signed_url(base_url + '/api/v2/private/trade/cancel', params)
  103.  
  104. content = json.loads(requests.post(url).content)
  105. if content['code'] == 0:
  106. return content['result']
  107. else:
  108. print(content['message'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement