Facebook
From not sure, 2 Months ago, written in Python.
Embed
Download Paste or View Raw
Hits: 262
  1. import requests  # pip install requests
  2. import json
  3. import base64
  4. import hashlib
  5. import hmac
  6. import os
  7. import time #for nonce
  8.  
  9. class BitfinexClient(object):
  10.     BASE_URL = "https://api.bitfinex.com/"
  11.     KEY = "6ccbcfc02d524c4d18183c89b6ee03dd92a7d120940"
  12.     SECRET = "3f18b1113f4f06494764cae7167b467c456ad65e686"
  13.  
  14.     def _nonce(self):
  15.         # Returns a nonce
  16.         # Used in authentication
  17.         return str(int(round(time.time() * 10000)))
  18.  
  19.     def _headers(self, path, nonce, body):
  20.         secbytes = self.SECRET.encode(encoding='UTF-8')
  21.         signature = "/api/" + path + nonce + body
  22.         sigbytes = signature.encode(encoding='UTF-8')
  23.         h = hmac.new(secbytes, sigbytes, hashlib.sha384)
  24.         hexstring = h.hexdigest()
  25.         return {
  26.             "bfx-nonce": nonce,
  27.             "bfx-apikey": self.KEY,
  28.             "bfx-signature": hexstring,
  29.             "content-type": "application/json"
  30.         }
  31.  
  32.     def req(self, path, params = {}):
  33.         nonce = self._nonce()
  34.         body = params
  35.         rawBody = json.dumps(body)
  36.         headers = self._headers(path, nonce, rawBody)
  37.         url = self.BASE_URL + path
  38.         print(rawBody)
  39.         resp = requests.post(url, headers=headers, data=rawBody, verify=True)
  40.         return resp
  41.  
  42.     def active_orders(self):
  43.         # Fetch active orders
  44.         response = self.req("v2/auth/r/orders")
  45.         if response.status_code == 200:
  46.           return response.json()
  47.         else:
  48.           print('error, status_code = ', response.status_code)
  49.           return ''
  50.  
  51.     def wallets(self):
  52.         response = self.req("v2/auth/r/wallets")
  53.         if response.status_code == 200:
  54.           return response.json()
  55.         else:
  56.           print('error, status_code = ', response.status_code)
  57.           return ''
  58.  
  59.     def get_btc_amount(self):
  60.  
  61.         wallets = self.wallets()
  62.  
  63.         btc_wallet = None
  64.         for wallet in wallets:
  65.             if wallet[1] == "TESTBTC":
  66.                 btc_wallet = wallet
  67.                 break
  68.         else:
  69.             print("Error, no BTC wallet found")
  70.             return -1, -1
  71.  
  72.  
  73.         btc_amount = btc_wallet[4]
  74.  
  75.         return btc_amount
  76.  
  77.     def sell_all_btc(self):
  78.  
  79.         btc_amount = self.get_btc_amount()
  80.  
  81.         params = {
  82.  
  83.             "type": "EXCHANGE MARKET",
  84.             "symbol": "tTESTBTC:TESTUSD",
  85.             "amount": "-" + str(btc_amount),
  86.             #"price": "1000",
  87.  
  88.         }
  89.  
  90.         response = self.req("v2/auth/w/order/submit", params=params)
  91.         print(response)
  92.         print(response.text)
  93.         return response
  94.  
  95.  
  96.  
  97. client = BitfinexClient()
  98. result = client.wallets()
  99.  
  100. for r in result:
  101.     print(r)
  102.  
  103. result = client.get_btc_amount()
  104. print(result)
  105.  
  106. result = client.sell_all_btc()
  107. print(result)
  108.  
  109.  
  110.