1.3.0-beta.1: introduce typescript, webpack, better overall browser support, deprecate assert calls

This commit is contained in:
tiagosiebler
2020-12-13 17:19:18 +00:00
parent 6f30c2d659
commit 0606525e96
18 changed files with 3351 additions and 660 deletions

3
src/index.ts Normal file
View File

@@ -0,0 +1,3 @@
export * from './rest-client';
export * from './websocket-client';
export * from './logger';

22
src/logger.ts Normal file
View File

@@ -0,0 +1,22 @@
export type LogParams = null | any;
export const DefaultLogger = {
silly: (...params: LogParams): void => {
console.log(params);
},
debug: (...params: LogParams): void => {
console.log(params);
},
notice: (...params: LogParams): void => {
console.log(params);
},
info: (...params: LogParams): void => {
console.info(params);
},
warning: (...params: LogParams): void => {
console.error(params);
},
error: (...params: LogParams): void => {
console.error(params);
}
};

531
src/rest-client.ts Normal file
View File

@@ -0,0 +1,531 @@
import { AxiosRequestConfig } from 'axios';
import { GenericAPIResponse, getBaseRESTInverseUrl, RestClientInverseOptions } from './util/requestUtils';
import RequestWrapper from './util/requestWrapper';
export class RestClient {
private requestWrapper: RequestWrapper;
/**
* @public Creates an instance of the inverse REST API client.
*
* @param {string} key - your API key
* @param {string} secret - your API secret
* @param {boolean} [useLivenet=false]
* @param {RestClientInverseOptions} [restInverseOptions={}] options to configure REST API connectivity
* @param {AxiosRequestConfig} [requestOptions={}] HTTP networking options for axios
*/
constructor(
key?: string | undefined,
secret?: string | undefined,
useLivenet?: boolean,
restInverseOptions: RestClientInverseOptions = {},
httpOptions: AxiosRequestConfig = {}
) {
this.requestWrapper = new RequestWrapper(
key,
secret,
getBaseRESTInverseUrl(useLivenet),
restInverseOptions,
httpOptions
);
return this;
}
/**
*
* Market Data Endpoints
*
*/
getOrderBook(params: {
symbol: string;
}): GenericAPIResponse {
return this.requestWrapper.get('v2/public/orderBook/L2', params);
}
getKline(params: {
symbol: string;
interval: string;
from: number;
limit?: number;
}): GenericAPIResponse {
return this.requestWrapper.get('v2/public/kline/list', params);
}
/**
* @deprecated use getTickers() instead
*/
getLatestInformation(params?: {
symbol?: string;
}): GenericAPIResponse {
return this.getTickers(params);
}
getTickers(params?: {
symbol?: string;
}): GenericAPIResponse {
return this.requestWrapper.get('v2/public/tickers', params);
}
/**
* @deprecated use getTrades() instead
*/
getPublicTradingRecords(params: {
symbol: string;
from?: number;
limit?: number;
}): GenericAPIResponse {
return this.getTrades(params);
}
getTrades(params: {
symbol: string;
from?: number;
limit?: number;
}): GenericAPIResponse {
return this.requestWrapper.get('v2/public/trading-records', params);
}
getSymbols(): GenericAPIResponse {
return this.requestWrapper.get('v2/public/symbols');
}
/**
* @deprecated use getLiquidations() instead
*/
getPublicLiquidations(params: {
symbol: string;
from?: number;
limit?: number;
start_time?: number;
end_time?: number;
}): GenericAPIResponse {
return this.getLiquidations(params);
}
getLiquidations(params: {
symbol: string;
from?: number;
limit?: number;
start_time?: number;
end_time?: number;
}): GenericAPIResponse {
return this.requestWrapper.get('v2/public/liq-records', params);
}
getMarkPriceKline(params: {
symbol: string;
interval: string;
from: number;
limit?: number;
}): GenericAPIResponse {
return this.requestWrapper.get('v2/public/mark-price-kline', params);
}
getOpenInterest(params: {
symbol: string;
period: string;
limit?: number;
}): GenericAPIResponse {
return this.requestWrapper.get('v2/public/open-interest', params);
}
getLatestBigDeal(params: {
symbol: string;
limit?: number;
}): GenericAPIResponse {
return this.requestWrapper.get('v2/public/big-deal', params);
}
getLongShortRatio(params: {
symbol: string;
period: string;
limit?: number;
}): GenericAPIResponse {
return this.requestWrapper.get('v2/public/account-ratio', params);
}
/**
*
* Account Data Endpoints
*
*/
placeActiveOrder(orderRequest: {
side: string;
symbol: string;
order_type: string;
qty: number;
price?: number;
time_in_force: string;
take_profit?: number;
stop_loss?: number;
reduce_only?: boolean;
close_on_trigger?: boolean;
order_link_id?: string;
}): GenericAPIResponse {
// if (orderRequest.order_type === 'Limit' && !orderRequest.price) {
// throw new Error('Price required for limit orders');
// }
return this.requestWrapper.post('v2/private/order/create', orderRequest);
}
getActiveOrderList(params: {
symbol: string;
order_status?: string;
direction?: string;
limit?: number;
cursor?: string;
}): GenericAPIResponse {
return this.requestWrapper.get('v2/private/order/list', params);
}
/**
* @deprecated use getActiveOrderList() instead
*/
getActiveOrder(params: {
order_id?: string;
order_link_id?: string;
symbol?: string;
order?: string;
page?: number;
limit?: number;
order_status?: string;
}): GenericAPIResponse {
return this.requestWrapper.get('open-api/order/list', params);
}
cancelActiveOrder(params: {
symbol: string;
order_id?: string;
order_link_id?: string;
}): GenericAPIResponse {
// if (!params.order_id && !params.order_link_id) {
// throw new Error('Parameter order_id OR order_link_id is required');
// }
return this.requestWrapper.post('v2/private/order/cancel', params);
}
cancelAllActiveOrders(params: {
symbol: string;
}): GenericAPIResponse {
return this.requestWrapper.post('v2/private/order/cancelAll', params);
}
replaceActiveOrder(params: {
order_id?: string;
order_link_id?: string;
symbol: string;
p_r_qty?: string;
p_r_price?: string;
}): GenericAPIResponse {
// if (!params.order_id && !params.order_link_id) {
// throw new Error('Parameter order_id OR order_link_id is required');
// }
return this.requestWrapper.post('v2/private/order/replace', params);
}
/**
* @deprecated use replaceActiveOrder()
*/
replaceActiveOrderOld(params: any): GenericAPIResponse {
// if (!params.order_id && !params.order_link_id) {
// throw new Error('Parameter order_id OR order_link_id is required');
// }
return this.requestWrapper.post('open-api/order/replace', params);
}
queryActiveOrder(params: {
order_id?: string;
order_link_id?: string;
symbol: string;
}): GenericAPIResponse {
// if (!params.order_id && !params.order_link_id) {
// throw new Error('Parameter order_id OR order_link_id is required');
// }
return this.requestWrapper.get('v2/private/order', params);
}
placeConditionalOrder(params: {
side: string;
symbol: string;
order_type: string;
qty: string;
price?: string;
base_price: string;
stop_px: string;
time_in_force: string;
trigger_by?: string;
close_on_trigger?: boolean;
order_link_id?: string;
}): GenericAPIResponse {
// if (params.order_type === 'Limit' && !params.price) {
// throw new Error('Parameter price is required for limit orders');
// }
return this.requestWrapper.post('v2/private/stop-order/create', params);
}
/**
* @deprecated use placeConditionalOrder
*/
placeConditionalOrderOld(params: any): GenericAPIResponse {
// if (params.order_type === 'Limit' && !params.price) {
// throw new Error('Parameter price is required for limit orders');
// }
return this.requestWrapper.post('open-api/stop-order/create', params);
}
getConditionalOrder(params: {
symbol: string;
stop_order_status?: string;
direction?: string;
limit?: number;
cursor?: string;
}): GenericAPIResponse {
return this.requestWrapper.get('v2/private/stop-order/list', params);
}
/**
* @deprecated use placeConditionalOrder
*/
getConditionalOrderOld(params: any): GenericAPIResponse {
return this.requestWrapper.get('open-api/stop-order/list', params);
}
cancelConditionalOrder(params: {
symbol: string;
stop_order_id?: string;
order_link_id?: string;
}): GenericAPIResponse {
// if (!params.stop_order_id && !params.order_link_id) {
// throw new Error('Parameter stop_order_id OR order_link_id is required');
// }
return this.requestWrapper.post('v2/private/stop-order/cancel', params);
}
/**
* @deprecated use cancelConditionalOrder
*/
cancelConditionalOrderOld(params: any): GenericAPIResponse {
// if (!params.stop_order_id && !params.order_link_id) {
// throw new Error('Parameter stop_order_id OR order_link_id is required');
// }
return this.requestWrapper.post('open-api/stop-order/cancel', params);
}
cancelAllConditionalOrders(params: {
symbol: string;
}): GenericAPIResponse {
return this.requestWrapper.post('v2/private/stop-order/cancelAll', params);
}
replaceConditionalOrder(params: {
stop_order_id?: string;
order_link_id?: string;
symbol: string;
p_r_qty?: number;
p_r_price?: string;
p_r_trigger_price?: string;
}): GenericAPIResponse {
// if (!params.stop_order_id && !params.order_link_id) {
// throw new Error('Parameter stop_order_id OR order_link_id is required');
// }
return this.requestWrapper.post('v2/private/stop-order/replace', params);
}
/**
* @deprecated use replaceConditionalOrder
*/
replaceConditionalOrderOld(params: any): GenericAPIResponse {
return this.requestWrapper.post('open-api/stop-order/replace', params);
}
queryConditionalOrder(params: {
symbol: string;
stop_order_id?: string;
order_link_id?: string;
}): GenericAPIResponse {
// if (!params.stop_order_id && !params.order_link_id) {
// throw new Error('Parameter stop_order_id OR order_link_id is required');
// }
return this.requestWrapper.get('v2/private/stop-order', params);
}
/**
* @deprecated use getPosition() instead
*/
getUserLeverage(): GenericAPIResponse {
return this.requestWrapper.get('user/leverage');
}
getPosition(params?: {
symbol?: string;
}): GenericAPIResponse {
return this.requestWrapper.get('v2/private/position/list', params);
}
/**
* @deprecated use getPosition() instead
*/
getPositions(): GenericAPIResponse {
return this.requestWrapper.get('position/list');
}
changePositionMargin(params: {
symbol: string;
margin: string;
}): GenericAPIResponse {
return this.requestWrapper.post('position/change-position-margin', params);
}
setTradingStop(params: {
symbol: string;
take_profit?: number;
stop_loss?: number;
tp_trigger_by?: string;
sl_trigger_by?: string;
new_trailing_active?: number;
}): GenericAPIResponse {
return this.requestWrapper.post('open-api/position/trading-stop', params);
}
setUserLeverage(params: {
symbol: string;
leverage: number;
}): GenericAPIResponse {
return this.requestWrapper.post('user/leverage/save', params);
}
/**
* @deprecated use setUserLeverage() instead
*/
changeUserLeverage(params: any): GenericAPIResponse {
return this.setUserLeverage(params);
}
getTradeRecords(params: {
order_id?: string;
symbol: string;
start_time?: number;
page?: number;
limit?: number;
order?: string;
}): GenericAPIResponse {
return this.requestWrapper.get('v2/private/execution/list', params);
}
getClosedPnl(params: {
symbol: string;
start_time?: number;
end_time?: number;
exec_type?: string;
page?: number;
limit?: number;
}): GenericAPIResponse {
return this.requestWrapper.get('v2/private/trade/closed-pnl/list', params);
}
getRiskLimitList(): GenericAPIResponse {
return this.requestWrapper.get('open-api/wallet/risk-limit/list');
}
setRiskLimit(params: {
symbol: string;
risk_id: string;
}): GenericAPIResponse {
return this.requestWrapper.post('open-api/wallet/risk-limit', params);
}
getLastFundingRate(params: {
symbol: string;
}): GenericAPIResponse {
return this.requestWrapper.get('open-api/funding/prev-funding-rate', params);
}
getMyLastFundingFee(params: {
symbol: string;
}): GenericAPIResponse {
return this.requestWrapper.get('open-api/funding/prev-funding', params);
}
getPredictedFunding(params: {
symbol: string;
}): GenericAPIResponse {
return this.requestWrapper.get('open-api/funding/predicted-funding', params);
}
getApiKeyInfo(): GenericAPIResponse {
return this.requestWrapper.get('open-api/api-key');
}
getLcpInfo(params: {
symbol: string;
}): GenericAPIResponse {
return this.requestWrapper.get('v2/private/account/lcp', params);
}
/**
*
* Wallet Data Endpoints
*
*/
getWalletBalance(params?: {
coin?: string;
}): GenericAPIResponse {
return this.requestWrapper.get('v2/private/wallet/balance', params);
}
getWalletFundRecords(params?: {
start_date?: string;
end_date?: string;
currency?: string;
coin?: string;
wallet_fund_type?: string;
page?: number;
limit?: number;
}): GenericAPIResponse {
return this.requestWrapper.get('open-api/wallet/fund/records', params);
}
getWithdrawRecords(params: {
start_date?: string;
end_date?: string;
coin?: string;
status?: string;
page?: number;
limit?: number;
}): GenericAPIResponse {
return this.requestWrapper.get('open-api/wallet/withdraw/list', params);
}
getAssetExchangeRecords(params?: {
limit?: number;
from?: number;
direction?: string;
}): GenericAPIResponse {
return this.requestWrapper.get('v2/private/exchange-order/list', params);
}
/**
*
* API Data Endpoints
*
*/
getServerTime(): GenericAPIResponse {
return this.requestWrapper.get('v2/public/time');
}
getApiAnnouncements(): GenericAPIResponse {
return this.requestWrapper.get('v2/public/announcement');
}
async getTimeOffset(): Promise<number> {
const start = Date.now();
return this.getServerTime().then(result => {
const end = Date.now();
return Math.ceil((result.time_now * 1000) - end + ((end - start) / 2));
});
}
};

