-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathkraken.js
More file actions
160 lines (131 loc) · 4.53 KB
/
Copy pathkraken.js
File metadata and controls
160 lines (131 loc) · 4.53 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const got = require('got');
const qs = require('qs');
const {getMessageSignature} = require('./lib/signature')
// Public/Private method names
const methods = require('./config/methods.json');
// Default options
const defaults = require('./config/request.json');
// Send an API request
const rawRequest = async (url, headers, data, timeout) => {
// Set custom User-Agent string
headers['User-Agent'] = 'Kraken Javascript API Client';
const options = { headers, timeout };
Object.assign(options, {
method : 'POST',
body : qs.stringify(data),
});
const { body } = await got(url, options);
const response = JSON.parse(body);
if(response.error && response.error.length) {
const error = response.error
.filter((e) => e.startsWith('E'))
.map((e) => e.substr(1));
if(!error.length) {
throw new Error("Kraken API returned an unknown error");
}
throw new Error(error.join(', '));
}
return response;
};
/**
* KrakenClient connects to the Kraken.com API
* @param {String} key API Key
* @param {String} secret API Secret
* @param {String|Object} [options={}] Additional options. If a string is passed, will default to just setting `options.otp`.
* @param {String} [options.otp] Two-factor password (optional) (also, doesn't work)
* @param {Number} [options.timeout] Maximum timeout (in milliseconds) for all API-calls (passed to `request`)
*/
class KrakenClient {
constructor(key, secret, options) {
if(!key || !secret) throw new Error('[Error] null key or secret')
// Allow passing the OTP as the third argument for backwards compatibility
if(typeof options === 'string') options = { otp : options };
this.config = { ...defaults, key, secret, options};
}
/**
* This method makes a public or private API request.
* @param {String} method The API method (public or private)
* @param {Object} params Arguments to pass to the api call
* @param {Function} callback A callback function to be executed when the request is complete
* @return {Object} The request object
*/
api(method, params, callback) {
// Default params to empty object
if(typeof params === 'function') {
callback = params;
params = {};
}
if(methods.public.includes(method)) {
return this.publicMethod(method, params, callback);
}
else if(methods.private.includes(method)) {
return this.privateMethod(method, params, callback);
}
else {
throw new Error(method + ' is not a valid API method.');
}
}
/**
* This method makes a public API request.
* @param {String} method The API method (public or private)
* @param {Object} params Arguments to pass to the api call
* @param {Function} callback A callback function to be executed when the request is complete
* @return {Object} The request object
*/
publicMethod(method, params = {}, callback) {
// Default params to empty object
if(typeof params === 'function') {
callback = params;
params = {};
}
const path = '/' + this.config.version + '/public/' + method;
const url = this.config.url + path;
const response = rawRequest(url, {}, params, this.config.timeout);
if(typeof callback === 'function') {
response
.then((result) => callback(null, result))
.catch((error) => callback(error, null));
}
return response;
}
/**
* This method makes a private API request.
* @param {String} method The API method (public or private)
* @param {Object} params Arguments to pass to the api call
* @param {Function} callback A callback function to be executed when the request is complete
* @return {Object} The request object
*/
privateMethod(method, params = {}, callback) {
// Default params to empty object
if(typeof params === 'function') {
callback = params;
params = {};
}
const path = '/' + this.config.version + '/private/' + method;
const url = this.config.url + path;
if(!params.nonce) {
params.nonce = new Date() * 1000; // spoof microsecond
}
if(this.config.otp !== undefined) {
params.otp = this.config.otp;
}
const signature = getMessageSignature(
path,
params,
this.config.secret,
params.nonce
);
const headers = {
'API-Key' : this.config.key,
'API-Sign' : signature,
};
const response = rawRequest(url, headers, params, this.config.timeout);
if(typeof callback === 'function') {
response
.then((result) => callback(null, result))
.catch((error) => callback(error, null));
}
return response;
}
}
module.exports = KrakenClient;