-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathokcoin.js
More file actions
263 lines (237 loc) · 9.07 KB
/
okcoin.js
File metadata and controls
263 lines (237 loc) · 9.07 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
'use strict';
var request = require('requestretry');
var md5 = require('MD5');
/**
* OKCoin connects to the OKCoin.cn API
* @param {String} api_key
* @param {String} secret API Secret
*/
function OKCoin(api_key, secret) {
var self = this;
var config = {
url: 'https://www.okcoin.cn/api',
version: 'v1',
api_key: api_key,
secret: secret,
timeoutMS: 18000
};
/**
* Public methods supported
*/
function ticker(callback) {
var path = '/' + config.version + '/ticker.do';
return publicMethod(path, callback);
}
function depth(callback) {
var path = '/' + config.version + '/depth.do';
return publicMethod(path, callback);
}
function trades(callback) {
var path = '/' + config.version + '/trades.do';
return publicMethod(path, callback);
}
/**
* Private methods supported
* For information on the parameters, check OKCoin website https://www.okcoin.cn/about/rest_api.do
*/
function userinfo(callback) {
var path = '/' + config.version + '/userinfo.do';
var params = {};
return privateMethod(path, params, callback);
}
function order_info(symbol, order_id, callback) {
var path = '/' + config.version + '/order_info.do';
var params = {};
params.symbol = symbol;
params.order_id = order_id;
return privateMethod(path, params, callback);
}
function cancel_order (symbol, order_id, callback) {
var path = '/' + config.version + '/cancel_order.do';
var params = {};
params.symbol = symbol;
params.order_id = order_id;
return privateMethod(path, params, callback);
}
function trade(symbol, type, price, amount, callback) {
var path = '/' + config.version + '/trade.do';
var params = {};
if (amount) params.amount = amount;
if (price) params.price = price;
params.symbol = symbol;
params.type = type;
return privateMethod(path, params, callback);
}
function withdraw(symbol, chargefee, trade_pwd, withdraw_address, withdraw_amount, callback) {
var path = '/' + config.version + '/withdraw.do';
var params = {};
params.chargefee = chargefee;
params.symbol = symbol;
params.trade_pwd = trade_pwd;
params.withdraw_address = withdraw_address;
params.withdraw_amount = withdraw_amount;
return privateMethod(path, params, callback);
}
function account_records(symbol, type, current_page, page_length, callback) {
var path = '/' + config.version + '/account_records.do';
var params = {};
params.symbol = symbol;
params.type = type;
params.current_page = current_page;
params.page_length = page_length;
return privateMethod(path, params, callback);
}
/**
* This method makes a public API request.
* @param {String} path The path to the API method
* @param {Function} callback A callback function to be executed when the request is complete
* @return {Object} The request object
*/
function publicMethod(path, callback) {
var params = null;
var url = config.url + path;
return okcoinRequest(url, 'GET', params, callback);
}
/**
* This method makes a private API request.
* @param {String} path The path to the API method
* @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
*/
function privateMethod(path, params, callback) {
var url = config.url + path;
params.api_key = config.api_key;
params.sign = getMessageSignature(params);
return okcoinRequest(url, 'POST', params, callback);
}
/**
* This method returns a signature for a request as a md5-encoded uppercase string
* @param {Object} params The object to encode
* @return {String} The request signature
*/
function getMessageSignature(params) {
var sign = md5(stringifyToOKCoinFormat(params) + '&secret_key='+ config.secret).toUpperCase();
return sign;
}
/**
* This method returns the parameters as an alphabetically sorted string
* @param {Object} params The object to encode
* @return {String} The request signature
*/
function stringifyToOKCoinFormat(obj) {
var arr = [],
i,
formattedObject = '';
for (i in obj) {
if (obj.hasOwnProperty(i)) {
arr.push(i);
}
}
arr.sort();
for (i = 0; i < arr.length; i++) {
if (i != 0) {
formattedObject += '&';
}
formattedObject += arr[i] + '=' + obj[arr[i]];
}
return formattedObject;
}
/**
* This method sends the actual HTTP request
* @param {String} url The URL to make the request
* @param {String} requestType POST or GET
* @param {Object} params POST body
* @param {Function} callback A callback function to call when the request is complete
* @return {Object} The request object
*/
function okcoinRequest(url, requestType, params, callback) {
var options = {
url: url,
method: requestType,
form: params,
timeout: config.timeoutMS,
maxAttempts: 3,
retryDelay: 1000, // (default) wait for 5s before trying again
retryStrategy: request.RetryStrategies.HTTPOrNetworkError // (default) retry on 5xx or network errors
};
var req = request(options, function(error, response, body) {
if(typeof callback === 'function') {
var data;
if(error) {
callback.call(self, new Error('Error in server response: ' + JSON.stringify(error)), null);
return;
}
try {
data = JSON.parse(body);
}
catch(e) {
callback.call(self, new Error('Could not understand response from server: ' + body), null);
return;
}
if(data.error_code) {
callback.call(self, error_code_meaning(data.error_code), null);
}
else {
callback.call(self, null, data);
}
}
});
return req;
}
/**
* This method return the OKCoin error information
* @param {Integer} error_code OKCoin error code
* @return {String} Error
*/
function error_code_meaning(error_code) {
var codes = { 10000 : 'Required parameter can not be null',
10001 : 'Requests are too frequent',
10002 : 'System Error',
10003 : 'Restricted list request, please try again later',
10004 : 'IP restriction',
10005 : 'Key does not exist',
10006 : 'User does not exist',
10007 : 'Signatures do not match',
10008 : 'Illegal parameter',
10009 : 'Order does not exist',
10010 : 'Insufficient balance',
10011 : 'Order is less than minimum trade amount',
10012 : 'Unsupported symbol (not btc_cny or ltc_cny)',
10013 : 'This interface only accepts https requests',
10014 : 'Order price must be between 0 and 1,000,000',
10015 : 'Order price differs from current market price too much',
10016 : 'Insufficient coins balance',
10017 : 'API authorization error',
10026 : 'Loan (including reserved loan) and margin cannot be withdrawn',
10027 : 'Cannot withdraw within 24 hrs of authentication information modification',
10028 : 'Withdrawal amount exceeds daily limit',
10029 : 'Account has unpaid loan, please cancel/pay off the loan before withdraw',
10031 : 'Deposits can only be withdrawn after 6 confirmations',
10032 : 'Please enabled phone/google authenticator',
10033 : 'Fee higher than maximum network transaction fee',
10034 : 'Fee lower than minimum network transaction fee',
10035 : 'Insufficient BTC/LTC',
10036 : 'Withdrawal amount too low',
10037 : 'Trade password not set',
10040 : 'Withdrawal cancellation fails',
10041 : 'Withdrawal address not approved',
10042 : 'Admin password error',
10100 : 'User account frozen',
10216 : 'Non-available API',
503 : 'Too many requests (Http)'};
if (!codes[error_code]) {
return 'OKCoin error code :' + error_code +' is not yet supported by the API';
}
return( codes[error_code] );
}
self.ticker = ticker;
self.trades = trades;
self.depth = depth;
self.userinfo = userinfo;
self.trade = trade;
self.order_info = order_info;
self.withdraw = withdraw;
self.account_records = account_records;
}
module.exports = OKCoin;