-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbitmex_rest.js
65 lines (54 loc) · 2.13 KB
/
bitmex_rest.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
53
54
55
56
57
58
59
60
61
62
63
64
65
const fetch = require('node-fetch');
const crypto = require('crypto');
const qs = require('qs');
const { logger } = require('./logger.js');
require('dotenv').config();
// Environment
const testnet = (process.env.TESTNET === 'true');
const apiKey = process.env.KEY;
const apiSecret = process.env.SECRET;
//
// Create and make a request to BitMEX REST API
//
function makeRequest(verb, endpoint, data = {}) {
if (!apiKey || !apiSecret) {
logger.log({ level: 'error', title: 'REST API', message: 'Missing API keys to make REST calls' });
return
}
logger.log({ level: 'verbose', title: 'REST API', message: 'Request made' });
const apiRoot = '/api/v1/';
const expires = Math.round(new Date().getTime() / 1000) + 60; // 1 min in the future
let query = '';
let postBody = '';
if (verb === 'GET')
query = '?' + qs.stringify(data);
else
// Pre-compute the reqBody so we can be sure that we're using *exactly* the same body in the request
// and in the signature. If you don't do this, you might get differently-sorted keys and blow the signature.
postBody = JSON.stringify(data);
const signature = crypto.createHmac('sha256', apiSecret)
.update(verb + apiRoot + endpoint + query + expires + postBody).digest('hex');
const headers = {
'content-type': 'application/json',
'accept': 'application/json',
// This example uses the 'expires' scheme. You can also use the 'nonce' scheme. See
// https://www.bitmex.com/app/apiKeysUsage for more details.
'api-expires': expires,
'api-key': apiKey,
'api-signature': signature,
};
const requestOptions = {
method: verb,
headers,
};
if (verb !== 'GET') requestOptions.body = postBody; // GET/HEAD requests can't have body
const url = `https://${testnet ? "testnet" : "www"}.bitmex.com` + apiRoot + endpoint + query;
return fetch(url, requestOptions).then(response => response.json()).then(
response => {
if ('error' in response) throw new Error(response.error.message);
return response;
},
err => logger.log({ level: 'error', title: 'REST API', message: `Error: ${err}` }),
);
}
module.exports = { makeRequest };