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)
|
||||
|
||||
[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
|
||||
`npm install --save bybit-api`
|
||||
|
||||
## Usage
|
||||
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)
|
||||
|
||||
## Documentation
|
||||
|
||||
8
index.js
8
index.js
@@ -1,9 +1,9 @@
|
||||
const RestClient = require('./lib/rest-client.js');
|
||||
const WebsocketClient = require('./lib/websocket-client.js');
|
||||
const DefaultLogger = require('./lib/logger.js');
|
||||
const RestClient = require('./lib/rest-client');
|
||||
const WebsocketClient = require('./lib/websocket-client');
|
||||
const DefaultLogger = require('./lib/logger');
|
||||
|
||||
module.exports = {
|
||||
RestClient,
|
||||
WebsocketClient,
|
||||
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 = {
|
||||
silly: function() {console.log(arguments)},
|
||||
debug: function() {console.log(arguments)},
|
||||
notice: function() {console.log(arguments)},
|
||||
info: function() {console.info(arguments)},
|
||||
warning: function() {console.warn(arguments)},
|
||||
error: function() {console.error(arguments)},
|
||||
}
|
||||
silly: function() {console.log(arguments);},
|
||||
debug: function() {console.log(arguments);},
|
||||
notice: function() {console.log(arguments);},
|
||||
info: function() {console.info(arguments);},
|
||||
warning: function() {console.warn(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 Request = require('./request.js');
|
||||
const RequestWrapper = require('./util/requestWrapper');
|
||||
|
||||
module.exports = class RestClient {
|
||||
|
||||
constructor(key, secret, livenet=false, options={}) {
|
||||
this.request = new Request(...arguments);
|
||||
this.request = new RequestWrapper(...arguments);
|
||||
}
|
||||
|
||||
async placeActiveOrder(params) {
|
||||
@@ -274,4 +274,4 @@ module.exports = class RestClient {
|
||||
async 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 defaultLogger = require('./logger.js');
|
||||
const RestClient = require('./rest-client.js');
|
||||
const {signMessage} = require('./utility.js');
|
||||
const defaultLogger = require('./logger');
|
||||
const RestClient = require('./rest-client');
|
||||
const { signMessage } = require('./util/requestUtils');
|
||||
|
||||
const wsUrls = {
|
||||
livenet: 'wss://stream.bybit.com/realtime',
|
||||
@@ -33,7 +33,7 @@ module.exports = class WebsocketClient extends EventEmitter {
|
||||
pingInterval: 10000,
|
||||
reconnectTimeout: 500,
|
||||
...options
|
||||
}
|
||||
};
|
||||
|
||||
this.client = new RestClient(null, null, this.options.livenet);
|
||||
this._subscriptions = new Set();
|
||||
@@ -62,7 +62,7 @@ module.exports = class WebsocketClient extends EventEmitter {
|
||||
this.logger.info('Closing connection', {category: 'bybit-ws'});
|
||||
this.readyState = READY_STATE_CLOSING;
|
||||
this._teardown();
|
||||
this.ws.close();
|
||||
this.ws && this.ws.close();
|
||||
}
|
||||
|
||||
async _connect() {
|
||||
@@ -219,4 +219,4 @@ module.exports = class WebsocketClient extends EventEmitter {
|
||||
|
||||
this.ws.send(msgStr);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
1098
package-lock.json
generated
1098
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",
|
||||
"version": "1.1.9",
|
||||
"description": "An unofficial node.js lowlevel wrapper for the Bybit Cryptocurrency Derivative exchange API",
|
||||
"version": "1.2.0",
|
||||
"description": "A light node.js wrapper for the Bybit Cryptocurrency Derivative exchange API",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
@@ -26,7 +26,10 @@
|
||||
},
|
||||
"homepage": "https://github.com/tiagosiebler/bybit-api#readme",
|
||||
"dependencies": {
|
||||
"request": "^2.88.0",
|
||||
"ws": "^7.1.2"
|
||||
"axios": "^0.20.0",
|
||||
"ws": "^7.3.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"eslint": "^7.10.0"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user