Skip to content

Commit

Permalink
Merge pull request #15 from tiagosiebler/feat/axios
Browse files Browse the repository at this point in the history
Replace deprecated `request` with `axios`
  • Loading branch information
tiagosiebler authored Oct 8, 2020
2 parents 327007b + af6a6e6 commit df57640
Show file tree
Hide file tree
Showing 14 changed files with 1,111 additions and 455 deletions.
36 changes: 36 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v12.18.1
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# 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/orderbooks.svg)][1]
# 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]
[![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

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
Expand Down
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -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 changes: 11 additions & 0 deletions jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"target": "ES6",
"module": "commonjs"
},
"exclude": [
"node_modules",
"**/node_modules/*",
"coverage"
]
}
14 changes: 7 additions & 7 deletions lib/logger.js
Original file line number Diff line number Diff line change
@@ -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 changes: 0 additions & 138 deletions lib/request.js

This file was deleted.

10 changes: 5 additions & 5 deletions lib/rest-client.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -17,7 +17,7 @@ module.exports = class RestClient {
assert(params.qty, 'Parameter qty 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);
}
Expand Down Expand Up @@ -67,7 +67,7 @@ module.exports = class RestClient {
assert(params.base_price, 'Parameter base_price 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);
}
Expand Down Expand Up @@ -274,4 +274,4 @@ module.exports = class RestClient {
async getTimeOffset() {
return await this.request.getTimeOffset();
}
}
};
21 changes: 21 additions & 0 deletions lib/util/requestUtils.js
Original file line number Diff line number Diff line change
@@ -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('&');
}
};
Loading

0 comments on commit df57640

Please sign in to comment.