@@ -1364,7 +1364,7 @@ def clear_stream_buffer(self, stream_buffer_name=False):
1364
1364
except KeyError :
1365
1365
return False
1366
1366
1367
- def create_payload (self , stream_id , method , channels = False , markets = False ):
1367
+ def create_payload (self , stream_id , method , channels = None , markets = None ):
1368
1368
"""
1369
1369
Create the payload for subscriptions
1370
1370
@@ -1380,10 +1380,16 @@ def create_payload(self, stream_id, method, channels=False, markets=False):
1380
1380
"""
1381
1381
logger .info ("BinanceWebSocketApiManager.create_payload(" + str (stream_id ) + ", " + str (channels ) + ", " +
1382
1382
str (markets ) + ") started ..." )
1383
- if type (channels ) is str :
1384
- channels = [channels ]
1385
- if type (markets ) is str :
1386
- markets = [markets ]
1383
+ if channels is None or markets is None :
1384
+ logger .info (f"BinanceWebSocketApiManager.create_payload({ str (stream_id )} ) - `channels` and `markets` must "
1385
+ f"be specified!" )
1386
+ return None
1387
+ if channels is not None :
1388
+ if type (channels ) is str :
1389
+ channels = [channels ]
1390
+ if markets is not None :
1391
+ if type (markets ) is str :
1392
+ markets = [markets ]
1387
1393
payload = []
1388
1394
if self .is_exchange_type ("dex" ):
1389
1395
if method == "subscribe" and channels is not False :
@@ -1441,7 +1447,7 @@ def create_payload(self, stream_id, method, channels=False, markets=False):
1441
1447
logger .critical ("BinanceWebSocketApiManager.create_payload(" + str (stream_id ) + ", "
1442
1448
+ str (channels ) + ", " + str (markets ) + ") - Allowed values for `method`: `subscribe` "
1443
1449
"or `unsubscribe`!" )
1444
- return False
1450
+ return None
1445
1451
elif self .is_exchange_type ("cex" ):
1446
1452
final_market = "@arr"
1447
1453
if markets :
@@ -1496,7 +1502,7 @@ def create_payload(self, stream_id, method, channels=False, markets=False):
1496
1502
logger .critical ("BinanceWebSocketApiManager.create_payload(" + str (stream_id ) + ", "
1497
1503
+ str (channels ) + ", " + str (markets ) + ") - Allowed values for `method`: `subscribe` "
1498
1504
"or `unsubscribe`!" )
1499
- return False
1505
+ return None
1500
1506
logger .info ("BinanceWebSocketApiManager.create_payload(" + str (stream_id ) + ", "
1501
1507
+ str (channels ) + ", " + str (markets ) + ") - Payload: " + str (payload ))
1502
1508
logger .info ("BinanceWebSocketApiManager.create_payload(" + str (stream_id ) + ", " + str (channels ) + ", " +
@@ -4071,11 +4077,15 @@ def split_payload(self, params, method, max_items_per_request=350):
4071
4077
payload .append (add_payload )
4072
4078
return payload
4073
4079
else :
4074
- return False
4080
+ logger .error (f"BinanceWebSocketApiManager.split_payload() CEX result is None!" )
4081
+ return None
4075
4082
elif self .is_exchange_type ('dex' ):
4076
- pass
4083
+ # Todo: ???
4084
+ logger .error (f"BinanceWebSocketApiManager.split_payload() DEX result is None!" )
4085
+ return None
4077
4086
else :
4078
- return False
4087
+ logger .error (f"BinanceWebSocketApiManager.split_payload() result is None!" )
4088
+ return None
4079
4089
4080
4090
def start_monitoring_api (self , host = '127.0.0.1' , port = 64201 , warn_on_update = True ):
4081
4091
"""
@@ -4258,7 +4268,7 @@ def _stream_is_stopping(self, stream_id: str = None) -> bool:
4258
4268
self .send_stream_signal (stream_id = stream_id , signal_type = "STOP" )
4259
4269
return True
4260
4270
4261
- def subscribe_to_stream (self , stream_id , channels = [] , markets = [] ):
4271
+ def subscribe_to_stream (self , stream_id , channels = None , markets = None ):
4262
4272
"""
4263
4273
Subscribe channels and/or markets to an existing stream
4264
4274
@@ -4278,24 +4288,30 @@ def subscribe_to_stream(self, stream_id, channels=[], markets=[]):
4278
4288
"""
4279
4289
logger .info (f"BinanceWebSocketApiManager.subscribe_to_stream(" + str (stream_id ) + ", " + str (channels ) +
4280
4290
f", " + str (markets ) + f"){ self .get_debug_log ()} - started ... -" )
4281
- if type (channels ) is str :
4282
- channels = [channels ]
4283
- if type (markets ) is str :
4284
- markets = [markets ]
4285
- if type (channels ) is set :
4286
- channels = list (channels )
4287
- if type (markets ) is set :
4288
- markets = list (markets )
4291
+
4292
+ if channels is not None :
4293
+ if type (channels ) is str :
4294
+ channels = [channels ]
4295
+ if type (channels ) is set :
4296
+ channels = list (channels )
4297
+
4298
+ if markets is not None :
4299
+ if type (markets ) is str :
4300
+ markets = [markets ]
4301
+ if type (markets ) is set :
4302
+ markets = list (markets )
4303
+
4289
4304
if type (self .stream_list [stream_id ]['channels' ]) is str :
4290
4305
self .stream_list [stream_id ]['channels' ] = [self .stream_list [stream_id ]['channels' ]]
4291
- if type (self .stream_list [stream_id ]['markets' ]) is str :
4292
- self .stream_list [stream_id ]['markets' ] = [self .stream_list [stream_id ]['markets' ]]
4293
4306
if type (self .stream_list [stream_id ]['channels' ]) is set :
4294
4307
self .stream_list [stream_id ]['channels' ] = list (self .stream_list [stream_id ]['channels' ])
4308
+ if type (self .stream_list [stream_id ]['markets' ]) is str :
4309
+ self .stream_list [stream_id ]['markets' ] = [self .stream_list [stream_id ]['markets' ]]
4295
4310
if type (self .stream_list [stream_id ]['markets' ]) is set :
4296
4311
self .stream_list [stream_id ]['markets' ] = list (self .stream_list [stream_id ]['markets' ])
4297
4312
4298
4313
self .stream_list [stream_id ]['channels' ] = list (set (self .stream_list [stream_id ]['channels' ] + channels ))
4314
+
4299
4315
markets_new = []
4300
4316
for market in markets :
4301
4317
if "!" in market \
@@ -4310,30 +4326,36 @@ def subscribe_to_stream(self, stream_id, channels=[], markets=[]):
4310
4326
elif self .is_exchange_type ('cex' ):
4311
4327
markets_new .append (str (market ).lower ())
4312
4328
self .stream_list [stream_id ]['markets' ] = list (set (self .stream_list [stream_id ]['markets' ] + markets_new ))
4329
+
4313
4330
payload = self .create_payload (stream_id , "subscribe" ,
4314
4331
channels = self .stream_list [stream_id ]['channels' ],
4315
4332
markets = self .stream_list [stream_id ]['markets' ])
4316
4333
self .stream_list [stream_id ]['subscriptions' ] = self .get_number_of_subscriptions (stream_id )
4334
+
4317
4335
# control subscription limit:
4318
- # https://github.com/binance-exchange/ binance-official -api-docs/blob/5fccfd572db2f530e25e302c02be5dec12759cf9/CHANGELOG.md#2020-04-23
4336
+ # https://github.com/LUCIT-Systems-and-Development/unicorn- binance-websocket -api/wiki/Binance-websocket-endpoint-configuration-overview
4319
4337
if self .stream_list [stream_id ]['subscriptions' ] > self .max_subscriptions_per_stream :
4320
- error_msg = "The limit of " + str (self .max_subscriptions_per_stream ) + " subscriptions per stream has " \
4321
- "been exceeded!"
4322
- logger .critical (f"BinanceWebSocketApiManager.subscribe_to_stream({ str (stream_id )} ) "
4323
- f"Info: { str (error_msg )} " )
4338
+ error_msg = (f"The limit of { str (self .max_subscriptions_per_stream )} subscriptions per stream has been "
4339
+ f"exceeded!" )
4340
+ logger .error (f"BinanceWebSocketApiManager.subscribe_to_stream({ str (stream_id )} ) - Info: { str (error_msg )} " )
4324
4341
self ._crash_stream (stream_id , error_msg = error_msg )
4325
4342
return False
4326
4343
4344
+ if payload is None :
4345
+ logger .error (f"BinanceWebSocketApiManager.subscribe_to_stream({ str (stream_id )} ) - Info: Payload is None!" )
4346
+ return False
4347
+
4327
4348
try :
4328
4349
for item in payload :
4329
4350
self .stream_list [stream_id ]['payload' ].append (item )
4330
- logger .info ("BinanceWebSocketApiManager.subscribe_to_stream(" + str (stream_id ) + ", " + str (channels ) +
4331
- ", " + str (markets ) + ") finished ..." )
4351
+ logger .info (f"BinanceWebSocketApiManager.subscribe_to_stream({ str (stream_id )} , { str (channels )} , "
4352
+ f"{ str (markets )} ) finished ..." )
4353
+ return True
4332
4354
except TypeError as error_msg :
4333
4355
logger .error (f"BinanceWebSocketApiManager.subscribe_to_stream({ str (stream_id )} ) - TypeError - "
4334
4356
f"{ str (error_msg )} " )
4335
4357
return False
4336
- return True
4358
+
4337
4359
4338
4360
def unsubscribe_from_stream (self , stream_id , channels = None , markets = None ):
4339
4361
"""
0 commit comments