pixtron / bybit-api

An unofficial node.js lowlevel wrapper for the Bybit Cryptocurrency Derivative exchange API
MIT License
21 stars 16 forks source link

Randomly failing requests and websocket authentication #1

Closed pixtron closed 4 years ago

pixtron commented 4 years ago

Authenticated rest requests to the testnet randomly fail with the error message

{ ret_code: 10002,
  ret_msg: 'invalid request, please check your timestamp and recv_window param.',
  ext_code: '',
  result: null,
  ext_info: null,
  time_now: '1568636398.022206' }

Also websocket authentication fails randomly with a 401 response.

tiagosiebler commented 4 years ago

Hey @pixtron are you still using this? I've tried your wrapper and it generally works, but sometimes, rarely, I see this issue too:

{
  "ret_code": 10002,
  "ret_msg": "invalid request, please check your timestamp and recv_window param. req_timestamp: 1579005481769 server_timestamp: 1579005480587 recv_window: 5000",
  "ext_code": "",
  "result": null,
  "ext_info": null,
  "time_now": "1579005480.578442"
}
pixtron commented 4 years ago

@tiagosiebler do you see that error on the livenet or on the testnet?

tiagosiebler commented 4 years ago

Livenet, I think it might only be immediately after instancing RestClient then calling getUserLeverage() immediately after, but I haven't implemented other parts of this yet, still hooking up websockets.

tiagosiebler commented 4 years ago

Are you using this live? Do you see it happen with livenet at all? In case it's useful, this module for binance hasn't seen any drift issues - not sure if that's thanks to binance or how this module handles it: https://github.com/zoeyg/binance/blob/master/lib/rest.js#L83

tiagosiebler commented 4 years ago

So from more testing I think this may be a race condition. If I call getUserLeverage() almost immediately after instancing RestClient, the issue happens maybe 80% of the time.

If I delay by one second, I don't see this at all. At least not so far...

pixtron commented 4 years ago

In case it's useful, this module for binance hasn't seen any drift issues - not sure if that's thanks to binance or how this module handles it: https://github.com/zoeyg/binance/blob/master/lib/rest.js#L83

This module does basically the same "time synchronisation" as the module you mention.

https://github.com/pixtron/bybit-api/blob/90cd20526fdda20ea70ea436c43ad2d28eabee71/lib/request.js#L41-L47

https://github.com/pixtron/bybit-api/blob/90cd20526fdda20ea70ea436c43ad2d28eabee71/lib/request.js#L86-L98

pixtron commented 4 years ago

req_timestamp: 1579005481769 server_timestamp: 1579005480587 recv_window: 5000

What i can see in your error message above is, that the timestamp of the request is 1182 ms ahead of the servers timestamp.

After some testing on my local client, i was able to reproduce the issue randomly (~30% on livenet). Looks like the livenet discards a request if client timestamp is > 1000ms ahead of server timestamp, whereas on the testnet this might be >2500ms.

Turns out the calculation of the offset was wrong. I fixed that in e5fd04a. @tiagosiebler could you check if that change resolves the issue for you as well?

tiagosiebler commented 4 years ago

Sweet, thanks! Can you publish the update via npm?

pixtron commented 4 years ago

Just published v1.0.3 to npm.

tiagosiebler commented 4 years ago

Nice, so far so good - can't reproduce it anymore. Thanks for fixing!