bybit-exchange / pybit

Official Python3 API connector for Bybit's HTTP and WebSockets APIs.
Other
418 stars 135 forks source link

i fix th http_manager, to snych with server time #232

Closed TRON-FAN closed 4 months ago

TRON-FAN commented 4 months ago

ERROR - invalid request, please check your server timestamp or recv_window param. req_timestamp[XXXXX],server_timestamp[XXXXX],recv_window[5000] (ErrCode: 10002). Added 2.5 seconds to recv_window. 2 retries remain.

This problem fucked up my live hahaha, needed few hours find the solution

UPDATE in:

_http_manager.py # first get UTC Time, later USE from session the bybit server tim maybe you have already, but for me find a solution, was very hard - i fixed the file in my version

def get_bybit_server_time(session): try: response = session.get_server_time() if response['retCode'] == 0: time_second = int(response['result']['timeSecond']) time_nano = int(response['result']['timeNano']) logging.info(f"Erfolgreich die Bybit-Serverzeit abgerufen: {time_second} Sekunden, {time_nano} Nanosekunden") print(f"Erfolgreich die Bybit-Serverzeit abgerufen: {time_second} Sekunden, {time_nano} Nanosekunden")

        converted_time = datetime.datetime.utcfromtimestamp(time_second)

        converted_time_nanoseconds = datetime.datetime.utcfromtimestamp(time_nano // 1000000000)

        print(f"Erfolgreich die Bybit-Serverzeit abgerufen: {converted_time.strftime('-%d-%m-%Y                               %H:%M:%S')} UTC")
        logging.info(f"Erfolgreich die Bybit-Serverzeit abgerufen: {converted_time.strftime('-%d-%m-%Y                               %H:%M:%S')} UTC")
        #print(f"Erfolgreich die Bybit-Serverzeit abgerufen (Nanosekunden): {converted_time_nanoseconds.strftime('%Y-%m-%d %H:%M:%S.%f')} UTC")

        return time_second, time_nano
    else:
        logging.error(f"Fehler beim Abrufen der Bybit-Serverzeit: {response['retMsg']}")
        print(f"Fehler beim Abrufen der Bybit-Serverzeit: {response['retMsg']}")
        return None, None
except Exception as e:
    logging.error(f"Fehler beim Abrufen der Bybit-Serverzeit: {e}")
    print(f"Fehler beim Abrufen der Bybit-Serverzeit: {e}")
    return None, None

def generate_req_timestamp(session): try: if session:

        server_time_second, server_time_nano = get_bybit_server_time(session)
        return server_time_nano  # Rückgabe des Nano-Sekunden-Werts vom Server

    if not session:
        print("Keine Session vorhanden, verwende UTC-Zeit als req_timestamp")

        # Aktuelle UTC-Zeit
        now_utc = datetime.datetime.utcnow()
        # Unix-Timestamp in Sekunden seit der Epoch
        epoch_time = (now_utc - datetime.datetime(1970, 1, 1)).total_seconds()
        # Umwandlung in Millisekunden
        utc_milliseconds = int(epoch_time * 1000)

        return utc_milliseconds  

except Exception as e:
    # Fallback
    now_utc = datetime.datetime.utcnow()
    epoch_time = (now_utc - datetime.datetime(1970, 1, 1)).total_seconds()
    utc_milliseconds = int(epoch_time * 1000)

    logging.error(f"Fehler beim Generieren des req_timestamp: {e}, verwendet UTC-Zeit: {utc_milliseconds}")
    print(f"Fehler beim Generieren des req_timestamp: {e}")

    return utc_milliseconds  # Oder eine andere sinnvolle Rückgabe, je nach Bedarf

HERE THE CHANGES:

Authenticate if we are using a private endpoint.

        if auth:
            # Prepare signature.
            timestamp = generate_req_timestamp(session)
            signature = self._auth(
                payload=req_params,
                recv_window=recv_window,
                timestamp=timestamp,
            )
dextertd commented 4 months ago

This is not a real fix. The real fix is to ensure your system clock is right, which is not the responsibility of pybit or even python. It's at the OS-level.

https://github.com/tiagosiebler/awesome-crypto-examples/wiki/Timestamp-for-this-request-is-outside-of-the-recvWindow