From e0785f9f4a5225db40cb2dec01a1b205e9fd95ea Mon Sep 17 00:00:00 2001 From: Davide Casale Date: Thu, 8 Dec 2022 17:35:39 +0100 Subject: [PATCH] Add support for GET book/{Symbol}/{Precision} endpoint. --- bfxapi/rest/BfxRestInterface.py | 12 +++++++++++- bfxapi/rest/serializers.py | 26 ++++++++++++++++++++++++++ bfxapi/rest/typings.py | 14 ++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/bfxapi/rest/BfxRestInterface.py b/bfxapi/rest/BfxRestInterface.py index c60cbc1..662a94c 100644 --- a/bfxapi/rest/BfxRestInterface.py +++ b/bfxapi/rest/BfxRestInterface.py @@ -5,7 +5,7 @@ from http import HTTPStatus from typing import List, Union, Optional from . import serializers -from .typings import PlatformStatus, TradingPairTicker, FundingCurrencyTicker, TickerHistories, TradingPairTrades, FundingCurrencyTrades +from .typings import * from .exceptions import RequestParametersError class BfxRestInterface(object): @@ -59,4 +59,14 @@ class BfxRestInterface(object): }[symbol[0]](*subdata) for subdata in self.__GET(f"trades/{symbol}/hist", params=params) + ] + + def book(self, symbol: str, precision: str, len: Optional[int]) -> Union[TradingPairBooks, FundingCurrencyBooks, TradingPairRawBooks, FundingCurrencyRawBooks]: + return [ + { + "t": precision == "R0" and serializers.TradingPairRawBook.parse or serializers.TradingPairBook.parse, + "f": precision == "R0" and serializers.FundingCurrencyRawBook.parse or serializers.FundingCurrencyBook.parse, + }[symbol[0]](*subdata) + + for subdata in self.__GET(f"book/{symbol}/{precision}", params={ "len": len }) ] \ No newline at end of file diff --git a/bfxapi/rest/serializers.py b/bfxapi/rest/serializers.py index a002cdf..d1c64c2 100644 --- a/bfxapi/rest/serializers.py +++ b/bfxapi/rest/serializers.py @@ -94,4 +94,30 @@ FundingCurrencyTrade = _Serializer[typings.FundingCurrencyTrade]("FundingCurrenc "PERIOD" ]) +TradingPairBook = _Serializer[typings.TradingPairBook]("TradingPairBook", labels=[ + "PRICE", + "COUNT", + "AMOUNT" +]) + +FundingCurrencyBook = _Serializer[typings.FundingCurrencyBook]("FundingCurrencyBook", labels=[ + "RATE", + "PERIOD", + "COUNT", + "AMOUNT" +]) + +TradingPairRawBook = _Serializer[typings.TradingPairRawBook]("TradingPairRawBook", labels=[ + "ORDER_ID", + "PRICE", + "AMOUNT" +]) + +FundingCurrencyRawBook = _Serializer[typings.FundingCurrencyRawBook]("FundingCurrencyRawBook", labels=[ + "OFFER_ID", + "PERIOD", + "RATE", + "AMOUNT" +]) + #endregion \ No newline at end of file diff --git a/bfxapi/rest/typings.py b/bfxapi/rest/typings.py index d511088..3b52a49 100644 --- a/bfxapi/rest/typings.py +++ b/bfxapi/rest/typings.py @@ -54,4 +54,18 @@ TickerHistories = List[TickerHistory] (TradingPairTrades, FundingCurrencyTrades) = (List[TradingPairTrade], List[FundingCurrencyTrade]) +(TradingPairBook, FundingCurrencyBook) = ( + TypedDict("TradingPairBook", { "PRICE": float, "COUNT": int, "AMOUNT": float }), + TypedDict("FundingCurrencyBook", { "RATE": float, "PERIOD": int, "COUNT": int, "AMOUNT": float }) +) + +(TradingPairBooks, FundingCurrencyBooks) = (List[TradingPairBook], List[FundingCurrencyBook]) + +(TradingPairRawBook, FundingCurrencyRawBook) = ( + TypedDict("TradingPairRawBook", { "ORDER_ID": int, "PRICE": float, "AMOUNT": float }), + TypedDict("FundingCurrencyRawBook", { "OFFER_ID": int, "PERIOD": int, "RATE": float, "AMOUNT": float }), +) + +(TradingPairRawBooks, FundingCurrencyRawBooks) = (List[TradingPairRawBook], List[FundingCurrencyRawBook]) + #endregion \ No newline at end of file