diff --git a/README.md b/README.md index 39c7acd..74550c8 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ -# bybit-api [![npm version](https://img.shields.io/npm/v/bybit-api.svg)][1] [![npm size](https://img.shields.io/bundlephobia/min/bybit-api.svg)][1] [![npm downloads](https://img.shields.io/npm/dt/bybit-api.svg)][1] +# bybit-api +[![npm version](https://img.shields.io/npm/v/bybit-api/latest)][1] [![npm size](https://img.shields.io/bundlephobia/min/bybit-api/latest)][1] [![npm downloads](https://img.shields.io/npm/dt/bybit-api)][1] +[![last commit](https://img.shields.io/github/last-commit/tiagosiebler/bybit-api)][1] [![CodeFactor](https://www.codefactor.io/repository/github/tiagosiebler/bybit-api/badge)](https://www.codefactor.io/repository/github/tiagosiebler/bybit-api) [1]: https://www.npmjs.com/package/bybit-api @@ -89,8 +91,7 @@ const ws = new WebsocketClient({key: API_KEY, secret: PRIVATE_KEY}, DefaultLogge ## Contributions & Thanks ### Donations #### pixtron -This library was started by @pixtron. If this library helps you to trade better on bybit, feel free to donate a coffee to @pixtron or create a bybit account using his [ref link](https://www.bybit.com/app/register?ref=j8q5l). - +This library was started by @pixtron. If this library helps you to trade better on bybit, feel free to donate a coffee to @pixtron: - BTC `1Fh1158pXXudfM6ZrPJJMR7Y5SgZUz4EdF` - ETH `0x21aEdeC53ab7593b77C9558942f0c9E78131e8d7` - LTC `LNdHSVtG6UWsriMYLJR3qLdfVNKwJ6GSLF` @@ -100,7 +101,7 @@ If you found this project interesting or useful, create accounts with my referra - [Bybit](https://www.bybit.com/en-US/register?affiliate_id=9410&language=en-US&group_id=0&group_type=1) - [Binance](https://www.binance.com/en/register?ref=20983262) -Or feed my coffee addiction using any of these: +Or buy me a coffee using any of these: - BTC: `1C6GWZL1XW3jrjpPTS863XtZiXL1aTK7Jk` - ETH (ERC20): `0xd773d8e6a50758e1ada699bb6c4f98bb4abf82da` diff --git a/lib/rest-client.js b/lib/rest-client.js index 8d5557a..a5d4d6e 100644 --- a/lib/rest-client.js +++ b/lib/rest-client.js @@ -4,8 +4,16 @@ const assert = require('assert'); const RequestWrapper = require('./util/requestWrapper'); module.exports = class RestClient { - - constructor(key, secret, livenet=false, options={}) { + /** + * @public Creates an instance of the inverse REST API client. + * + * @param {string} key - your API key + * @param {string} secret - your API secret + * @param {boolean} [livenet=false] + * @param {*} [options={}] options to configure REST API connectivity + * @param {*} [requestOptions={}] HTTP networking options for axios + */ + constructor(key, secret, livenet=false, options={}, requestOptions={}) { this.request = new RequestWrapper(...arguments); } diff --git a/lib/websocket-client.js b/lib/websocket-client.js index 36aeee8..51e64cc 100644 --- a/lib/websocket-client.js +++ b/lib/websocket-client.js @@ -4,7 +4,7 @@ const WebSocket = require('ws'); const defaultLogger = require('./logger'); const RestClient = require('./rest-client'); -const { signMessage } = require('./util/requestUtils'); +const { signMessage, serializeParams } = require('./util/requestUtils'); const wsUrls = { livenet: 'wss://stream.bybit.com/realtime', @@ -96,11 +96,8 @@ module.exports = class WebsocketClient extends EventEmitter { }; params.signature = signMessage('GET/realtime' + params.expires, this.options.secret); + return '?' + serializeParams(params); - return '?' + Object.keys(params) - .sort() - .map(key => `${key}=${params[key]}`) - .join('&'); } 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 { @@ -112,7 +109,9 @@ module.exports = class WebsocketClient extends EventEmitter { _reconnect(timeout) { 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(() => { this.logger.info('Reconnecting to server', {category: 'bybit-ws'}); @@ -159,7 +158,7 @@ module.exports = class WebsocketClient extends EventEmitter { } _wsMessageHandler(message) { - let msg = JSON.parse(message); + const msg = JSON.parse(message); if ('success' in msg) { this._handleResponse(msg); @@ -188,11 +187,14 @@ module.exports = class WebsocketClient extends EventEmitter { } _handleResponse(response) { - if (response.request && response.request.op === 'ping' && response.ret_msg === 'pong') { - if (response.success === true) { + 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); } diff --git a/package.json b/package.json index 09d72cb..8988bbd 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "bybit-api", - "version": "1.2.0", - "description": "A light node.js wrapper for the Bybit Cryptocurrency Derivative exchange API", + "version": "1.2.1", + "description": "A node.js wrapper for the Bybit Cryptocurrency Derivative exchange APIs", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" @@ -14,7 +14,13 @@ "bybit", "api", "websocket", - "rest" + "rest", + "rest api", + "inverse", + "nodejs", + "trading", + "cryptocurrency", + "bitcoin" ], "author": "Stefan Aebischer (https://pixtron.ch)", "contributors": [