From 18977499f84291ce42be9fc0dc1f3bae99bfc76b Mon Sep 17 00:00:00 2001 From: tiagosiebler Date: Sat, 28 May 2022 20:39:37 +0100 Subject: [PATCH] v2.2.1: fix ws & rest authentication issues caused by time-sync mechanism --- examples/README.md | 6 +++++ examples/ws-private.ts | 55 ++++++++++++++++++++++++++++++++++++++ package.json | 2 +- src/util/BaseRestClient.ts | 3 ++- src/websocket-client.ts | 8 ++++-- 5 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 examples/README.md create mode 100644 examples/ws-private.ts diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 0000000..6860449 --- /dev/null +++ b/examples/README.md @@ -0,0 +1,6 @@ +# Examples + +These samples can be executed using `ts-node`: +``` +ts-node ./examples/rest-spot-public.ts +``` diff --git a/examples/ws-private.ts b/examples/ws-private.ts new file mode 100644 index 0000000..34e0685 --- /dev/null +++ b/examples/ws-private.ts @@ -0,0 +1,55 @@ +import { DefaultLogger } from '../src'; +import { WebsocketClient, wsKeySpotPublic } from '../src/websocket-client'; + +// or +// import { DefaultLogger, WebsocketClient } from 'bybit-api'; + +(async () => { + const logger = { + ...DefaultLogger, + // silly: () => {}, + }; + + const key = process.env.API_KEY; + const secret = process.env.API_SECRET; + + // USDT Perps: + const market = 'linear'; + // Inverse Perp + // const market = 'inverse'; + // const market = 'spot'; + + // Note: the WebsocketClient defaults to testnet. Set `livenet: true` to use live markets. + const wsClient = new WebsocketClient( + { + key: key, + secret: secret, + market: market, + livenet: true, + restOptions: { + // disable_time_sync: true, + }, + }, + logger + ); + + wsClient.on('update', (data) => { + console.log('raw message received ', JSON.stringify(data, null, 2)); + }); + + wsClient.on('open', (data) => { + console.log('connection opened open:', data.wsKey); + }); + wsClient.on('response', (data) => { + console.log('ws response: ', JSON.stringify(data, null, 2)); + }); + wsClient.on('reconnect', ({ wsKey }) => { + console.log('ws automatically reconnecting.... ', wsKey); + }); + wsClient.on('reconnected', (data) => { + console.log('ws has reconnected ', data?.wsKey); + }); + + // subscribe to private endpoints + wsClient.subscribe(['position', 'execution', 'order', 'wallet']); +})(); diff --git a/package.json b/package.json index 4ee162d..04da11b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bybit-api", - "version": "2.2.0", + "version": "2.2.1", "description": "Node.js connector for Bybit's REST APIs and WebSockets, with TypeScript & integration tests.", "main": "lib/index.js", "types": "lib/index.d.ts", diff --git a/src/util/BaseRestClient.ts b/src/util/BaseRestClient.ts index 6e19922..c38f890 100644 --- a/src/util/BaseRestClient.ts +++ b/src/util/BaseRestClient.ts @@ -288,9 +288,10 @@ export default abstract class BaseRestClient { } const end = Date.now(); + const severTimeMs = serverTime * 1000; const avgDrift = (end - start) / 2; - return Math.ceil(serverTime - end + avgDrift); + return Math.ceil(severTimeMs - end + avgDrift); } catch (e) { console.error('Failed to fetch get time offset: ', e); return 0; diff --git a/src/websocket-client.ts b/src/websocket-client.ts index 31b77e9..d9dd4c6 100644 --- a/src/websocket-client.ts +++ b/src/websocket-client.ts @@ -6,7 +6,11 @@ import { LinearClient } from './linear-client'; import { DefaultLogger } from './logger'; import { KlineInterval } from './types/shared'; import { signMessage } from './util/node-support'; -import { serializeParams, isWsPong } from './util/requestUtils'; +import { + serializeParams, + isWsPong, + RestClientOptions, +} from './util/requestUtils'; import WsStore from './util/WsStore'; @@ -141,7 +145,7 @@ export interface WSClientConfigurableOptions { pongTimeout?: number; pingInterval?: number; reconnectTimeout?: number; - restOptions?: any; + restOptions?: RestClientOptions; requestOptions?: any; wsUrl?: string; }