59
src/util/requestUtils.ts Normal file
View File

@@ -0,0 +1,59 @@
import { createHmac } from 'crypto';
export interface RestClientInverseOptions {
// override the max size of the request window (in ms)
recv_window?: number;
// how often to sync time drift with bybit servers
sync_interval_ms?: number | string;
// Default: false. Disable above sync mechanism if true.
disable_time_sync?: boolean;
// Default: false. If true, we'll throw errors if any params are undefined
strict_param_validation?: boolean;
// Optionally override API protocol + domain
// e.g 'https://api.bytick.com'
baseUrl?: string;
// Default: true. whether to try and post-process request exceptions.
parse_exceptions?: boolean;
}
export type GenericAPIResponse = Promise<any>;
export function signMessage(message: string, secret: string): string {
return createHmac('sha256', secret)
.update(message)
.digest('hex');
};
export function serializeParams(params: object = {}, strict_validation = false): string {
return Object.keys(params)
.sort()
.map(key => {
const value = params[key];
if (strict_validation === true && typeof value === 'undefined') {
throw new Error('Failed to sign API request due to undefined parameter');
}
return `${key}=${value}`;
})
.join('&');
};
export function getBaseRESTInverseUrl(useLivenet?: boolean, restInverseOptions?: RestClientInverseOptions) {
const baseUrlsInverse = {
livenet: 'https://api.bybit.com',
testnet: 'https://api-testnet.bybit.com'
};
if (restInverseOptions?.baseUrl) {
return restInverseOptions.baseUrl;
}
if (useLivenet === true) {
return baseUrlsInverse.livenet;
}
return baseUrlsInverse.testnet;
}

