diff --git a/bfxapi/rest/BfxRest.py b/bfxapi/rest/BfxRest.py index 47bd8f9..941bcbf 100644 --- a/bfxapi/rest/BfxRest.py +++ b/bfxapi/rest/BfxRest.py @@ -5,7 +5,7 @@ import json from ..utils.CustomLogger import CustomLogger from ..utils.auth import generate_auth_headers -from ..models import Wallet +from ..models import Wallet, Order, Position, Trade class BfxRest: @@ -17,30 +17,34 @@ class BfxRest: self.host = host self.logger = CustomLogger('BfxRest', logLevel=logLevel) - async def fetch(self, endpoint): - url = '{}/{}'.format(self.host, endpoint) + async def fetch(self, endpoint, params=""): + url = '{}/{}{}'.format(self.host, endpoint, params) async with aiohttp.ClientSession() as session: async with session.get(url) as resp: text = await resp.text() if resp.status is not 200: raise Exception('GET {} failed with status {} - {}' .format(url, resp.status, text)) - return await resp.json(text) + return await resp.json() - async def post(self, endpoint, data={}): + async def post(self, endpoint, data={}, params=""): url = '{}/{}'.format(self.host, endpoint) sData = json.dumps(data) headers = generate_auth_headers( self.API_KEY, self.API_SECRET, endpoint, sData) headers["content-type"] = "application/json" async with aiohttp.ClientSession() as session: - async with session.post(url, headers=headers, data=sData) as resp: + async with session.post(url + params, headers=headers, data=sData) as resp: text = await resp.text() if resp.status is not 200: raise Exception('POST {} failed with status {} - {}' .format(url, resp.status, text)) return await resp.json() + ################################################## + # Public # + ################################################## + async def get_seed_candles(self, symbol): endpoint = 'candles/trade:1m:{}/hist?limit=5000&_bfx=1'.format(symbol) time_difference = (1000 * 60) * 5000 @@ -62,11 +66,57 @@ class BfxRest: self.logger.info("Downloaded {} candles.".format(len(candles))) return candles + async def get_public_candles(self, symbol, start, end, section='hist', + tf='1m', limit="100", sort=-1): + endpoint = "candles/trade:{}:{}/{}".format(tf, symbol, section) + params = "?start={}&end={}&limit={}&sort={}".format(start, end, limit, sort) + candles = await self.fetch(endpoint, params=params) + return candles + + async def get_public_trades(self, symbol, start, end, limit="100", sort=-1): + endpoint = "trades/{}/hist".format(symbol) + params = "?start={}&end={}&limit={}&sort={}".format(start, end, limit, sort) + trades = await self.fetch(endpoint, params=params) + return trades + + async def get_public_books(self, symbol, precision="P0", length=25): + endpoint = "book/{}/{}".format(symbol, precision) + params = "?len={}".format(length) + books = await self.fetch(endpoint, params) + return books + + async def get_public_ticker(self, symbol): + endpoint = "ticker/{}".format(symbol) + ticker = await self.fetch(endpoint) + return ticker + ################################################## - # Wallets # + # Authenticated # ################################################## async def get_wallets(self): endpoint = "auth/r/wallets" raw_wallets = await self.post(endpoint) - return [ Wallet(rw[0], rw[1], rw[2], rw[3]) for rw in raw_wallets ] + return [ Wallet(*rw[:4]) for rw in raw_wallets ] + + async def get_active_orders(self, symbol): + endpoint = "auth/r/orders/{}".format(symbol) + raw_orders = await self.post(endpoint) + return [ Order.from_raw_order(ro) for ro in raw_orders ] + + async def get_order_history(self, symbol, start, end, limit=25, sort=-1): + endpoint = "auth/r/orders/{}/hist".format(symbol) + params = "?start={}&end={}&limit={}&sort={}".format(start, end, limit, sort) + raw_orders = await self.post(endpoint, params=params) + return [ Order.from_raw_order(ro) for ro in raw_orders ] + + async def get_active_position(self): + endpoint = "auth/r/positions" + raw_positions = await self.post(endpoint) + return [ Position.from_raw_rest_position(rp) for rp in raw_positions ] + + async def get_trades(self, symbol, start, end, limit=25): + endpoint = "auth/r/trades/{}/hist".format(symbol) + params = "?start={}&end={}&limit={}".format(start, end, limit) + raw_trades = await self.post(endpoint, params=params) + return [ Trade.from_raw_rest_trade(rt) for rt in raw_trades ] diff --git a/bfxapi/utils/auth.py b/bfxapi/utils/auth.py index 358c953..6eebd65 100644 --- a/bfxapi/utils/auth.py +++ b/bfxapi/utils/auth.py @@ -17,8 +17,6 @@ def generate_auth_payload(API_KEY, API_SECRET): def generate_auth_headers(API_KEY, API_SECRET, path, body): nonce = str(_gen_nonce()) signature = "/api/v2/{}{}{}".format(path, nonce, body) - print (API_KEY) - print (API_SECRET) h = hmac.new(API_SECRET.encode('utf8'), signature.encode('utf8'), hashlib.sha384) signature = h.hexdigest()