Merge pull request #15 from tiagosiebler/feat/axios
Replace deprecated `request` with `axios`
This commit is contained in:
36
.eslintrc.js
Normal file
36
.eslintrc.js
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
module.exports = {
|
||||||
|
env: {
|
||||||
|
es6: true,
|
||||||
|
node: true,
|
||||||
|
},
|
||||||
|
extends: ['eslint:recommended'],
|
||||||
|
parserOptions: {
|
||||||
|
sourceType: 'module',
|
||||||
|
ecmaVersion: 9
|
||||||
|
},
|
||||||
|
|
||||||
|
plugins: [],
|
||||||
|
rules: {
|
||||||
|
'array-bracket-spacing': ['error', 'never'],
|
||||||
|
indent: ['warn', 2],
|
||||||
|
'linebreak-style': ['error', 'unix'],
|
||||||
|
'lines-between-class-members': ['warn', 'always'],
|
||||||
|
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
|
||||||
|
|||||||
8
index.js
8
index.js
@@ -1,9 +1,9 @@
|
|||||||
const RestClient = require('./lib/rest-client.js');
|
const RestClient = require('./lib/rest-client');
|
||||||
const WebsocketClient = require('./lib/websocket-client.js');
|
const WebsocketClient = require('./lib/websocket-client');
|
||||||
const DefaultLogger = require('./lib/logger.js');
|
const DefaultLogger = require('./lib/logger');
|
||||||
|
|
||||||
module.exports = {
|
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);},
|
||||||
}
|
};
|
||||||
|
|||||||
138
lib/request.js
138
lib/request.js
@@ -1,138 +0,0 @@
|
|||||||
const assert = require('assert');
|
|
||||||
const request = require('request');
|
|
||||||
|
|
||||||
const {signMessage} = require('./utility.js');
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
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,12 +1,12 @@
|
|||||||
|
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
|
|
||||||
const Request = require('./request.js');
|
const RequestWrapper = require('./util/requestWrapper');
|
||||||
|
|
||||||
module.exports = class RestClient {
|
module.exports = class RestClient {
|
||||||
|
|
||||||
constructor(key, secret, livenet=false, options={}) {
|
constructor(key, secret, livenet=false, options={}) {
|
||||||
this.request = new Request(...arguments);
|
this.request = new RequestWrapper(...arguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
async placeActiveOrder(params) {
|
async placeActiveOrder(params) {
|
||||||
@@ -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();
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|||||||
21
lib/util/requestUtils.js
Normal file
21
lib/util/requestUtils.js
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
const { createHmac } = require('crypto');
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
signMessage(message, secret) {
|
||||||
|
return createHmac('sha256', secret)
|
||||||
|
.update(message)
|
||||||
|
.digest('hex');
|
||||||
|
},
|
||||||
|
serializeParams(params, strict_validation) {
|
||||||
|
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('&');
|
||||||
|
}
|
||||||
|
};
|
||||||
167
lib/util/requestWrapper.js
Normal file
167
lib/util/requestWrapper.js
Normal file
@@ -0,0 +1,167 @@
|
|||||||
|
const assert = require('assert');
|
||||||
|
const axios = require('axios');
|
||||||
|
|
||||||
|
const { signMessage, serializeParams } = require('./requestUtils');
|
||||||
|
|
||||||
|
const baseUrls = {
|
||||||
|
livenet: 'https://api.bybit.com',
|
||||||
|
testnet: 'https://api-testnet.bybit.com'
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = class RequestUtil {
|
||||||
|
constructor(key, secret, livenet=false, options={}, requestOptions={}) {
|
||||||
|
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.baseUrl = baseUrls[livenet === true ? 'livenet' : 'testnet'];
|
||||||
|
if (options.baseUrl) {
|
||||||
|
this.baseUrl = options.baseUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
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: {
|
||||||
|
'referer': 'bybitapinode'
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
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 = {
|
||||||
|
...this.globalRequestOptions,
|
||||||
|
url: [this.baseUrl, endpoint].join('/'),
|
||||||
|
method: method,
|
||||||
|
json: true
|
||||||
|
};
|
||||||
|
|
||||||
|
switch (method) {
|
||||||
|
case 'GET':
|
||||||
|
options.params = params;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
options.data = params;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return axios(options).then(response => {
|
||||||
|
if (response.status == 200) {
|
||||||
|
return response.data;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw {
|
||||||
|
code: response.status,
|
||||||
|
message: response.statusText,
|
||||||
|
body: response.data,
|
||||||
|
requestOptions: options
|
||||||
|
};
|
||||||
|
})
|
||||||
|
.catch(e => {
|
||||||
|
if (!e.response) {
|
||||||
|
// Something happened in setting up the request that triggered an Error
|
||||||
|
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
|
||||||
|
throw {
|
||||||
|
code: e.response.statusCode,
|
||||||
|
message: e.response.message,
|
||||||
|
body: e.response.body,
|
||||||
|
requestOptions: options,
|
||||||
|
headers: e.response.headers
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
_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) {
|
||||||
|
const serializedParams = serializeParams(params, this.options.strict_param_validation);
|
||||||
|
params.sign = signMessage(serializedParams, this.secret);
|
||||||
|
}
|
||||||
|
|
||||||
|
return params;
|
||||||
|
}
|
||||||
|
|
||||||
|
_syncTime() {
|
||||||
|
if (this._syncTimePromise !== null) {
|
||||||
|
return this._syncTimePromise;
|
||||||
|
}
|
||||||
|
|
||||||
|
this._syncTimePromise = this.getTimeOffset().then(offset => {
|
||||||
|
this._timeOffset = offset;
|
||||||
|
this._syncTimePromise = null;
|
||||||
|
});
|
||||||
|
|
||||||
|
return this._syncTimePromise;
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
const {createHmac} = require('crypto');
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
signMessage(message, secret) {
|
|
||||||
return createHmac('sha256', secret)
|
|
||||||
.update(message)
|
|
||||||
.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('./util/requestUtils');
|
||||||
|
|
||||||
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,32 +42,32 @@ 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() {
|
||||||
this.logger.info('Closing connection', {category: 'bybit-ws'});
|
this.logger.info('Closing connection', {category: 'bybit-ws'});
|
||||||
this.readyState = READY_STATE_CLOSING;
|
this.readyState = READY_STATE_CLOSING;
|
||||||
this._teardown();
|
this._teardown();
|
||||||
this.ws.close();
|
this.ws && this.ws.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|||||||
1094
package-lock.json
generated
1094
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
11
package.json
11
package.json
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "bybit-api",
|
"name": "bybit-api",
|
||||||
"version": "1.1.9",
|
"version": "1.2.0",
|
||||||
"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,10 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/tiagosiebler/bybit-api#readme",
|
"homepage": "https://github.com/tiagosiebler/bybit-api#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"request": "^2.88.0",
|
"axios": "^0.20.0",
|
||||||
"ws": "^7.1.2"
|
"ws": "^7.3.1"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"eslint": "^7.10.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user