-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathyunbi-module.js
430 lines (363 loc) · 14.1 KB
/
yunbi-module.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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/**
* Created by kim on 2/25/15.
*/
//Create module for Yunbi exchange to call the APIs
/*
Trading and deposit are free until 04/01/2015 So get fee will be 0 until then
*/
module.exports = (function() {
'use strict';
// Module dependencies
var crypto = require('crypto'),
request = require('request'),
nonce = require('nonce')();
var Gkey=''
var Gsign='' //global var, FIXME should use one from constructor
var GSecret = '';
// Constants
var version = '0.0.6',
API_URL = 'https://yunbi.com:443//api/v2',
HASH_URL = '/api/v2',
USER_AGENT = 'yunbi.js ' + version;
var errorMsg = "Missing Params";
//USER_AGENT = 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0'
// Helper methods
function joinCurrencies(currencyA, currencyB){
// If only one arg, then return the first
if (typeof currencyB !== 'string'){
return currencyA;
}
return currencyA.toLowerCase() + currencyB.toLowerCase();
}
function sortParameters(a, b){return 0;
// Sort `nonce` parameter last, and the rest alphabetically
return a === 'nonce' || a > b ? 1 : -1;
}
function cleanUpParam(param){
for (var key in param){
if(param[key] == null)
delete param[key];
}
}
// Constructor
function Yunbi(key, secret){
Gkey = key;
GSecret = secret;
// Generate headers signed by this user's key and secret.
// The secret is encapsulated and never exposed
this._getPrivateHeaders = function(parameters,link){
var paramString, signature;
var url = "POST|"+HASH_URL+link+"|"
if (!key || !secret){
throw 'Yunbi: Error. API key and secret required';
}
// Sort parameters alphabetically and convert to `arg1=foo&arg2=bar`
paramString = Object.keys(parameters).sort(parameters).map(function(param){
return encodeURIComponent(param) + '=' + encodeURIComponent(parameters[param]);
}).join('&');
var signature = crypto.createHmac('SHA256', secret).update(url+paramString).digest('hex');
parameters.signature = signature
signature = crypto.createHmac('sha256', secret).update(paramString).digest('hex');
//console.log("SIGNATURE " +signature)
Gkey = key
Gsign = signature
GSecret = secret
return {
Key: key,
Sign: signature
};
};
}
// Currently, this fails with `Error: CERT_UNTRUSTED`
// Yunbi.STRICT_SSL can be set to `false` to avoid this. Use with caution.
// Will be removed in future, once this is resolved.
//Yunbi.STRICT_SSL = false;
// Customisable user agent string
Yunbi.USER_AGENT = USER_AGENT;
// Prototype
Yunbi.prototype = {
constructor: Yunbi,
// Make an API request
_request: function(options, callback){
request(options, function(err, response, body) {
// Empty response
if (!err && (typeof body === 'undefined' || body === null)){
err = 'Empty response';
}
callback(err, body);
});
return this;
},
// Make a public API request
_public: function(link, parameters, callback){
var options;
if (typeof parameters === 'function'){
callback = parameters;
parameters = {};
}
parameters || (parameters = {});
options = {
method: 'GET',
url: API_URL+link,
qs: parameters
};
return this._request(options, callback);
},
// Make a private API request POST
_privatePost: function(link, parameters, callback){
var options;
if (typeof parameters === 'function'){
callback = parameters;
parameters = {};
}
parameters || (parameters = {});
parameters.tonce = nonce()/100;
parameters.access_key = Gkey
var paramString = Object.keys(parameters).sort(parameters).map(function(param){
return encodeURIComponent(param) + '=' + encodeURIComponent(parameters[param]);
}).join('&');
// var signature = (crypto.createHmac('sha256', secret).update(url+paramString).digest('hex')).toString();
// parameters.signature = signature
//// console.log(signature + " tonce " + parameters.tonce)
options = {
method: "POST",
url: API_URL+link,
form: parameters,
headers: this._getPrivateHeaders(parameters,link)
};
options.headers['User-Agent'] = "Yunbi API Client/0.0.1"
return this._request(options, callback);
},
/////
// Make a private API request GET
_privateGet: function(link,callback){
var options;
var url = "GET|"+HASH_URL+link+"|"
var parameters= {}
parameters.access_key = Gkey
parameters.tonce = parseInt(nonce()/100 );
var paramString = Object.keys(parameters).sort(parameters).map(function(param){
return encodeURIComponent(param) + '=' + encodeURIComponent(parameters[param]);
}).join('&');
var signature = crypto.createHmac('sha256', GSecret).update(url+paramString).digest('hex');
parameters.signature=signature
paramString = paramString +"&"+encodeURIComponent('signature') + "=" + encodeURIComponent(signature)
options = {
method: 'GET',
url: API_URL+link+"?"+paramString
};
return this._request(options, callback);
},
/////
// PUBLIC METHODS
getTicker: function(A,B,callback){
if(!A || !B)
return callback (errorMsg, null);
else {
var parameters = {
market: joinCurrencies(A, B)
};
var url = '/tickers/' + joinCurrencies(A, B) + '.json'
return this._public(url, parameters, callback);
}
},
getAllTickers : function(callback){
return this._public("/tickers.json",{},callback);
},
getDepth: function(currencyA,currencyB,mLimit,callback) {
if (!currencyA || !currencyB)
return callback(errorMsg, null);
else {
var parameters = {
market: joinCurrencies(currencyA, currencyB),
limit: mLimit ? mLimit : 300
};
return this._public('/depth.json', parameters, callback);
}
},
getOrderBook: function(currencyA, currencyB,askLimit, bidLimit, callback){
if(!currencyA || !currencyB)
return callback(errorMsg,null);
else {
var parameters = {
market: joinCurrencies(currencyA, currencyB),
asks_limit: askLimit ? askLimit : 20,
bids_limit: bidLimit ? bidLimit : 20
};
return this._public('/order_book.json', parameters, callback);
}
},
getMarkets : function(callback){
return this._public("/markets.json",{},callback);
},
getTimeStamp : function(callback){
return this._public("/timestamp.json",{},callback);
},
getK : function(currencyA,currencyB,Limit, Period,Timestamp,callback){
if(!currencyA || !currencyB)
return callback(errorMsg,null);
else {
var param = {
market: joinCurrencies(currencyA, currencyB),
limit: Limit ? Limit : null,
period: Period ? Period : null,
timestamp: Timestamp ? Timestamp : null
}
cleanUpParam(param);
return this._public("/k.json", param, callback);
}
},
KPendingTrades : function (currencyA,currencyB, tradeId, Limit,Period,Timestamp,callback){
if(!currencyA || !currencyB || !tradeId)
return callback(errorMsg,null);
else {
var param = {
market: joinCurrencies(currencyA, currencyB),
trade_id: tradeId,
period: Period ? Period : null,
timestamp: Timestamp ? Timestamp : null
}
cleanUpParam(param);
return this._public("/k_with_pending_trades.json", param, callback);
}
},
//// PRIVATE GET
getMember : function(callback){
var url = "/members/me.json";
return this._privateGet(url,{},callback);
},
getAllDeposits : function(InCurrency,InLimit, InState,callback){
if(!InCurrency)
return callback(errorMsg,null)
else {
var param = {
currency: InCurrency,
limit: InLimit ? InLimit : 100,
state: InState ? InState : "wait"
}
return this._privateGet("/deposits.json", param, callback);
}
},
getDeposit : function(transaction, callback){
if(!transaction)
return callback(errorMsg,null)
else {
var param = {
txid: transaction
}
return this._privateGet("/deposit.json", param, callback);
}
},
getDepositAddress : function(InCurrency, callback){
if(!InCurrency){
return callback(errorMsg,null)
}
else {
var param = {
currency: InCurrency
}
return this._privateGet("/deposit_address.json", param, callback);
}
},
getAllOrders : function(currencyA, currencyB, mState, mLimit, mPage, mOrderBy, callback){
if(!currencyA || !currencyB ){
return callback(errorMsg,null);
}
var param = {
state : mState ? mState : "wait",
limit : mLimit ? mLimit : 100,
page : mPage ? mPage : 0 ,
order_by : mOrderBy? mOrderBy : 'asc',
market : joinCurrencies(currencyA,currencyB)
}
return this._privateGet("/orders.json",param,callback);
},
getOrder : function(orderId,callback){
if(!orderId)
return callback(errorMsg,null);
var param = { id : orderId}
return this._privateGet("/orders.json",param, callback);
},
myBalances: function(callback){
return this._privateGet('/members/me.json',callback);
},
getRecentTrade : function(currencyA, currencyB, mLimit, Timestamp, From, To, Orderby,callback){
if(!currencyA || !currencyB){
return callback(errorMsg,null);
}
var param = {
market : joinCurrencies(currencyA,currencyB),
limit : mLimit ? mLimit : 50,
timestamp : Timestamp ? Timestamp : null,
from : From ? From : null,
to : To ? To : null,
order_by : Orderby ? Orderby : "desc"
}
cleanUpParam(param);
return this._privateGet("/trades.json",param,callback);
},
getMyTrades : function(currencyA, currencyB, mLimit, Timestamp, From, To, Orderby,callback){
if(!currencyA || !currencyB){
return callback(errorMsg,null);
}
var param = {
market : joinCurrencies(currencyA,currencyB),
limit : mLimit ? mLimit : 50,
timestamp : Timestamp ? Timestamp : null,
from : From ? From : null,
to : To ? To : null,
order_by : Orderby ? Orderby : "desc"
}
cleanUpParam(param)
return this._privateGet("/trades/my.json",param,callback);
},
/////PRIVATE POST
buy: function(currencyA, currencyB, rate, amount, callback){
if(!currencyA || !currencyB || !rate || !amount){
return callback(errorMsg,null);
}
var parameters = {
market: joinCurrencies(currencyA, currencyB),
price: rate,
volume: amount,
side : 'buy'
};
return this._privatePost('/orders.json', parameters, callback);
},
sell: function(currencyA, currencyB, rate, amount, callback){
if(!currencyA || !currencyB || !rate || !amount){
return callback(errorMsg,null);
}
else {
var parameters = {
market: joinCurrencies(currencyA, currencyB),
price: rate,
volume: amount,
side: 'sell'
};
return this._privatePost('/orders.json', parameters, callback);
}
},
cancelAllOrders : function(mSide, callback){
if(mSide){
var param = {
side : mSide
}
return this._privatePost("orders/clear.json",param,callback);
}
else{
return this._privatePost("orders/clear.json",{},callback);
}
},
cancelOrder : function(orderId,callback){
if(!orderId){
return callback(errorMsg,null);
}
else {
var param = {id: orderId}
return this._privatePost("/order/delete.json", param, callback);
}
}
};
return Yunbi;
})();