-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbitmex.js
52 lines (41 loc) · 1.48 KB
/
bitmex.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const assert = require('assert');
const { createHmac } = require('crypto');
const querystring = require('querystring');
const request = require('superagent');
const BITMEX_BASE_URL = 'https://www.bitmex.com';
const createBitmexClient = (apiKey, apiSecret) => {
const bitmexRequest = async (verb, path, query, payload) => {
const queryStringified = query ? `?${querystring.stringify(query)}` : '';
const pathWithQuery = `${path}${queryStringified}`;
const payloadString = payload ? JSON.stringify(payload) : '';
const expires = (+new Date() + 1000 * 60).toString();
const signatureInput = [verb, pathWithQuery, expires, payloadString].join(
''
);
const signature = createHmac('sha256', apiSecret)
.update(signatureInput)
.digest('hex');
const requestForVerb = request[verb.toLowerCase()];
const promise = requestForVerb(`${BITMEX_BASE_URL}${pathWithQuery}`)
.set('api-expires', expires)
.set('api-key', apiKey)
.set('api-signature', signature)
.set('user-agent', 'Helix Capital - https://helix.capital');
let response;
if (payloadString.length) {
response = await promise
.set('content-type', 'application/json')
.send(payloadString);
} else {
response = await promise;
}
const { body } = response;
const { error } = body;
if (error) {
throw new Error(error.message);
}
return body;
};
return bitmexRequest;
};
module.exports = createBitmexClient;