LUCIT-Systems-and-Development / unicorn-binance-websocket-api

A Python SDK by LUCIT to use the Binance Websocket API`s (com+testnet, com-margin+testnet, com-isolated_margin+testnet, com-futures+testnet, com-coin_futures, us, tr, dex/chain+testnet) in a simple, fast, flexible, robust and fully-featured way.
https://unicorn-binance-websocket-api.docs.lucit.tech/
Other
677 stars 166 forks source link

userData stream doesn't work on binance-futures-testnet #335

Closed Tidone closed 4 months ago

Tidone commented 1 year ago

Version of this library.

unicorn_fy: 0.12.2 unicorn_binance_local_depth_cache: not found unicorn_binance_rest_api: 1.9.0 unicorn_binance_trailing_stop_loss: not found unicorn_binance_websocket_api: 1.46.1

Solution to Issue cannot be found in the documentation or other Issues and also occurs in the latest version of this library.

Hardware?

Local server/workstation

Operating System?

Linux

Python version?

Python3.10

Installed packages

aiohttp                       3.8.4
aiosignal                     1.2.0
aniso8601                     9.0.1
apprise                       1.3.0
async-timeout                 4.0.2
attrs                         21.4.0
bidict                        0.22.0
cachetools                    5.3.0
certifi                       2021.10.8
cffi                          1.15.0
charset-normalizer            2.0.12
cheroot                       8.6.0
click                         8.1.3
colorama                      0.4.4
cryptography                  37.0.2
dateparser                    1.1.1
dnspython                     2.2.1
eventlet                      0.33.1
Flask                         2.3.1
Flask-Cors                    3.0.10
Flask-RESTful                 0.3.9
Flask-SocketIO                5.3.3
frozenlist                    1.3.0
greenlet                      1.1.2
h11                           0.13.0
idna                          3.3
itsdangerous                  2.1.2
jaraco.functools              3.5.0
Jinja2                        3.1.2
Markdown                      3.3.7
MarkupSafe                    2.1.1
more-itertools                8.13.0
multidict                     6.0.2
oauthlib                      3.2.0
pip                           22.0.0
psutil                        5.9.1
pyasn1                        0.4.8
pyasn1-modules                0.2.8
pycparser                     2.21
pyOpenSSL                     22.0.0
python-binance                1.0.17
python-dateutil               2.8.2
python-engineio               4.3.2
python-socketio               5.6.0
pytz                          2022.1
pytz-deprecation-shim         0.1.0.post0
PyYAML                        6.0
regex                         2023.3.2
requests                      2.29.0
requests-oauthlib             1.3.1
schedule                      1.2.0
service-identity              21.1.0
setuptools                    60.2.0
simple-websocket              0.10.0
six                           1.16.0
SQLAlchemy                    2.0.12
tzdata                        2022.1
tzlocal                       4.2
ujson                         5.3.0
unicorn-binance-websocket-api 1.46.1
urllib3                       1.26.9
websocket-client              1.3.2
websockets                    10.4
Werkzeug                      2.3.1
wheel                         0.37.1
wsproto                       1.1.0
yarl                          1.7.2

Logging output

2023-05-01 12:39:51,179 [INFO    ] 4026210 140628987846720 manager: New instance of unicorn-binance-websocket-api_1.46.1-python_3.10.7 on Linux 5.19.0-40-generic for exchange binance.com-futures-testnet started ...
2023-05-01 12:39:51,179 [INFO    ] 4026210 140628987846720 manager: Debug is False
2023-05-01 12:39:51,179 [INFO    ] 4026210 140628987846720 manager: Initiating `colorama_0.4.4`
2023-05-01 12:39:51,179 [INFO    ] 4026210 140628987846720 manager: Using `websockets_10.4`
2023-05-01 12:39:51,179 [INFO    ] 4026210 140628987846720 manager: Using `stream_buffer`
2023-05-01 12:39:51,179 [INFO    ] 4026210 140628987846720 manager: Using `stream_signal_buffer`
2023-05-01 12:39:51,180 [INFO    ] 4026210 140628987846720 manager: Using exchange_type 'cex' ...
2023-05-01 12:39:51,181 [INFO    ] 4026210 140628807112384 manager: BinanceWebSocketApiManager._frequent_checks() new instance created with frequent_checks_id=1682937591.1815612
2023-05-01 12:39:51,181 [INFO    ] 4026210 140628798719680 manager: BinanceWebSocketApiManager._keepalive_streams() new instance created with keepalive_streams_id=1682937591.1818385
2023-05-01 12:39:51,182 [DEBUG   ] 4026210 140628987846720 connectionpool: Starting new HTTPS connection (1): api.github.com:443
2023-05-01 12:39:51,270 [DEBUG   ] 4026210 140628987846720 connectionpool: https://api.github.com:443 "GET /repos/LUCIT-Systems-and-Development/unicorn-binance-websocket-api/releases/latest HTTP/1.1" 200 1108
2023-05-01 12:39:51,272 [INFO    ] 4026210 140628987846720 manager: BinanceWebSocketApiManager.create_stream(['arr'], ['!userData'], None, False, False, False, False) with stream_id=ff8f29638dd8-79b7-5f70-7018-978de89e
2023-05-01 12:39:51,272 [INFO    ] 4026210 140628987846720 manager: BinanceWebSocketApiManager._add_stream_to_stream_list(ff8f29638dd8-79b7-5f70-7018-978de89e, ['arr'], ['!userData'], None, False, None, False)
2023-05-01 12:39:51,272 [DEBUG   ] 4026210 140628987846720 selector_events: Using selector: EpollSelector
2023-05-01 12:39:51,272 [DEBUG   ] 4026210 140628987846720 manager: BinanceWebSocketApiManager.set_socket_is_not_ready(ff8f29638dd8-79b7-5f70-7018-978de89e)
2023-05-01 12:39:51,272 [DEBUG   ] 4026210 140628815505088 manager: BinanceWebSocketApiManager.is_stop_request(ff8f29638dd8-79b7-5f70-7018-978de89e)
2023-05-01 12:39:51,272 [DEBUG   ] 4026210 140628987846720 manager: BinanceWebSocketApiManager.create_stream(['arr'], ['!userData'], None, False, False, None, False) with stream_id=ff8f29638dd8-79b7-5f70-7018-978de89e - Waiting till new socket and asyncio is ready
2023-05-01 12:39:51,272 [INFO    ] 4026210 140628815505088 sockets: BinanceWebSocketApiSocket.start_socket(ff8f29638dd8-79b7-5f70-7018-978de89e, ['arr'], ['!userData']) socket_id=bc67b210-001b-4047-937b-7fdc272136e5 recent_socket_id=bc67b210-001b-4047-937b-7fdc272136e5
2023-05-01 12:39:51,273 [DEBUG   ] 4026210 140628815505088 connection: BinanceWebSocketApiConnection.receive(ff8f29638dd8-79b7-5f70-7018-978de89e) socket_id=bc67b210-001b-4047-937b-7fdc272136e5) - Adding timeout to `websocket.recv()` 
2023-05-01 12:39:51,273 [DEBUG   ] 4026210 140628815505088 manager: BinanceWebSocketApiManager.is_stop_request(ff8f29638dd8-79b7-5f70-7018-978de89e)
2023-05-01 12:39:51,273 [INFO    ] 4026210 140628815505088 restclient: BinanceWebSocketApiRestclient.get_listen_key() symbol='False' stream_id='ff8f29638dd8-79b7-5f70-7018-978de89e')
2023-05-01 12:39:51,273 [INFO    ] 4026210 140628815505088 manager: New instance of unicorn-binance-rest-api_1.9.0-python_3.10.7 on Linux 5.19.0-40-generic for exchange binance.com-futures-testnet started ...
2023-05-01 12:39:51,273 [INFO    ] 4026210 140628815505088 manager: Initiating `colorama_0.4.4`
2023-05-01 12:39:51,273 [DEBUG   ] 4026210 140628815505088 connectionpool: Starting new HTTPS connection (1): testnet.binance.vision:443
2023-05-01 12:39:51,551 [DEBUG   ] 4026210 140628815505088 connectionpool: https://testnet.binance.vision:443 "GET /api/v3/ping HTTP/1.1" 200 2
2023-05-01 12:39:52,236 [DEBUG   ] 4026210 140628815505088 connectionpool: https://testnet.binance.vision:443 "GET /api/v3/time HTTP/1.1" 200 28
2023-05-01 12:39:52,237 [DEBUG   ] 4026210 140628815505088 connectionpool: Starting new HTTPS connection (1): api.github.com:443
2023-05-01 12:39:52,274 [DEBUG   ] 4026210 140628987846720 manager: BinanceWebSocketApiManager.create_stream(['arr'], ['!userData'], None, False, False, None, False) with stream_id=ff8f29638dd8-79b7-5f70-7018-978de89e - Waiting till new socket and asyncio is ready
2023-05-01 12:39:52,483 [DEBUG   ] 4026210 140628815505088 connectionpool: https://api.github.com:443 "GET /repos/LUCIT-Systems-and-Development/unicorn-binance-rest-api/releases/latest HTTP/1.1" 200 1051
2023-05-01 12:39:52,718 [DEBUG   ] 4026210 140628815505088 connectionpool: https://testnet.binance.vision:443 "POST /api/v3/userDataStream HTTP/1.1" 401 70
2023-05-01 12:39:52,720 [CRITICAL] 4026210 140628815505088 manager: BinanceWebSocketApiManager.create_websocket_uri(['arr'], ['!userData'], , False) - Received known error code from rest client: {'code': -2015, 'msg': 'Invalid API-key, IP, or permissions for action.'}
2023-05-01 12:39:52,720 [CRITICAL] 4026210 140628815505088 connection: BinanceWebSocketApiConnection.await._conn.__aenter__(ff8f29638dd8-79b7-5f70-7018-978de89e, ['arr'], ['!userData']) - error: 4 - Invalid API-key, IP, or permissions for action.
2023-05-01 12:39:52,720 [CRITICAL] 4026210 140628815505088 connection: BinanceWebSocketApiConnection.await._conn.__aenter__(ff8f29638dd8-79b7-5f70-7018-978de89e, ['arr'], ['!userData']) - error: 6 - 'ff8f29638dd8-79b7-5f70-7018-978de89e'
2023-05-01 12:39:52,720 [CRITICAL] 4026210 140628815505088 manager: BinanceWebSocketApiManager.stream_is_crashing(ff8f29638dd8-79b7-5f70-7018-978de89e)
2023-05-01 12:39:52,720 [DEBUG   ] 4026210 140628815505088 manager: BinanceWebSocketApiManager.set_socket_is_ready(ff8f29638dd8-79b7-5f70-7018-978de89e)

Processing method?

stream_buffer

Used endpoint?

binance.com-futures-testnet

Issue

Hi,

I noticed that creating a userdata stream on futures-testnet doesn't work on the last few versions of unicorn-binance-websocket-api. Whenever a userdata stream is created, a critical error is thrown (Received known error code from rest client: {'code': -2015, 'msg': 'Invalid API-key, IP, or permissions for action.'}) and the program exits.

The last working version of unicorn-binance-websocket-api was 1.43.3. (And yes, the API key is valid. The same key which is working on 1.43.3, throws the above error on the newest versions.)

I already tried creating a new API key (and a new account) on testnet, but the error persists.

Here's a simple code to reproduce the error, which is taken from the userdata_stream_new_style example:

from unicorn_binance_websocket_api.manager import BinanceWebSocketApiManager
import logging
import time
import threading
import os

def main():
    logging.getLogger("unicorn_binance_websocket_api")
    #logging.basicConfig(level=logging.DEBUG,
    #                    filename=os.path.basename(__file__) + '.log',
    #                    format="{asctime} [{levelname:8}] {process} {thread} {module}: {message}",
    #                    style="{")

    def print_stream_data_from_stream_buffer(binance_websocket_api_manager):
        while True:
            if binance_websocket_api_manager.is_manager_stopping():
                exit(0)
            oldest_stream_data_from_stream_buffer = binance_websocket_api_manager.pop_stream_data_from_stream_buffer()
            if oldest_stream_data_from_stream_buffer is False:
                time.sleep(0.01)
            else:
                print(oldest_stream_data_from_stream_buffer)

    # configure api key and secret for binance.com
    api_key = "KEY"
    api_secret = "SECRET"

    # create instances of BinanceWebSocketApiManager
    ubwa_com = BinanceWebSocketApiManager(exchange="binance.com-futures-testnet")

    # create the userData streams
    #the script throws an error here
    user_stream_id = ubwa_com.create_stream('arr', '!userData', api_key=api_key, api_secret=api_secret)

    # start a worker process to move the received stream_data from the stream_buffer to a print function
    worker_thread = threading.Thread(target=print_stream_data_from_stream_buffer, args=(ubwa_com,))
    worker_thread.start()

    # monitor the streams
    while True:
        ubwa_com.print_stream_info(user_stream_id)
        time.sleep(1)
smooth-lee commented 1 year ago

I'm having the same problem.

I solved it by lowering the version, but hopefully it will be resolved soon.

jmy12k3 commented 11 months ago

@smooth-lee May I ask you which version have you downgraded to? I have encountered the same issue. Thanks

oliver-zehentleitner commented 4 months ago

Duplicate of https://github.com/LUCIT-Systems-and-Development/unicorn-binance-websocket-api/issues/347

Its fixed and will be released within the next few hours.