v2.2.1: fix ws & rest authentication issues caused by time-sync mechanism
This commit is contained in:
6
examples/README.md
Normal file
6
examples/README.md
Normal file
@@ -0,0 +1,6 @@
|
||||
# Examples
|
||||
|
||||
These samples can be executed using `ts-node`:
|
||||
```
|
||||
ts-node ./examples/rest-spot-public.ts
|
||||
```
|
||||
55
examples/ws-private.ts
Normal file
55
examples/ws-private.ts
Normal file
@@ -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']);
|
||||
})();
|
||||
@@ -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",
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user