193
src/util/requestWrapper.ts Normal file
View File

@@ -0,0 +1,193 @@
import axios, { AxiosRequestConfig, AxiosResponse, Method } from 'axios';
import { signMessage, serializeParams, RestClientInverseOptions, GenericAPIResponse } from './requestUtils';
export default class RequestUtil {
private timeOffset: number | null;
private syncTimePromise: null | Promise<any>;
private options: RestClientInverseOptions;
private baseUrl: string;
private globalRequestOptions: AxiosRequestConfig;
private key: string | undefined;
private secret: string | undefined;
constructor(
key: string | undefined,
secret: string | undefined,
baseUrl: string,
options: RestClientInverseOptions = {},
requestOptions: AxiosRequestConfig = {}
) {
this.timeOffset = null;
this.syncTimePromise = null;
this.options = {
recv_window: 5000,
// how often to sync time drift with bybit servers
sync_interval_ms: 3600000,
// if true, we'll throw errors if any params are undefined
strict_param_validation: false,
...options
};
this.globalRequestOptions = {
// in ms == 5 minutes by default
timeout: 1000 * 60 * 5,
// custom request options based on axios specs - see: https://github.com/axios/axios#request-config
...requestOptions,
headers: {
'x-referer': 'bybitapinode'
},
};
this.baseUrl = baseUrl;
if (key && !secret) {
throw new Error('API Key & Secret are both required for private enpoints')
}
if (this.options.disable_time_sync !== true) {
this.syncTime();
setInterval(this.syncTime.bind(this), +this.options.sync_interval_ms!);
}
this.key = key;
this.secret = secret;
}
// TODO: type check that endpoint never starts with forward slash??
get(endpoint: string, params?: any): GenericAPIResponse {
return this._call('GET', endpoint, params);
}
post(endpoint: string, params?: any): GenericAPIResponse {
return this._call('POST', endpoint, params);
}
/**
* @private Make a HTTP request to a specific endpoint. Private endpoints are automatically signed.
*/
async _call(method: Method, endpoint: string, params?: any): GenericAPIResponse {
const publicEndpoint = endpoint.startsWith('v2/public');
if (!publicEndpoint) {
if (!this.key || !this.secret) {
throw new Error('Private endpoints require api and private keys set');
}
if (this.timeOffset === null) {
await this.syncTime();
}
params = this.signRequest(params);
}
const options = {
...this.globalRequestOptions,
url: [this.baseUrl, endpoint].join('/'),
method: method,
json: true
};
if (method === 'GET') {
options.params = params;
} else {
options.data = params;
}
return axios(options).then(response => {
if (response.status == 200) {
return response.data;
}
throw response;
}).catch(this.parseException);
}
/**
* @private generic handler to parse request exceptions
*/
parseException(e: any): unknown {
if (this.options.parse_exceptions === false) {
throw e;
}
// Something happened in setting up the request that triggered an Error
if (!e.response) {
if (!e.request) {
throw e.message;
}
// request made but no response received
throw e;
}
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
const response: AxiosResponse = e.response;
throw {
code: response.status,
message: response.statusText,
body: response.data,
headers: response.headers,
requestOptions: this.options
};
}
/**
* @private sign request and set recv window
*/
signRequest(data: any): any {
const params = {
...data,
api_key: this.key,
timestamp: Date.now() + (this.timeOffset || 0)
};
// Optional, set to 5000 by default. Increase if timestamp/recv_window errors are seen.
if (this.options.recv_window && !params.recv_window) {
params.recv_window = this.options.recv_window;
}
if (this.key && this.secret) {
const serializedParams = serializeParams(params, this.options.strict_param_validation);
params.sign = signMessage(serializedParams, this.secret);
}
return params;
}
/**
* @private trigger time sync and store promise
*/
syncTime(): GenericAPIResponse {
if (this.options.disable_time_sync === true) {
return Promise.resolve(false);
}
if (this.syncTimePromise !== null) {
return this.syncTimePromise;
}
this.syncTimePromise = this.getTimeOffset().then(offset => {
this.timeOffset = offset;
this.syncTimePromise = null;
});
return this.syncTimePromise;
}
/**
* @deprecated move this somewhere else, because v2/public/time shouldn't be hardcoded here
*
* @returns {Promise<number>}
* @memberof RequestUtil
*/
async getTimeOffset(): Promise<number> {
const start = Date.now();
const result = await this.get('v2/public/time');
const end = Date.now();
return Math.ceil((result.time_now * 1000) - end + ((end - start) / 2));
}
};

