feat: start work on axios integration, deprecating request.
This commit is contained in:
51
.eslintrc.js
Normal file
51
.eslintrc.js
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
module.exports = {
|
||||||
|
env: {
|
||||||
|
es6: true,
|
||||||
|
node: true,
|
||||||
|
'jest/globals': true
|
||||||
|
},
|
||||||
|
extends: ['eslint:recommended', 'plugin:jest/recommended'],
|
||||||
|
parserOptions: {
|
||||||
|
sourceType: 'module',
|
||||||
|
ecmaVersion: 9
|
||||||
|
},
|
||||||
|
|
||||||
|
plugins: ['jest', 'prettier'],
|
||||||
|
rules: {
|
||||||
|
// 'prettier/prettier': [
|
||||||
|
// 'error',
|
||||||
|
// {
|
||||||
|
// singleQuote: true,
|
||||||
|
// printWidth: 140,
|
||||||
|
// arrowParens: 'avoid'
|
||||||
|
// }
|
||||||
|
// ],
|
||||||
|
'jest/no-disabled-tests': 'warn',
|
||||||
|
'jest/no-focused-tests': 'error',
|
||||||
|
'jest/no-identical-title': 'error',
|
||||||
|
'jest/prefer-to-have-length': 'warn',
|
||||||
|
'jest/valid-expect': 'error',
|
||||||
|
'array-bracket-spacing': ['error', 'never'],
|
||||||
|
indent: ['warn', 2],
|
||||||
|
'linebreak-style': ['error', 'unix'],
|
||||||
|
'lines-between-class-members': ['warn', 'always'],
|
||||||
|
quotes: ['error', 'single'],
|
||||||
|
semi: ['error', 'always'],
|
||||||
|
'new-cap': 'off',
|
||||||
|
'no-console': 'off',
|
||||||
|
'no-debugger': 'off',
|
||||||
|
'no-mixed-spaces-and-tabs': 2,
|
||||||
|
'no-use-before-define': [2, 'nofunc'],
|
||||||
|
'no-unreachable': ['warn'],
|
||||||
|
'no-unused-vars': ['warn'],
|
||||||
|
'no-extra-parens': ['off'],
|
||||||
|
'no-mixed-operators': ['off'],
|
||||||
|
quotes: [2, 'single', 'avoid-escape'],
|
||||||
|
'block-scoped-var': 2,
|
||||||
|
'brace-style': [2, '1tbs', { allowSingleLine: true }],
|
||||||
|
'computed-property-spacing': [2, 'never'],
|
||||||
|
'keyword-spacing': 2,
|
||||||
|
'space-unary-ops': 2,
|
||||||
|
'max-len': ['warn', { 'code': 140 }]
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -1,16 +1,16 @@
|
|||||||
# bybit-api [][1] [][1] [][1]
|
# bybit-api [][1] [][1] [][1]
|
||||||
[](https://www.codefactor.io/repository/github/tiagosiebler/bybit-api)
|
[](https://www.codefactor.io/repository/github/tiagosiebler/bybit-api)
|
||||||
|
|
||||||
[1]: https://www.npmjs.com/package/bybit-api
|
[1]: https://www.npmjs.com/package/bybit-api
|
||||||
|
|
||||||
An unofficial node.js lowlevel wrapper for the Bybit Cryptocurrency Derivative exchange API. Forked from [@pxtrn/bybit-api](https://github.com/pixtron/bybit-api), due to low activity on fixes & improvements.
|
An light node.js wrapper for the Bybit Cryptocurrency Derivative exchange API. Forked & adapted from [@pxtrn/bybit-api](https://github.com/pixtron/bybit-api).
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
`npm install --save bybit-api`
|
`npm install --save bybit-api`
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
Create API credentials at bybit (obviously you need to be logged in):
|
Create API credentials at bybit (obviously you need to be logged in):
|
||||||
- [Livenet](https://bybit.com/app/user/api-management)
|
- [Livenet](https://bybit.com/app/user/api-management?affiliate_id=9410&language=en-US&group_id=0&group_type=1)
|
||||||
- [Testnet](https://testnet.bybit.com/app/user/api-management)
|
- [Testnet](https://testnet.bybit.com/app/user/api-management)
|
||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|||||||
2
index.js
2
index.js
@@ -6,4 +6,4 @@ module.exports = {
|
|||||||
RestClient,
|
RestClient,
|
||||||
WebsocketClient,
|
WebsocketClient,
|
||||||
DefaultLogger
|
DefaultLogger
|
||||||
}
|
};
|
||||||
|
|||||||
11
jsconfig.json
Normal file
11
jsconfig.json
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"target": "ES6",
|
||||||
|
"module": "commonjs"
|
||||||
|
},
|
||||||
|
"exclude": [
|
||||||
|
"node_modules",
|
||||||
|
"**/node_modules/*",
|
||||||
|
"coverage"
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
silly: function() {console.log(arguments)},
|
silly: function() {console.log(arguments);},
|
||||||
debug: function() {console.log(arguments)},
|
debug: function() {console.log(arguments);},
|
||||||
notice: function() {console.log(arguments)},
|
notice: function() {console.log(arguments);},
|
||||||
info: function() {console.info(arguments)},
|
info: function() {console.info(arguments);},
|
||||||
warning: function() {console.warn(arguments)},
|
warning: function() {console.warn(arguments);},
|
||||||
error: function() {console.error(arguments)},
|
error: function() {console.error(arguments);},
|
||||||
}
|
};
|
||||||
|
|||||||
@@ -19,9 +19,9 @@ module.exports = class Request {
|
|||||||
recv_window: 5000,
|
recv_window: 5000,
|
||||||
sync_interval_ms: 3600000,
|
sync_interval_ms: 3600000,
|
||||||
...options
|
...options
|
||||||
}
|
};
|
||||||
|
|
||||||
if(key) assert(secret, 'Secret is required for private enpoints');
|
if (key) assert(secret, 'Secret is required for private enpoints');
|
||||||
|
|
||||||
this._syncTime();
|
this._syncTime();
|
||||||
setInterval(this._syncTime.bind(this), parseInt(this.options.sync_interval_ms));
|
setInterval(this._syncTime.bind(this), parseInt(this.options.sync_interval_ms));
|
||||||
@@ -53,10 +53,10 @@ module.exports = class Request {
|
|||||||
async _call(method, endpoint, params) {
|
async _call(method, endpoint, params) {
|
||||||
const publicEndpoint = endpoint.startsWith('v2/public');
|
const publicEndpoint = endpoint.startsWith('v2/public');
|
||||||
|
|
||||||
if(!publicEndpoint) {
|
if (!publicEndpoint) {
|
||||||
if(!this.key || !this.secret) throw new Error('Private endpoints require api and private keys set');
|
if (!this.key || !this.secret) throw new Error('Private endpoints require api and private keys set');
|
||||||
|
|
||||||
if(this._timeOffset === null) await this._syncTime();
|
if (this._timeOffset === null) await this._syncTime();
|
||||||
|
|
||||||
params = this._signRequest(params);
|
params = this._signRequest(params);
|
||||||
}
|
}
|
||||||
@@ -67,12 +67,12 @@ module.exports = class Request {
|
|||||||
json: true
|
json: true
|
||||||
};
|
};
|
||||||
|
|
||||||
switch(method) {
|
switch (method) {
|
||||||
case 'GET':
|
case 'GET':
|
||||||
options.qs = params
|
options.qs = params;
|
||||||
break;
|
break;
|
||||||
case 'POST':
|
case 'POST':
|
||||||
options.body = params
|
options.body = params;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,11 +102,11 @@ module.exports = class Request {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Optional, set to 5000 by default. Increase if timestamp/recv_window errors are seen.
|
// Optional, set to 5000 by default. Increase if timestamp/recv_window errors are seen.
|
||||||
if(this.options.recv_window && !params.recv_window) {
|
if (this.options.recv_window && !params.recv_window) {
|
||||||
params.recv_window = this.options.recv_window;
|
params.recv_window = this.options.recv_window;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(this.key && this.secret) {
|
if (this.key && this.secret) {
|
||||||
params.sign = signMessage(this._serializeParams(params), this.secret);
|
params.sign = signMessage(this._serializeParams(params), this.secret);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,18 +121,18 @@ module.exports = class Request {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async _syncTime() {
|
async _syncTime() {
|
||||||
if(this._syncTimePromise !== null) return this._syncTimePromise;
|
if (this._syncTimePromise !== null) return this._syncTimePromise;
|
||||||
|
|
||||||
this._syncTimePromise = new Promise(async (resolve, reject) => {
|
this._syncTimePromise = new Promise(async (resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
this._timeOffset = await this.getTimeOffset();
|
this._timeOffset = await this.getTimeOffset();
|
||||||
this._syncTimePromise = null;
|
this._syncTimePromise = null;
|
||||||
resolve();
|
resolve();
|
||||||
} catch(err) {
|
} catch (err) {
|
||||||
reject(err);
|
reject(err);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return this._syncTimePromise;
|
return this._syncTimePromise;
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
139
lib/request-v2.js
Normal file
139
lib/request-v2.js
Normal file
@@ -0,0 +1,139 @@
|
|||||||
|
const assert = require('assert');
|
||||||
|
const request = require('request');
|
||||||
|
|
||||||
|
const {signMessage} = require('./utility');
|
||||||
|
|
||||||
|
const baseUrls = {
|
||||||
|
livenet: 'https://api.bybit.com',
|
||||||
|
testnet: 'https://api-testnet.bybit.com'
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = class Request {
|
||||||
|
|
||||||
|
constructor(key, secret, livenet=false, options={}) {
|
||||||
|
this.baseUrl = baseUrls[livenet === true ? 'livenet' : 'testnet'];
|
||||||
|
this._timeOffset = null;
|
||||||
|
this._syncTimePromise = null;
|
||||||
|
|
||||||
|
this.options = {
|
||||||
|
recv_window: 5000,
|
||||||
|
sync_interval_ms: 3600000,
|
||||||
|
...options
|
||||||
|
};
|
||||||
|
console.log('init new byaaabit api!!!!!!!!!');
|
||||||
|
|
||||||
|
if (key) assert(secret, 'Secret is required for private enpoints');
|
||||||
|
|
||||||
|
this._syncTime();
|
||||||
|
setInterval(this._syncTime.bind(this), parseInt(this.options.sync_interval_ms));
|
||||||
|
|
||||||
|
this.key = key;
|
||||||
|
this.secret = secret;
|
||||||
|
}
|
||||||
|
|
||||||
|
async get(endpoint, params) {
|
||||||
|
const result = await this._call('GET', endpoint, params);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
async post(endpoint, params) {
|
||||||
|
const result = await this._call('POST', endpoint, params);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
async getTimeOffset() {
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
async _call(method, endpoint, params) {
|
||||||
|
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 = {
|
||||||
|
url: [this.baseUrl, endpoint].join('/'),
|
||||||
|
method: method,
|
||||||
|
json: true
|
||||||
|
};
|
||||||
|
|
||||||
|
switch (method) {
|
||||||
|
case 'GET':
|
||||||
|
options.qs = params;
|
||||||
|
break;
|
||||||
|
case 'POST':
|
||||||
|
options.body = params;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
request(options, function callback(error, response, body) {
|
||||||
|
if (error) {
|
||||||
|
return reject(error);
|
||||||
|
}
|
||||||
|
if (response.statusCode == 200) {
|
||||||
|
return resolve(body);
|
||||||
|
}
|
||||||
|
return reject({
|
||||||
|
code: response.statusCode,
|
||||||
|
message: response.statusMessage,
|
||||||
|
body: response.body,
|
||||||
|
requestOptions: options
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
_signRequest(data) {
|
||||||
|
const params = {
|
||||||
|
...data,
|
||||||
|
api_key: this.key,
|
||||||
|
timestamp: Date.now() + this._timeOffset
|
||||||
|
};
|
||||||
|
|
||||||
|
// 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) {
|
||||||
|
params.sign = signMessage(this._serializeParams(params), this.secret);
|
||||||
|
}
|
||||||
|
|
||||||
|
return params;
|
||||||
|
}
|
||||||
|
|
||||||
|
_serializeParams(params) {
|
||||||
|
return Object.keys(params)
|
||||||
|
.sort()
|
||||||
|
.map(key => `${key}=${params[key]}`)
|
||||||
|
.join('&');
|
||||||
|
}
|
||||||
|
|
||||||
|
async _syncTime() {
|
||||||
|
if (this._syncTimePromise !== null) return this._syncTimePromise;
|
||||||
|
|
||||||
|
this._syncTimePromise = new Promise(async (resolve, reject) => {
|
||||||
|
try {
|
||||||
|
this._timeOffset = await this.getTimeOffset();
|
||||||
|
this._syncTimePromise = null;
|
||||||
|
resolve();
|
||||||
|
} catch (err) {
|
||||||
|
reject(err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return this._syncTimePromise;
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
|
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
|
|
||||||
const Request = require('./request.js');
|
const Request = require('./request-v2');
|
||||||
|
|
||||||
module.exports = class RestClient {
|
module.exports = class RestClient {
|
||||||
|
|
||||||
@@ -17,7 +17,7 @@ module.exports = class RestClient {
|
|||||||
assert(params.qty, 'Parameter qty is required');
|
assert(params.qty, 'Parameter qty is required');
|
||||||
assert(params.time_in_force, 'Parameter time_in_force is required');
|
assert(params.time_in_force, 'Parameter time_in_force is required');
|
||||||
|
|
||||||
if(params.order_type === 'Limit') assert(params.price, 'Parameter price is required for limit orders');
|
if (params.order_type === 'Limit') assert(params.price, 'Parameter price is required for limit orders');
|
||||||
|
|
||||||
return await this.request.post('v2/private/order/create', params);
|
return await this.request.post('v2/private/order/create', params);
|
||||||
}
|
}
|
||||||
@@ -67,7 +67,7 @@ module.exports = class RestClient {
|
|||||||
assert(params.base_price, 'Parameter base_price is required');
|
assert(params.base_price, 'Parameter base_price is required');
|
||||||
assert(params.stop_px, 'Parameter stop_px is required');
|
assert(params.stop_px, 'Parameter stop_px is required');
|
||||||
|
|
||||||
if(params.order_type === 'Limit') assert(params.price, 'Parameter price is required for limit orders');
|
if (params.order_type === 'Limit') assert(params.price, 'Parameter price is required for limit orders');
|
||||||
|
|
||||||
return await this.request.post('open-api/stop-order/create', params);
|
return await this.request.post('open-api/stop-order/create', params);
|
||||||
}
|
}
|
||||||
@@ -274,4 +274,4 @@ module.exports = class RestClient {
|
|||||||
async getTimeOffset() {
|
async getTimeOffset() {
|
||||||
return await this.request.getTimeOffset();
|
return await this.request.getTimeOffset();
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|||||||
@@ -6,4 +6,4 @@ module.exports = {
|
|||||||
.update(message)
|
.update(message)
|
||||||
.digest('hex');
|
.digest('hex');
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|||||||
@@ -2,9 +2,9 @@ const {EventEmitter} = require('events');
|
|||||||
|
|
||||||
const WebSocket = require('ws');
|
const WebSocket = require('ws');
|
||||||
|
|
||||||
const defaultLogger = require('./logger.js');
|
const defaultLogger = require('./logger');
|
||||||
const RestClient = require('./rest-client.js');
|
const RestClient = require('./rest-client');
|
||||||
const {signMessage} = require('./utility.js');
|
const { signMessage } = require('./utility');
|
||||||
|
|
||||||
const wsUrls = {
|
const wsUrls = {
|
||||||
livenet: 'wss://stream.bybit.com/realtime',
|
livenet: 'wss://stream.bybit.com/realtime',
|
||||||
@@ -33,7 +33,7 @@ module.exports = class WebsocketClient extends EventEmitter {
|
|||||||
pingInterval: 10000,
|
pingInterval: 10000,
|
||||||
reconnectTimeout: 500,
|
reconnectTimeout: 500,
|
||||||
...options
|
...options
|
||||||
}
|
};
|
||||||
|
|
||||||
this.client = new RestClient(null, null, this.options.livenet);
|
this.client = new RestClient(null, null, this.options.livenet);
|
||||||
this._subscriptions = new Set();
|
this._subscriptions = new Set();
|
||||||
@@ -42,20 +42,20 @@ module.exports = class WebsocketClient extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
subscribe(topics) {
|
subscribe(topics) {
|
||||||
if(!Array.isArray(topics)) topics = [topics];
|
if (!Array.isArray(topics)) topics = [topics];
|
||||||
topics.forEach(topic => this._subscriptions.add(topic));
|
topics.forEach(topic => this._subscriptions.add(topic));
|
||||||
|
|
||||||
// subscribe not necessary if not yet connected (will subscribe onOpen)
|
// subscribe not necessary if not yet connected (will subscribe onOpen)
|
||||||
if(this.readyState === READY_STATE_CONNECTED) this._subscribe(topics);
|
if (this.readyState === READY_STATE_CONNECTED) this._subscribe(topics);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsubscribe(topics) {
|
unsubscribe(topics) {
|
||||||
if(!Array.isArray(topics)) topics = [topics];
|
if (!Array.isArray(topics)) topics = [topics];
|
||||||
|
|
||||||
topics.forEach(topic => this._subscriptions.delete(topic));
|
topics.forEach(topic => this._subscriptions.delete(topic));
|
||||||
|
|
||||||
// unsubscribe not necessary if not yet connected
|
// unsubscribe not necessary if not yet connected
|
||||||
if(this.readyState === READY_STATE_CONNECTED) this._unsubscribe(topics);
|
if (this.readyState === READY_STATE_CONNECTED) this._unsubscribe(topics);
|
||||||
}
|
}
|
||||||
|
|
||||||
close() {
|
close() {
|
||||||
@@ -67,7 +67,7 @@ module.exports = class WebsocketClient extends EventEmitter {
|
|||||||
|
|
||||||
async _connect() {
|
async _connect() {
|
||||||
try {
|
try {
|
||||||
if(this.readyState === READY_STATE_INITIAL) this.readyState = READY_STATE_CONNECTING;
|
if (this.readyState === READY_STATE_INITIAL) this.readyState = READY_STATE_CONNECTING;
|
||||||
|
|
||||||
const authParams = await this._authenticate();
|
const authParams = await this._authenticate();
|
||||||
const url = wsUrls[this.options.livenet ? 'livenet' : 'testnet'] + authParams;
|
const url = wsUrls[this.options.livenet ? 'livenet' : 'testnet'] + authParams;
|
||||||
@@ -78,14 +78,14 @@ module.exports = class WebsocketClient extends EventEmitter {
|
|||||||
this.ws.on('message', this._wsMessageHandler.bind(this));
|
this.ws.on('message', this._wsMessageHandler.bind(this));
|
||||||
this.ws.on('error', this._wsOnErrorHandler.bind(this));
|
this.ws.on('error', this._wsOnErrorHandler.bind(this));
|
||||||
this.ws.on('close', this._wsCloseHandler.bind(this));
|
this.ws.on('close', this._wsCloseHandler.bind(this));
|
||||||
} catch(err) {
|
} catch (err) {
|
||||||
this.logger.error('Connection failed', err);
|
this.logger.error('Connection failed', err);
|
||||||
this._reconnect(this.options.reconnectTimeout);
|
this._reconnect(this.options.reconnectTimeout);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async _authenticate() {
|
async _authenticate() {
|
||||||
if(this.options.key && this.options.secret) {
|
if (this.options.key && this.options.secret) {
|
||||||
this.logger.debug('Starting authenticated websocket client.', {category: 'bybit-ws'});
|
this.logger.debug('Starting authenticated websocket client.', {category: 'bybit-ws'});
|
||||||
|
|
||||||
const timeOffset = await this.client.getTimeOffset();
|
const timeOffset = await this.client.getTimeOffset();
|
||||||
@@ -101,7 +101,7 @@ module.exports = class WebsocketClient extends EventEmitter {
|
|||||||
.sort()
|
.sort()
|
||||||
.map(key => `${key}=${params[key]}`)
|
.map(key => `${key}=${params[key]}`)
|
||||||
.join('&');
|
.join('&');
|
||||||
} else if(this.options.key || this.options.secret) {
|
} 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'});
|
this.logger.warning('Could not authenticate websocket, either api key or private key missing.', {category: 'bybit-ws'});
|
||||||
} else {
|
} else {
|
||||||
this.logger.debug('Starting public only websocket client.', {category: 'bybit-ws'});
|
this.logger.debug('Starting public only websocket client.', {category: 'bybit-ws'});
|
||||||
@@ -112,7 +112,7 @@ module.exports = class WebsocketClient extends EventEmitter {
|
|||||||
|
|
||||||
_reconnect(timeout) {
|
_reconnect(timeout) {
|
||||||
this._teardown();
|
this._teardown();
|
||||||
if(this.readyState !== READY_STATE_CONNECTING) this.readyState = READY_STATE_RECONNECTING;
|
if (this.readyState !== READY_STATE_CONNECTING) this.readyState = READY_STATE_RECONNECTING;
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
this.logger.info('Reconnecting to server', {category: 'bybit-ws'});
|
this.logger.info('Reconnecting to server', {category: 'bybit-ws'});
|
||||||
@@ -136,18 +136,18 @@ module.exports = class WebsocketClient extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_teardown() {
|
_teardown() {
|
||||||
if(this.pingInterval) clearInterval(this.pingInterval);
|
if (this.pingInterval) clearInterval(this.pingInterval);
|
||||||
if(this.pongTimeout) clearTimeout(this.pongTimeout);
|
if (this.pongTimeout) clearTimeout(this.pongTimeout);
|
||||||
|
|
||||||
this.pongTimeout = null;
|
this.pongTimeout = null;
|
||||||
this.pingInterval = null;
|
this.pingInterval = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
_wsOpenHandler() {
|
_wsOpenHandler() {
|
||||||
if(this.readyState === READY_STATE_CONNECTING) {
|
if (this.readyState === READY_STATE_CONNECTING) {
|
||||||
this.logger.info('Websocket connected', {category: 'bybit-ws', livenet: this.options.livenet});
|
this.logger.info('Websocket connected', {category: 'bybit-ws', livenet: this.options.livenet});
|
||||||
this.emit('open');
|
this.emit('open');
|
||||||
} else if(this.readyState === READY_STATE_RECONNECTING) {
|
} else if (this.readyState === READY_STATE_RECONNECTING) {
|
||||||
this.logger.info('Websocket reconnected', {category: 'bybit-ws', livenet: this.options.livenet});
|
this.logger.info('Websocket reconnected', {category: 'bybit-ws', livenet: this.options.livenet});
|
||||||
this.emit('reconnected');
|
this.emit('reconnected');
|
||||||
}
|
}
|
||||||
@@ -161,9 +161,9 @@ module.exports = class WebsocketClient extends EventEmitter {
|
|||||||
_wsMessageHandler(message) {
|
_wsMessageHandler(message) {
|
||||||
let msg = JSON.parse(message);
|
let msg = JSON.parse(message);
|
||||||
|
|
||||||
if('success' in msg) {
|
if ('success' in msg) {
|
||||||
this._handleResponse(msg);
|
this._handleResponse(msg);
|
||||||
} else if(msg.topic) {
|
} else if (msg.topic) {
|
||||||
this._handleUpdate(msg);
|
this._handleUpdate(msg);
|
||||||
} else {
|
} else {
|
||||||
this.logger.warning('Got unhandled ws message', msg);
|
this.logger.warning('Got unhandled ws message', msg);
|
||||||
@@ -172,13 +172,13 @@ module.exports = class WebsocketClient extends EventEmitter {
|
|||||||
|
|
||||||
_wsOnErrorHandler(err) {
|
_wsOnErrorHandler(err) {
|
||||||
this.logger.error('Websocket error', {category: 'bybit-ws', err});
|
this.logger.error('Websocket error', {category: 'bybit-ws', err});
|
||||||
if(this.readyState === READY_STATE_CONNECTED) this.emit('error', err);
|
if (this.readyState === READY_STATE_CONNECTED) this.emit('error', err);
|
||||||
}
|
}
|
||||||
|
|
||||||
_wsCloseHandler() {
|
_wsCloseHandler() {
|
||||||
this.logger.info('Websocket connection closed', {category: 'bybit-ws'});
|
this.logger.info('Websocket connection closed', {category: 'bybit-ws'});
|
||||||
|
|
||||||
if(this.readyState !== READY_STATE_CLOSING) {
|
if (this.readyState !== READY_STATE_CLOSING) {
|
||||||
this._reconnect(this.options.reconnectTimeout);
|
this._reconnect(this.options.reconnectTimeout);
|
||||||
this.emit('reconnect');
|
this.emit('reconnect');
|
||||||
} else {
|
} else {
|
||||||
@@ -188,8 +188,8 @@ module.exports = class WebsocketClient extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_handleResponse(response) {
|
_handleResponse(response) {
|
||||||
if(response.request && response.request.op === 'ping' && response.ret_msg === 'pong') {
|
if (response.request && response.request.op === 'ping' && response.ret_msg === 'pong') {
|
||||||
if(response.success === true) {
|
if (response.success === true) {
|
||||||
this.logger.silly('pong recieved', {category: 'bybit-ws'});
|
this.logger.silly('pong recieved', {category: 'bybit-ws'});
|
||||||
clearTimeout(this.pongTimeout);
|
clearTimeout(this.pongTimeout);
|
||||||
}
|
}
|
||||||
@@ -219,4 +219,4 @@ module.exports = class WebsocketClient extends EventEmitter {
|
|||||||
|
|
||||||
this.ws.send(msgStr);
|
this.ws.send(msgStr);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|||||||
5225
package-lock.json
generated
5225
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "bybit-api",
|
"name": "bybit-api",
|
||||||
"version": "1.1.9",
|
"version": "1.1.9",
|
||||||
"description": "An unofficial node.js lowlevel wrapper for the Bybit Cryptocurrency Derivative exchange API",
|
"description": "A light node.js wrapper for the Bybit Cryptocurrency Derivative exchange API",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
@@ -26,7 +26,14 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/tiagosiebler/bybit-api#readme",
|
"homepage": "https://github.com/tiagosiebler/bybit-api#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"axios": "^0.20.0",
|
||||||
"request": "^2.88.0",
|
"request": "^2.88.0",
|
||||||
"ws": "^7.1.2"
|
"ws": "^7.1.2"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"eslint": "^7.10.0",
|
||||||
|
"eslint-plugin-jest": "^24.0.2",
|
||||||
|
"eslint-plugin-prettier": "^3.1.4",
|
||||||
|
"jest": "^26.4.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user