-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapiHelper.js
More file actions
114 lines (105 loc) · 2.24 KB
/
Copy pathapiHelper.js
File metadata and controls
114 lines (105 loc) · 2.24 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const nonce = require('nonce')();
const crypto = require('crypto');
const urlLib = require('url');
module.exports.create = (apiKey, secret, debug = false) => {
function createQuery(command, {
currencyPair,
start,
end,
buy,
rate,
amount,
sell,
cancelOrder,
orderNumber,
period,
fillOrKill,
immediateOrCancel,
postOnly,
currency,
account,
}) {
const query = {};
query.command = command;
if (currencyPair) {
query.currencyPair = currencyPair;
}
if (start) {
query.start = start;
}
if (end) {
query.end = end;
}
if (buy) {
query.buy = buy;
}
if (sell) {
query.sell = sell;
}
if (rate) {
query.rate = rate;
}
if (amount) {
query.amount = amount;
}
if (cancelOrder) {
query.cancelOrder = cancelOrder;
}
if (orderNumber) {
query.orderNumber = orderNumber;
}
if (period) {
query.period = period;
}
if (immediateOrCancel) {
query.immediateOrCancel = immediateOrCancel;
}
if (fillOrKill) {
query.fillOrKill = fillOrKill;
}
if (postOnly) {
query.postOnly = postOnly;
}
if (currency) {
query.currency = currency;
}
if (account) {
query.account = account;
}
query.nonce = nonce();
return query;
}
function createQueryString(command, opts) {
const query = createQuery(command, opts);
const queryString = urlLib.format({ query }).substring(1);
debug && console.log('querystring', queryString);
return queryString;
}
function createHeader(queryString) {
return {
Key: apiKey,
Sign: crypto.createHmac('sha512', secret).update(queryString).digest('hex'),
'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent': 'request node',
};
}
function createOptions({ url, queryString, method = 'post' }) {
const options = {
url,
method,
body: queryString,
};
if (method === 'post') {
options.headers = createHeader(queryString);
}
debug && console.log('requesting with options: ', options);
return options;
}
return {
createQuery,
createQueryString,
createHeader,
createOptions,
};
}
;