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)
|
||||
|
||||
[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
|
||||
|
||||
2
index.js
2
index.js
@@ -6,4 +6,4 @@ 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);},
|
||||
};
|
||||
|
||||
@@ -19,7 +19,7 @@ module.exports = class Request {
|
||||
recv_window: 5000,
|
||||
sync_interval_ms: 3600000,
|
||||
...options
|
||||
}
|
||||
};
|
||||
|
||||
if (key) assert(secret, 'Secret is required for private enpoints');
|
||||
|
||||
@@ -69,10 +69,10 @@ module.exports = class Request {
|
||||
|
||||
switch (method) {
|
||||
case 'GET':
|
||||
options.qs = params
|
||||
options.qs = params;
|
||||
break;
|
||||
case 'POST':
|
||||
options.body = params
|
||||
options.body = params;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -135,4 +135,4 @@ module.exports = class Request {
|
||||
|
||||
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 Request = require('./request.js');
|
||||
const Request = require('./request-v2');
|
||||
|
||||
module.exports = class RestClient {
|
||||
|
||||
@@ -274,4 +274,4 @@ module.exports = class RestClient {
|
||||
async getTimeOffset() {
|
||||
return await this.request.getTimeOffset();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -6,4 +6,4 @@ module.exports = {
|
||||
.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('./utility');
|
||||
|
||||
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();
|
||||
@@ -219,4 +219,4 @@ module.exports = class WebsocketClient extends EventEmitter {
|
||||
|
||||
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",
|
||||
"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",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
@@ -26,7 +26,14 @@
|
||||
},
|
||||
"homepage": "https://github.com/tiagosiebler/bybit-api#readme",
|
||||
"dependencies": {
|
||||
"axios": "^0.20.0",
|
||||
"request": "^2.88.0",
|
||||
"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