-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpoloniex.js
47 lines (37 loc) · 1.14 KB
/
poloniex.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
const assert = require('assert');
const { createHmac } = require('crypto');
const request = require('superagent');
const createPoloniexRequest = (apiKey, apiSecret) => {
assert(apiKey);
assert(apiSecret);
const apiSecretAsBuffer = new Buffer(apiSecret);
const poloniexPost = (path, fields) => {
const fieldsWithNonce = {
nonce: +new Date(),
...fields,
};
const requestBody = Object.keys(fieldsWithNonce)
.map(key => `${key}=${fieldsWithNonce[key]}`)
.join('&');
const signature = createHmac('sha512', apiSecretAsBuffer)
.update(requestBody)
.digest('hex');
return request
.post(`https://poloniex.com${path}`)
.set('Content-Type', 'application/x-www-form-urlencoded')
.set('Content-Length', requestBody.length)
.set('Key', apiKey)
.set('Sign', signature)
.send(requestBody)
.then(response => {
const { body } = response;
const { error } = body;
if (error) {
throw new Error(`Poloniex error: ${error}`);
}
return body;
});
};
return poloniexPost;
};
module.exports = createPoloniexRequest;