265
src/websocket-client.ts Normal file
View File

@@ -0,0 +1,265 @@
import { EventEmitter } from 'events';
import { DefaultLogger } from './logger';
import { RestClient } from './rest-client';
import { signMessage, serializeParams } from './util/requestUtils';
// import WebSocket from 'ws';
import WebSocket from 'isomorphic-ws';
const wsUrls = {
livenet: 'wss://stream.bybit.com/realtime',
testnet: 'wss://stream-testnet.bybit.com/realtime'
};
const READY_STATE_INITIAL = 0;
const READY_STATE_CONNECTING = 1;
const READY_STATE_CONNECTED = 2;
const READY_STATE_CLOSING = 3;
const READY_STATE_RECONNECTING = 4;
export interface WebsocketClientOptions {
key?: string;
secret?: string;
livenet?: boolean;
pongTimeout?: number;
pingInterval?: number;
reconnectTimeout?: number;
restOptions?: any;
requestOptions?: any;
wsUrl?: string;
};
type Logger = typeof DefaultLogger;
export class WebsocketClient extends EventEmitter {
private logger: Logger;
private readyState: number;
private pingInterval?: number | undefined;
private pongTimeout?: number | undefined;
private client: RestClient;
private _subscriptions: Set<unknown>;
private ws: WebSocket;
private options: WebsocketClientOptions;
constructor(options: WebsocketClientOptions, logger?: Logger) {
super();
this.logger = logger || DefaultLogger;
this.readyState = READY_STATE_INITIAL;
this.pingInterval = undefined;
this.pongTimeout = undefined;
this.options = {
livenet: false,
pongTimeout: 1000,
pingInterval: 10000,
reconnectTimeout: 500,
...options
};
this.client = new RestClient(undefined, undefined, this.options.livenet, this.options.restOptions, this.options.requestOptions);
this._subscriptions = new Set();
this._connect();
}
subscribe(topics) {
if (!Array.isArray(topics)) topics = [topics];
topics.forEach(topic => this._subscriptions.add(topic));
// subscribe not necessary if not yet connected (will subscribe onOpen)
if (this.readyState === READY_STATE_CONNECTED) this._subscribe(topics);
}
unsubscribe(topics) {
if (!Array.isArray(topics)) topics = [topics];
topics.forEach(topic => this._subscriptions.delete(topic));
// unsubscribe not necessary if not yet connected
if (this.readyState === READY_STATE_CONNECTED) this._unsubscribe(topics);
}
close() {
this.logger.info('Closing connection', {category: 'bybit-ws'});
this.readyState = READY_STATE_CLOSING;
this._teardown();
this.ws && this.ws.close();
}
_getWsUrl() {
if (this.options.wsUrl) {
return this.options.wsUrl;
}
return wsUrls[this.options.livenet ? 'livenet' : 'testnet'];
}
async _connect() {
try {
if (this.readyState === READY_STATE_INITIAL) this.readyState = READY_STATE_CONNECTING;
const authParams = await this._authenticate();
const url = this._getWsUrl() + authParams;
const ws = new WebSocket(url);
// ws.onopen!(this._wsOpenHandler.bind(this));
// ws.onmessage!(this._wsMessageHandler.bind(this));
// ws.onerror!(this._wsOnErrorHandler.bind(this));
// ws.onclose!(this._wsCloseHandler.bind(this));
ws.onopen = this._wsOpenHandler.bind(this);
ws.onmessage = this._wsMessageHandler.bind(this);
ws.onerror = this._wsOnErrorHandler.bind(this);
ws.onclose = this._wsCloseHandler.bind(this);
this.ws = ws;
} catch (err) {
this.logger.error('Connection failed: ', err);
this._reconnect(this.options.reconnectTimeout);
}
}
async _authenticate() {
if (this.options.key && this.options.secret) {
this.logger.debug('Starting authenticated websocket client.', {category: 'bybit-ws'});
const timeOffset = await this.client.getTimeOffset();
const params: any = {
api_key: this.options.key,
expires: (Date.now() + timeOffset + 5000)
};
params.signature = signMessage('GET/realtime' + params.expires, this.options.secret);
return '?' + serializeParams(params);
} else if (this.options.key || this.options.secret) {
this.logger.warning('Could not authenticate websocket, either api key or private key missing.', { category: 'bybit-ws' });
} else {
this.logger.debug('Starting public only websocket client.', { category: 'bybit-ws' });
}
return '';
}
_reconnect(timeout) {
this._teardown();
if (this.readyState !== READY_STATE_CONNECTING) {
this.readyState = READY_STATE_RECONNECTING;
}
setTimeout(() => {
this.logger.info('Reconnecting to server', { category: 'bybit-ws' });
this._connect();
}, timeout);
}
_ping() {
clearTimeout(this.pongTimeout!);
delete this.pongTimeout;
this.logger.silly('Sending ping', { category: 'bybit-ws' });
this.ws.send(JSON.stringify({op: 'ping'}));
this.pongTimeout = setTimeout(() => {
this.logger.info('Pong timeout', { category: 'bybit-ws' });
this._teardown();
// this.ws.terminate();
// TODO: does this work?
this.ws.close();
}, this.options.pongTimeout);
}
_teardown() {
if (this.pingInterval) clearInterval(this.pingInterval);
if (this.pongTimeout) clearTimeout(this.pongTimeout);
this.pongTimeout = undefined;
this.pingInterval = undefined;
}
_wsOpenHandler() {
if (this.readyState === READY_STATE_CONNECTING) {
this.logger.info('Websocket connected', { category: 'bybit-ws', livenet: this.options.livenet });
this.emit('open');
} else if (this.readyState === READY_STATE_RECONNECTING) {
this.logger.info('Websocket reconnected', { category: 'bybit-ws', livenet: this.options.livenet });
this.emit('reconnected');
}
this.readyState = READY_STATE_CONNECTED;
this._subscribe([...this._subscriptions]);
this.pingInterval = setInterval(this._ping.bind(this), this.options.pingInterval);
}
_wsMessageHandler(message) {
const msg = JSON.parse(message && message.data || message);
if ('success' in msg) {
this._handleResponse(msg);
} else if (msg.topic) {
this._handleUpdate(msg);
} else {
this.logger.warning('Got unhandled ws message', msg);
}
}
_wsOnErrorHandler(err) {
this.logger.error('Websocket error', {category: 'bybit-ws', err});
if (this.readyState === READY_STATE_CONNECTED) this.emit('error', err);
}
_wsCloseHandler() {
this.logger.info('Websocket connection closed', {category: 'bybit-ws'});
if (this.readyState !== READY_STATE_CLOSING) {
this._reconnect(this.options.reconnectTimeout);
this.emit('reconnect');
} else {
this.readyState = READY_STATE_INITIAL;
this.emit('close');
}
}
_handleResponse(response) {
if (
response.request &&
response.request.op === 'ping' &&
response.ret_msg === 'pong' &&
response.success === true
) {
this.logger.silly('pong recieved', {category: 'bybit-ws'});
clearTimeout(this.pongTimeout);
} else {
this.emit('response', response);
}
}
_handleUpdate(message) {
this.emit('update', message);
}
_subscribe(topics) {
const msgStr = JSON.stringify({
op: 'subscribe',
'args': topics
});
this.ws.send(msgStr);
}
_unsubscribe(topics) {
const msgStr = JSON.stringify({
op: 'unsubscribe',
'args': topics
});
this.ws.send(msgStr);
}
};