From a72a9475c00bc9c83dda910f8a476854f23bba87 Mon Sep 17 00:00:00 2001 From: Davide Casale Date: Wed, 24 Apr 2024 16:42:54 +0200 Subject: [PATCH] Add support for event base_margin_info and symbol_margin_info. --- MANIFEST | 38 ------------------- .../rest/_interfaces/rest_auth_endpoints.py | 2 +- bfxapi/types/serializers.py | 10 ++++- .../_event_emitter/bfx_event_emitter.py | 4 +- .../_handlers/auth_events_handler.py | 27 ++++++++----- 5 files changed, 31 insertions(+), 50 deletions(-) delete mode 100644 MANIFEST diff --git a/MANIFEST b/MANIFEST deleted file mode 100644 index f5e35c2..0000000 --- a/MANIFEST +++ /dev/null @@ -1,38 +0,0 @@ -# file GENERATED by distutils, do NOT edit -setup.py -bfxapi/__init__.py -bfxapi/_client.py -bfxapi/_version.py -bfxapi/exceptions.py -bfxapi/_utils/__init__.py -bfxapi/_utils/json_decoder.py -bfxapi/_utils/json_encoder.py -bfxapi/_utils/logging.py -bfxapi/rest/__init__.py -bfxapi/rest/_bfx_rest_interface.py -bfxapi/rest/exceptions.py -bfxapi/rest/_interface/__init__.py -bfxapi/rest/_interface/interface.py -bfxapi/rest/_interface/middleware.py -bfxapi/rest/_interfaces/__init__.py -bfxapi/rest/_interfaces/rest_auth_endpoints.py -bfxapi/rest/_interfaces/rest_merchant_endpoints.py -bfxapi/rest/_interfaces/rest_public_endpoints.py -bfxapi/types/__init__.py -bfxapi/types/dataclasses.py -bfxapi/types/labeler.py -bfxapi/types/notification.py -bfxapi/types/serializers.py -bfxapi/websocket/__init__.py -bfxapi/websocket/_connection.py -bfxapi/websocket/exceptions.py -bfxapi/websocket/subscriptions.py -bfxapi/websocket/_client/__init__.py -bfxapi/websocket/_client/bfx_websocket_bucket.py -bfxapi/websocket/_client/bfx_websocket_client.py -bfxapi/websocket/_client/bfx_websocket_inputs.py -bfxapi/websocket/_event_emitter/__init__.py -bfxapi/websocket/_event_emitter/bfx_event_emitter.py -bfxapi/websocket/_handlers/__init__.py -bfxapi/websocket/_handlers/auth_events_handler.py -bfxapi/websocket/_handlers/public_channels_handler.py diff --git a/bfxapi/rest/_interfaces/rest_auth_endpoints.py b/bfxapi/rest/_interfaces/rest_auth_endpoints.py index 6c34b14..50ae249 100644 --- a/bfxapi/rest/_interfaces/rest_auth_endpoints.py +++ b/bfxapi/rest/_interfaces/rest_auth_endpoints.py @@ -248,7 +248,7 @@ class RestAuthEndpoints(Interface): def get_base_margin_info(self) -> BaseMarginInfo: return serializers.BaseMarginInfo.parse( - *(self._m.post("auth/r/info/margin/base")[1]) + *self._m.post("auth/r/info/margin/base") ) def get_symbol_margin_info(self, symbol: str) -> SymbolMarginInfo: diff --git a/bfxapi/types/serializers.py b/bfxapi/types/serializers.py index ae2b3b1..4ec04d7 100644 --- a/bfxapi/types/serializers.py +++ b/bfxapi/types/serializers.py @@ -745,7 +745,15 @@ SymbolMarginInfo = generate_labeler_serializer( BaseMarginInfo = generate_labeler_serializer( name="BaseMarginInfo", klass=dataclasses.BaseMarginInfo, - labels=["user_pl", "user_swaps", "margin_balance", "margin_net", "margin_min"], + labels=[ + "_PLACEHOLDER", + "user_pl", + "user_swaps", + "margin_balance", + "margin_net", + "margin_min", + ], + flat=True, ) PositionClaim = generate_labeler_serializer( diff --git a/bfxapi/websocket/_event_emitter/bfx_event_emitter.py b/bfxapi/websocket/_event_emitter/bfx_event_emitter.py index 3638767..64e59b4 100644 --- a/bfxapi/websocket/_event_emitter/bfx_event_emitter.py +++ b/bfxapi/websocket/_event_emitter/bfx_event_emitter.py @@ -64,6 +64,8 @@ _COMMON = [ "trade_execution", "trade_execution_update", "wallet_update", + "base_margin_info", + "symbol_margin_info", "notification", "on-req-notification", "ou-req-notification", @@ -105,7 +107,7 @@ class BfxEventEmitter(AsyncIOEventEmitter): ) -> Union[_Handler, Callable[[_Handler], _Handler]]: if event not in BfxEventEmitter._EVENTS: raise UnknownEventError( - f"Can't register to unknown event: <{event}> (to get a full" + f"Can't register to unknown event: <{event}> (to get a full " "list of available events see https://docs.bitfinex.com/)." ) diff --git a/bfxapi/websocket/_handlers/auth_events_handler.py b/bfxapi/websocket/_handlers/auth_events_handler.py index 89d6ce8..c756ab4 100644 --- a/bfxapi/websocket/_handlers/auth_events_handler.py +++ b/bfxapi/websocket/_handlers/auth_events_handler.py @@ -51,17 +51,26 @@ class AuthEventsHandler: def handle(self, abbrevation: str, stream: Any) -> None: if abbrevation == "n": self.__notification(stream) + elif abbrevation == "miu": + if stream[0] == "base": + self.__event_emitter.emit( + "base_margin_info", serializers.BaseMarginInfo.parse(*stream) + ) + elif stream[0] == "sym": + self.__event_emitter.emit( + "symbol_margin_info", serializers.SymbolMarginInfo.parse(*stream) + ) + else: + for abbrevations, serializer in AuthEventsHandler.__SERIALIZERS.items(): + if abbrevation in abbrevations: + event = AuthEventsHandler.__ABBREVIATIONS[abbrevation] - for abbrevations, serializer in AuthEventsHandler.__SERIALIZERS.items(): - if abbrevation in abbrevations: - event = AuthEventsHandler.__ABBREVIATIONS[abbrevation] + if all(isinstance(sub_stream, list) for sub_stream in stream): + data = [serializer.parse(*sub_stream) for sub_stream in stream] + else: + data = serializer.parse(*stream) - if all(isinstance(sub_stream, list) for sub_stream in stream): - data = [serializer.parse(*sub_stream) for sub_stream in stream] - else: - data = serializer.parse(*stream) - - self.__event_emitter.emit(event, data) + self.__event_emitter.emit(event, data) def __notification(self, stream: Any) -> None: event: str = "notification"