Merge pull request #19 from tiagosiebler/feat/requestOpts

Feat/request opts
This commit is contained in:
Tiago
2020-10-08 22:15:57 +01:00
committed by GitHub
4 changed files with 36 additions and 19 deletions

View File

@@ -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) [![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 [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 ## Contributions & Thanks
### Donations ### Donations
#### pixtron #### 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` - BTC `1Fh1158pXXudfM6ZrPJJMR7Y5SgZUz4EdF`
- ETH `0x21aEdeC53ab7593b77C9558942f0c9E78131e8d7` - ETH `0x21aEdeC53ab7593b77C9558942f0c9E78131e8d7`
- LTC `LNdHSVtG6UWsriMYLJR3qLdfVNKwJ6GSLF` - 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) - [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) - [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` - BTC: `1C6GWZL1XW3jrjpPTS863XtZiXL1aTK7Jk`
- ETH (ERC20): `0xd773d8e6a50758e1ada699bb6c4f98bb4abf82da` - ETH (ERC20): `0xd773d8e6a50758e1ada699bb6c4f98bb4abf82da`

View File

@@ -4,8 +4,16 @@ const assert = require('assert');
const RequestWrapper = require('./util/requestWrapper'); const RequestWrapper = require('./util/requestWrapper');
module.exports = class RestClient { 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); this.request = new RequestWrapper(...arguments);
} }

View File

@@ -4,7 +4,7 @@ const WebSocket = require('ws');
const defaultLogger = require('./logger'); const defaultLogger = require('./logger');
const RestClient = require('./rest-client'); const RestClient = require('./rest-client');
const { signMessage } = require('./util/requestUtils'); const { signMessage, serializeParams } = require('./util/requestUtils');
const wsUrls = { const wsUrls = {
livenet: 'wss://stream.bybit.com/realtime', 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); 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) { } 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 {
@@ -112,7 +109,9 @@ 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'});
@@ -159,7 +158,7 @@ module.exports = class WebsocketClient extends EventEmitter {
} }
_wsMessageHandler(message) { _wsMessageHandler(message) {
let msg = JSON.parse(message); const msg = JSON.parse(message);
if ('success' in msg) { if ('success' in msg) {
this._handleResponse(msg); this._handleResponse(msg);
@@ -188,11 +187,14 @@ module.exports = class WebsocketClient extends EventEmitter {
} }
_handleResponse(response) { _handleResponse(response) {
if (response.request && response.request.op === 'ping' && response.ret_msg === 'pong') { if (
if (response.success === true) { response.request &&
response.request.op === 'ping' &&
response.ret_msg === 'pong' &&
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);
}
} else { } else {
this.emit('response', response); this.emit('response', response);
} }

View File

@@ -1,7 +1,7 @@
{ {
"name": "bybit-api", "name": "bybit-api",
"version": "1.2.0", "version": "1.2.1",
"description": "A light node.js wrapper for the Bybit Cryptocurrency Derivative exchange API", "description": "A node.js wrapper for the Bybit Cryptocurrency Derivative exchange APIs",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
@@ -14,7 +14,13 @@
"bybit", "bybit",
"api", "api",
"websocket", "websocket",
"rest" "rest",
"rest api",
"inverse",
"nodejs",
"trading",
"cryptocurrency",
"bitcoin"
], ],
"author": "Stefan Aebischer <os@pixtron.ch> (https://pixtron.ch)", "author": "Stefan Aebischer <os@pixtron.ch> (https://pixtron.ch)",
"contributors": [ "contributors": [