Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring manager for ease testing #305

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ on, you can use the [UNICORN Binance REST API](https://www.lucit.tech/unicorn-bi
| [Binance JEX](https://www.jex.com) | `jex.com` |
| [Binance DEX](https://www.binance.org) | `binance.org` |
| [Binance DEX Testnet](https://testnet.binance.org) | `binance.org-testnet` |
| Localhost | `localhost` |

- Streams are processing asynchronous/concurrent (Python asyncio) and each stream is started in a separate thread, so
you dont need to deal with asyncio in your code!
Expand Down
109 changes: 31 additions & 78 deletions unicorn_binance_websocket_api/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@
from unicorn_binance_websocket_api.sockets import BinanceWebSocketApiSocket
from unicorn_binance_websocket_api.restclient import BinanceWebSocketApiRestclient
from unicorn_binance_websocket_api.restserver import BinanceWebSocketApiRestServer
from unicorn_binance_websocket_api.ws_connection_settings import CEX_EXCHANGES, DEX_EXCHANGES, ws_connection_settings
from cheroot import wsgi
from collections import deque
from datetime import datetime
from flask import Flask, redirect
from flask_restful import Api
from typing import Optional, Union
from typing import Literal, Optional, Union
import asyncio
import colorama
import copy
Expand Down Expand Up @@ -120,7 +121,7 @@ class BinanceWebSocketApiManager(threading.Thread):
:param exchange: Select binance.com, binance.com-testnet, binance.com-margin, binance.com-margin-testnet,
binance.com-isolated_margin, binance.com-isolated_margin-testnet, binance.com-futures,
binance.com-futures-testnet, binance.com-coin_futures, binance.us, trbinance.com,
jex.com, binance.org or binance.org-testnet (default: binance.com)
jex.com, binance.org, localhost, binance.org-testnet (default: binance.com)
:type exchange: str
:param warn_on_update: set to `False` to disable the update warning
:type warn_on_update: bool
Expand Down Expand Up @@ -196,7 +197,10 @@ def __init__(self,
ping_interval_default: int = 5,
ping_timeout_default: int = 10,
high_performance=False,
debug=False):
debug=False,
websocket_base_uri: Optional[str] = None,
max_subscriptions_per_stream: Optional[int] = None,
exchange_type: Optional[Literal['cex', 'dex']] = None):
threading.Thread.__init__(self)
self.name = "unicorn-binance-websocket-api"
self.version = "1.41.0.dev"
Expand Down Expand Up @@ -225,56 +229,30 @@ def __init__(self,
# use the provided method to process stream signals:
self.process_stream_signals = process_stream_signals
logger.info(f"Using `process_stream_signals` ...")
self.exchange = exchange
if self.exchange == "binance.com":
self.websocket_base_uri = "wss://stream.binance.com:9443/"
self.max_subscriptions_per_stream = 1024
elif self.exchange == "binance.com-testnet":
self.websocket_base_uri = "wss://testnet.binance.vision/"
self.max_subscriptions_per_stream = 1024
elif self.exchange == "binance.com-margin":
self.websocket_base_uri = "wss://stream.binance.com:9443/"
self.max_subscriptions_per_stream = 1024
elif self.exchange == "binance.com-margin-testnet":
self.websocket_base_uri = "wss://testnet.binance.vision/"
self.max_subscriptions_per_stream = 1024
elif self.exchange == "binance.com-isolated_margin":
self.websocket_base_uri = "wss://stream.binance.com:9443/"
self.max_subscriptions_per_stream = 1024
elif self.exchange == "binance.com-isolated_margin-testnet":
self.websocket_base_uri = "wss://testnet.binance.vision/"
self.max_subscriptions_per_stream = 1024
elif self.exchange == "binance.com-futures":
self.websocket_base_uri = "wss://fstream.binance.com/"
self.max_subscriptions_per_stream = 200
elif self.exchange == "binance.com-coin-futures" or self.exchange == "binance.com-coin_futures":
self.websocket_base_uri = "wss://dstream.binance.com/"
self.max_subscriptions_per_stream = 200
elif self.exchange == "binance.com-futures-testnet":
self.websocket_base_uri = "wss://stream.binancefuture.com/"
self.max_subscriptions_per_stream = 200
elif self.exchange == "binance.us":
self.websocket_base_uri = "wss://stream.binance.us:9443/"
self.max_subscriptions_per_stream = 1024
elif self.exchange == "trbinance.com":
self.websocket_base_uri = "wss://stream-cloud.trbinance.com/"
self.max_subscriptions_per_stream = 1024
elif self.exchange == "jex.com":
self.websocket_base_uri = "wss://ws.jex.com/"
self.max_subscriptions_per_stream = 10
elif self.exchange == "binance.org":
self.websocket_base_uri = "wss://dex.binance.org/api/"
self.max_subscriptions_per_stream = 1024
elif self.exchange == "binance.org-testnet":
self.websocket_base_uri = "wss://testnet-dex.binance.org/api/"
self.max_subscriptions_per_stream = 1024
else:
# Unknown Exchange
error_msg = f"Unknown exchange '{str(self.exchange)}'! Read the docs to see a list of supported " \
f"exchanges: https://github.com/LUCIT-Systems-and-Development/unicorn-binance-websocket-api/wiki/" \
f"Binance-websocket-endpoint-configuration-overview"
if exchange not in ws_connection_settings:
error_msg = f"Unknown exchange '{str(exchange)}'! Read the docs to see a list of supported " \
"exchanges: https://unicorn-binance-websocket-api.docs.lucit.tech/unicorn_" \
"binance_websocket_api.html#module-unicorn_binance_websocket_api.unicorn_binance_websocket_" \
"api_manager"
logger.critical(error_msg)
raise UnknownExchange(error_msg)

self.exchange = exchange
self.websocket_base_uri = websocket_base_uri or ws_connection_settings[self.exchange][0]
self.max_subscriptions_per_stream = max_subscriptions_per_stream or ws_connection_settings[self.exchange][1]

self.exchange_type = exchange_type
logger.info(f"{self.exchange_type=}")
if not self.exchange_type:
if self.exchange in DEX_EXCHANGES:
self.exchange_type = "dex"
elif self.exchange in CEX_EXCHANGES:
self.exchange_type = "cex"
else:
logger.critical(f"BinanceWebSocketApiManager.is_exchange_type() - Can not determine exchange type for"
f"exchange={str(self.exchange)}")
self.exchange_type = None

self.stop_manager_request = None
self.all_subscriptions_number = 0
self.binance_api_status = {'weight': None,
Expand Down Expand Up @@ -2655,34 +2633,9 @@ def is_exchange_type(self, exchange_type=False):
:type exchange_type: str
:return: bool
"""
if exchange_type is False:
return False
if self.exchange == "binance.org" or \
self.exchange == "binance.org-testnet":
is_type = "dex"
elif self.exchange == "binance.com" or \
self.exchange == "binance.com-testnet" or \
self.exchange == "binance.com-margin" or \
self.exchange == "binance.com-margin-testnet" or \
self.exchange == "binance.com-isolated_margin" or \
self.exchange == "binance.com-isolated_margin-testnet" or \
self.exchange == "binance.com-futures" or \
self.exchange == "binance.com-futures-testnet" or \
self.exchange == "binance.com-coin-futures" or \
self.exchange == "binance.com-coin_futures" or \
self.exchange == "binance.je" or \
self.exchange == "binance.us" or \
self.exchange == "trbinance.com" or \
self.exchange == "jex.com":
is_type = "cex"
else:
logger.critical(f"BinanceWebSocketApiManager.is_exchange_type() - Can not determine exchange type for"
f"exchange={str(self.exchange)}")
return False
if is_type == exchange_type:
return True
else:
if exchange_type is False or not self.exchange_type:
return False
return self.exchange_type == exchange_type

def is_stop_request(self, stream_id, exclude_kill_requests=False):
"""
Expand Down
59 changes: 59 additions & 0 deletions unicorn_binance_websocket_api/ws_connection_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from enum import Enum
from typing import Tuple


WEBSOCKET_BASE_URI = str
MAX_SUBSCRIPTIONS_PER_STREAM = int


class Exchanges(str, Enum):
BINANCE = "binance.com"
BINANCE_TESTNET = "binance.com-testnet"
BINANCE_MARGIN = "binance.com-margin"
BINANCE_MARGIN_TESTNET = "binance.com-margin-testnet"
BINANCE_ISOLATED_MARGIN = "binance.com-isolated_margin"
BINANCE_ISOLATED_MARGIN_TESTNET = "binance.com-isolated_margin-testnet"
BINANCE_FUTURES = "binance.com-futures"
BINANCE_COIN_FUTURES = "binance.com-coin-futures"
BINANCE_FUTURES_TESTNET = "binance.com-futures-testnet"
BINANCE_US = "binance.us"
TRBINANCE = "trbinance.com"
JEX = "jex.com"
BINANCE_ORG = "binance.org"
BINANCE_ORG_TESTNET = "binance.org-testnet"
LOCALHOST = "localhost"


DEX_EXCHANGES = [Exchanges.BINANCE_ORG, Exchanges.BINANCE_ORG_TESTNET]
CEX_EXCHANGES = [
Exchanges.BINANCE,
Exchanges.BINANCE_TESTNET,
Exchanges.BINANCE_MARGIN,
Exchanges.BINANCE_MARGIN_TESTNET,
Exchanges.BINANCE_ISOLATED_MARGIN,
Exchanges.BINANCE_ISOLATED_MARGIN_TESTNET,
Exchanges.BINANCE_FUTURES,
Exchanges.BINANCE_COIN_FUTURES,
Exchanges.BINANCE_FUTURES_TESTNET,
Exchanges.BINANCE_US,
Exchanges.TRBINANCE,
Exchanges.JEX,
]

ws_connection_settings: dict[str, Tuple[WEBSOCKET_BASE_URI, MAX_SUBSCRIPTIONS_PER_STREAM]] = {
Exchanges.BINANCE: ("wss://stream.binance.com:9443/", 1024),
Exchanges.BINANCE_TESTNET: ("wss://testnet.binance.vision/", 1024),
Exchanges.BINANCE_MARGIN: ("wss://stream.binance.com:9443/", 1024),
Exchanges.BINANCE_MARGIN_TESTNET: ("wss://testnet.binance.vision/", 1024),
Exchanges.BINANCE_ISOLATED_MARGIN: ("wss://stream.binance.com:9443/", 1024),
Exchanges.BINANCE_ISOLATED_MARGIN_TESTNET: ("wss://testnet.binance.vision/", 1024),
Exchanges.BINANCE_FUTURES: ("wss://fstream.binance.com/", 200),
Exchanges.BINANCE_COIN_FUTURES: ("wss://dstream.binance.com/", 200),
Exchanges.BINANCE_FUTURES_TESTNET: ("wss://stream.binancefuture.com/", 200),
Exchanges.BINANCE_US: ("wss://stream.binance.us:9443/", 1024),
Exchanges.TRBINANCE: ("wss://stream-cloud.trbinance.com/", 1024),
Exchanges.JEX: ("wss://ws.jex.com/", 10),
Exchanges.BINANCE_ORG: ("wss://dex.binance.org/api/", 1024),
Exchanges.BINANCE_ORG_TESTNET: ("wss://testnet-dex.binance.org/api/", 1024),
Exchanges.LOCALHOST: ("ws://127.0.0.1:8765/", 1024),
}